简体   繁体   English

即使我声明映射类,未知实体也会休眠

[英]Unknown entity hibernate even if I declare mapping class

I tried to use hibernate to connect to Mysql. 我试图使用休眠模式连接到Mysql。 I generate an entity with Intellij JPA support and I receive the Unknown entity: com.UsersEntity even if I declare the mapping in the hibernate.cfg.xml . 我生成了具有Intellij JPA支持的实体,并且即使我在hibernate.cfg.xml声明了映射,也收到了Unknown entity: com.UsersEntity

Entity: 实体:

package com;
import javax.persistence.*;
@Entity
@Table(name = "users", schema = "", catalog = "protein_tracker")

public class UsersEntity {
    private int id;
    private String name;
    private int total;
    private int goal;

    @Id
    @Column(name = "id", nullable = false, insertable = true, updatable = true)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "name", nullable = false, insertable = true, updatable = true, length = 45)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Basic
    @Column(name = "total", nullable = false, insertable = true, updatable = true)
    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    @Basic
    @Column(name = "goal", nullable = false, insertable = true, updatable = true)
    public int getGoal() {
        return goal;
    }

    public void setGoal(int goal) {
        this.goal = goal;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UsersEntity that = (UsersEntity) o;

        if (id != that.id) return false;
        if (total != that.total) return false;
        if (goal != that.goal) return false;
        if (name != null ? !name.equals(that.name) : that.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + total;
        result = 31 * result + goal;
        return result;
    }
}

Configuration file: 配置文件:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/protein_tracker</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.default_schema">protein_tracker</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">AmPiramide69</property>
        <mapping class="com.UsersEntity" />
        <mapping resource="com/UsersEntity.hbm.xml"/>
        <!-- DB schema will be updated if needed -->
        <!-- <property name="hbm2ddl.auto">update</property> -->
    </session-factory>
</hibernate-configuration>

Session factory creation: 会话工厂创建:

package com.simpleprogrammer;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtilities {
    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    static {
        try {
            Configuration configuration = new Configuration().configure();

            serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }
        catch (HibernateException exeption) {
            exeption.printStackTrace();
            System.out.println("Problem creating session factory!");
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

How can I use it: 我如何使用它:

package com.simpleprogrammer;

import com.UsersEntity;
import org.hibernate.Session;

public class Main {

    public static void main(String[] args) {
        Session session = HibernateUtilities.getSessionFactory().openSession();
        session.beginTransaction();

        UsersEntity usersEntity = new UsersEntity();

        usersEntity.setName("Name");
        usersEntity.setTotal(20);
        session.save(usersEntity);

        session.getTransaction().commit();
        session.close();
        HibernateUtilities.getSessionFactory().close();
    }
}

Do you know what is the problem? 你知道是什么问题吗?

Try using AnnotationConfiguration() rather than Configuration() when you build your SessionFactory. 在构建SessionFactory时,请尝试使用AnnotationConfiguration()而不是Configuration()。

Then you only need the 'mapping class=""' tags. 然后,您只需要'mapping class =“”'标签。 (Not the 'mapping resource=""' tags) in your hibernate configuration file. (而不是休眠配置文件中的'mapping resource =“”标签)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM