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)
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
vineeshchauhan
  • 765
  • 2
  • 7
  • 14

7 Answers7

1

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.

KayV
  • 12,987
  • 11
  • 98
  • 148
  • 1
    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 ?? – vineeshchauhan Dec 08 '15 at 14:20
  • 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. – KayV Dec 09 '15 at 10:09
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.

Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
Kandy
  • 673
  • 9
  • 21
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;
    }
Biswasini
  • 11
  • 1
0

your User not implemented Serializable, try with this:

 public class User implements Serializable{
   }
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
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.

-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>
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
-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.

Barett
  • 5,826
  • 6
  • 51
  • 55