org.hibernate.UnknownEntityTypeException:无法找到持久性

时间:2017-09-13 09:29:08

标签: java hibernate hibernate-mapping

我正在阅读一本hibernate书的例子。但我无法根据实体名称加载数据。以下是详细信息

实体类

@Entity
public class SimpleNaturalIdEmployee implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Integer id;
    @NaturalId
    Integer badge;
    String name;

    public SimpleNaturalIdEmployee() {
    }

    public Integer getId() {
        return id;
    }

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

    public Integer getBadge() {
        return badge;
    }

    public void setBadge(Integer badge) {
        this.badge = badge;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SimpleNaturalIdEmployee)) return false;

        SimpleNaturalIdEmployee that = (SimpleNaturalIdEmployee) o;

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

        return true;
    }

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

我的hibernate配置文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/rd?autoReconnect=true</property>

        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!-- set up c3p0 for use -->
        <property name="c3p0.max_size">10</property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <mapping class="chapter06.primarykey.after.Book"/>
        <mapping class="chapter06.compoundpk.CPKBook"/>
        <mapping class="chapter06.compoundpk.EmbeddedPKBook"/>
        <mapping class="chapter06.compoundpk.IdClassBook"/>
        <mapping class="chapter06.twotables.Customer"/>

        <mapping class="chapter06.mappedsuperclass.ComputerBook"/>

        <mapping class="chapter06.naturalid.Employee"/>
        <mapping class="chapter06.naturalid.SimpleNaturalIdEmployee"/>
    </session-factory>
</hibernate-configuration>

我使用测试类来运行几个场景

@Test
public void testSimpleNaturalId() {
    Integer id = 107;

    try (Session session = getSession()) {
        Transaction tx = session.beginTransaction();

        SimpleNaturalIdEmployee employee =
                session
                        .byId(SimpleNaturalIdEmployee.class)
                        .load(id);
        assertNotNull(employee);
        SimpleNaturalIdEmployee badgedEmployee =
                session
                        .bySimpleNaturalId(SimpleNaturalIdEmployee.class)
                        .load(541011);
     //   assertEquals(badgedEmployee, employee);
        SimpleNaturalIdEmployee badgedEmployee1 =
                (SimpleNaturalIdEmployee) session.byId("SimpleNaturalIdEmployee").load(id);

        tx.commit();
    }
}

我正在使用hibernate 5,我尝试了所有建议的方法来创建一个会话,但注意到了

Configuration configuration = new Configuration().configure();
            StandardServiceRegistryBuilder builder = new 
StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
ServiceRegistry registry = builder.build();
factory = configuration.addAnnotatedClass(SimpleNaturalIdEmployee.class).buildSessionFactory(registry);
return factory.openSession();

但我仍然得到以下异常

org.hibernate.UnknownEntityTypeException:无法找到persister:SimpleNaturalIdEmployee     在org.hibernate.metamodel.internal.MetamodelImpl.locateEntityPersister(MetamodelImpl.java:647)     在org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2946)     在org.hibernate.internal.SessionImpl.access $ 1700(SessionImpl.java:203)     at org.hibernate.internal.SessionImpl $ IdentifierLoadAccessImpl。(SessionImpl.java:2689)     at org.hibernate.internal.SessionImpl $ IdentifierLoadAccessImpl。(SessionImpl.java:2679)     在org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1195)

我正在尝试加载使用实体名称请帮助

0 个答案:

没有答案