Exception in thread "main" org.hibernate.UnknownEntityTypeException: Unable to locate persister
Asked Answered
G

8

10

I have to ask it, I almost tried everything.

Entity Class


@Entity
@Table(name="UserInfo")
public class User {

    @Id@Column(name="user_name")
    private String userName;

    @Column(name="user_id")
    private Integer userId;
}

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=BankingApplication</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.connection.password">2OmniWay</property>

        <!-- org.hibernate.HibernateException: No CurrentSessionContext configured! -->
        <property name="hibernate.current_session_context_class">thread</property>
         <property name="hibernate.default_schema">dbo</property>
        <!-- Mapping with model class containing annotations -->
        <mapping class="pojo.User"/>
    </session-factory>  
</hibernate-configuration>

SeesionFactory

// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate-annotation.cfg.xml");
System.out.println("Hibernate Annotation Configuration loaded");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().                                    applySettings(configuration.getProperties()).build();
 System.out.println("Hibernate Annotation serviceRegistry created");             
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

return sessionFactory;

Getting Object saved in DB


SessionFactory sessionFactory = HibernateUtil.getSessionAnnotationFactory();
        Session session = sessionFactory.openSession();
        Account account = null;
        try{
        account = (Account) session.get(Account.class, id);

I am getting exception mentioned in subject. I just tripled checked everything but it is just not working.enter image description here

Any suggestion would be helpful.

___________Stack Trace_________

 Exception in thread "main" org.hibernate.UnknownEntityTypeException: Unable to locate persister: pojo.User
at org.hibernate.internal.SessionFactoryImpl.locateEntityPersister(SessionFactoryImpl.java:792)
at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2652)
at org.hibernate.internal.SessionImpl.access$2500(SessionImpl.java:164)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2590)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2577)
at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1044)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:955)
at business.ManageAccount.getAccountDetails(ManageAccount.java:18)
at Utils.TestConnecttion.main(TestConnecttion.java:16)
Getaway answered 7/12, 2015 at 7:12 Comment(2)
Where is stacktrace?Fir
how is this finally solved?Funds
M
3

You need to specifically add the entity classes in the configuration. By default it loads the classes but do not initialize the entity classes. For that configuration.addAnnotatedClass() or addResource will solve this out.

Mange answered 7/12, 2015 at 10:12 Comment(2)
by using addAnnotatedClass, it is working. But it is not good design if I have to specify each entity while creating SessionFactory. I want this to be part of configuration. idea ??Getaway
For that you need you create .hbm for every entity and configure that hbm file with hibernate.cfg.xml. I think this is the only way to make it a part of configuration.Mange
I
1

replace your code

ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();

from SessionFactory to

ServiceRegistry  serviceRegistry = new ServiceRegistryBuilder().applySettings(
            configuration.getProperties()). buildServiceRegistry();

and try to make your sessionfactory and serviceRegistry static in class

May it works for you.

Irfan answered 7/12, 2015 at 11:10 Comment(1)
what version of hibernate you are usingIrfan
R
1
//I am using Hib5,I have created SF like this and it is working
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build();
            Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().build();
            sessionFactory = metadata.getSessionFactoryBuilder().build();
        }
        return sessionFactory;
    }
Ragouzis answered 17/2, 2018 at 9:48 Comment(0)
B
0

your User not implemented Serializable, try with this:

 public class User implements Serializable{
   }
Basset answered 7/12, 2015 at 8:17 Comment(7)
It is compile time exception. addAnnotatedClass is method of configuration object.Getaway
I have added configuration.addAnnotatedClass(Account.class); line after configuration.configure("hibernate-annotation.cfg.xml"); and it works. But I want to know why it is not picking mappings from cfg.xml files ?Getaway
you added only User.java to cfg.xml file ,what about other Entities?Ingathering
I have added all entities in cfg.xml. Hibernate is not picking any of them, be it User or Account. I have shown cfg.xml as short as possible. I want Hibernate to pick mapping from cfg.xml not from solution provided above. any view ?Getaway
@Getaway see the answerBasset
public class User implements Serializable{ } - Good catch but no impact.Getaway
@youshchauhan if are you reach a answer canyou share with us thank you so muchBasset
C
0

Hibernate version 6.0 moves from Java Persistence as defined by the Java EE specs to Jakarta Persistence. Instead of using the entity of java persistence use Jakarta.

Caudle answered 11/3, 2023 at 21:22 Comment(0)
C
0

kindly check either you have added and added your class correctly in hibernate.cfg.xml

Casa answered 19/8 at 6:31 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Cornhusking
I
-2

You did not add Dialect to your Hibernate config file.Add this line in your config file.

<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
Ingathering answered 7/12, 2015 at 7:35 Comment(2)
It is automatically loaded. I can see it in logs. something is wrong somewhere else.Getaway
I have also added but no success.Getaway
G
-2

Your syntax works in Hibernate 4.x, but causes an error with Hibernate 5.x.

The configuration file causes this problem. In the configuration XML file, you added a mapping file, such as:

mapping resource="hibernate.map.xml"

Remove this and add this programatically instead:

configure.addResource(hibernate.map.xml);

It will surely work.

Galatians answered 28/1, 2016 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.