UnknownEntityTypeException:无法找到persister(Hibernate 5.0) [英] UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0)

查看:374
本文介绍了UnknownEntityTypeException:无法找到persister(Hibernate 5.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,当我尝试执行 Main.java 时,我收到了异常:

 线程main中的异常org.hibernate.UnknownEntityTypeException:无法找到persister:com.np.vta.test.pojo.Users 
在org.hibernate.internal.SessionFactoryImpl.locateEntityPersister( SessionFactoryImpl.java:792)
在org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2637)
在org.hibernate.internal.SessionImpl.access $ 2500(SessionImpl.java:164)
at org.hibernate.internal.SessionImpl $ IdentifierLoadAccessImpl。< init>(SessionImpl.java:2575)
at org.hibernate.internal.SessionImpl $ IdentifierLoadAccessImpl。< init>(SessionImpl.java:2562 )
at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1044)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:955)
at com。 app.test.Main.main(Main.java:20)

但如果我取消注释 cfg.ad dClass(Users.class).addResource(com / np / vta / test / pojo / Users.hbm.xml); 那么代码就可以正常工作。



为什么它不是从 hibernate.cfg.xml <?>中读取< mapping> / p>




项目设置




In the code below when I try to execute Main.java I am getting exception:

Exception in thread "main" org.hibernate.UnknownEntityTypeException: Unable to locate persister: com.np.vta.test.pojo.Users
    at org.hibernate.internal.SessionFactoryImpl.locateEntityPersister(SessionFactoryImpl.java:792)
    at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2637)
    at org.hibernate.internal.SessionImpl.access$2500(SessionImpl.java:164)
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2575)
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2562)
    at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1044)
    at org.hibernate.internal.SessionImpl.get(SessionImpl.java:955)
    at com.app.test.Main.main(Main.java:20)

but if I do uncomment cfg.addClass( Users.class ).addResource( "com/np/vta/test/pojo/Users.hbm.xml" ); then the code works fine.

Why it is not reading the <mapping> from hibernate.cfg.xml?


Project setup


hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">root@123321</property>
        <property name="hibernate.connection.url">jdbc:mysql://192.168.1.90:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <mapping resource="com/np/vta/test/pojo/Users.hbm.xml" class="com.np.vta.test.pojo.Users" />
    </session-factory>
</hibernate-configuration>


Users.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 24 Aug, 2015 3:57:45 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.np.vta.test.pojo.Users" table="tomcat_users">
        <id name="userName" type="java.lang.String">
            <column name="user_name" />
            <generator class="assigned" />
        </id>
        <property name="password" type="java.lang.String">
            <column name="password" />
        </property>
    </class>
</hibernate-mapping>


Users.java

package com.np.vta.test.pojo;

import java.io.Serializable;

public class Users implements Serializable
{
    private static final long serialVersionUID = 7855937172997134350L;
    private String            userName;
    private String            password;

    public Users()
    {
    }
    // getter and setters
}


Main.java

package com.app.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import com.np.vta.test.pojo.Users;

public class Main
{
    public static void main( String[] args )
    {
        Configuration cfg = new Configuration();
        cfg.configure( "hibernate.cfg.xml" );
        //        cfg.addClass( Users.class ).addResource( "com/np/vta/test/pojo/Users.hbm.xml" );
        StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() );
        SessionFactory sessionFactory = cfg.buildSessionFactory( registryBuilder.build() );
        Session session = sessionFactory.openSession();
        Users users = session.get( Users.class, "anand" );
        System.out.println( users );
    }
}

解决方案

Don't use Configuration with StandardServiceRegistryBuilder, Configuration is considered deprecated, but instead make the bootstrapping as mentioned in the hibernate 5 documentation, I had the same problem and this fixed it.

StandardServiceRegistry standardRegistry = new     StandardServiceRegistryBuilder()
    .configure( "org/hibernate/example/MyCfg.xml" )
    .build();

Metadata metadata = new MetadataSources( standardRegistry )
    .addAnnotatedClass( MyEntity.class )
    .addAnnotatedClassName( "org.hibernate.example.Customer" )
    .addResource( "org/hibernate/example/Order.hbm.xml" )
    .addResource( "org/hibernate/example/Product.orm.xml" )
    .getMetadataBuilder()
    .applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE )
    .build();

SessionFactory sessionFactory = metadata.getSessionFactoryBuilder()
    .applyBeanManager( getBeanManagerFromSomewhere() )
    .build();

for further details , check the documentation

这篇关于UnknownEntityTypeException:无法找到persister(Hibernate 5.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆