java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -


in code below when try execute main.java getting exception:

exception in thread "main" org.hibernate.unknownentitytypeexception: unable locate 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)     @ org.hibernate.internal.sessionimpl$identifierloadaccessimpl.<init>(sessionimpl.java:2575)     @ org.hibernate.internal.sessionimpl$identifierloadaccessimpl.<init>(sessionimpl.java:2562)     @ org.hibernate.internal.sessionimpl.byid(sessionimpl.java:1044)     @ org.hibernate.internal.sessionimpl.get(sessionimpl.java:955)     @ com.app.test.main.main(main.java:20) 

but if uncomment cfg.addclass( users.class ).addresource( "com/np/vta/test/pojo/users.hbm.xml" ); code works fine.

why not reading <mapping> hibernate.cfg.xml?


project setup

project setup eclipse


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 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 , 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 standardserviceregistrybuilder, configuration considered deprecated, instead make bootstrapping mentioned in hibernate 5 documentation, had same problem , 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 documentation


Comments

Popular posts from this blog

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -