0

I tried to see all the questions on this topic but none of them helped me, I have tried all possible way to resolve the issue, but not able to.I want to understand where I have done mistake in my code.

My project structure is

 hibernateOnline
       |---------src/main/java
                       |------------com.hibernateOnline.data
                                               |---------HibernateUtil.java
                       |------------com.hibernateOnline.data.entities
                                               |------------------User.java
       |---------src/main/resources
                       |------------hibernate.cfg.xml

Below is my User class where I have used all the JPA annotation not specific to hibernate

package com.hibernateOnline.data.entities;

@Entity

@Table(name = "finances_user")

public class User   

{

   @Id

   @GeneratedValue(strategy=GenerationType.IDENTITY)

   @Column(name="USER_ID")

   private Long userId;

   @Column(name="FIRST_NAME")
   private String firstName;

   @Column(name="LAST_NAME")
   private String lastName;

   @Column(name="BIRTH_DATE")
   private Date birthDate;

   @Column(name="EMAIL_ADDRESS")
   private String emailAddress;

   @Column(name="LAST_UPDATED_DATE")
   private Date lastUpdatedDate;

   @Column(name="LAST_UPDATED_BY")
   private String lastUpdatedBy;

   @Column(name="CREATED_DATE")
   private Date createdDate;

   @Column(name="CREATED_BY")
   private String createdBy;

   public Long getUserId() {
       return userId;
   }

   public void setUserId(Long userId) {
       this.userId = userId;
   }

   public String getFirstName() {
       return firstName;
   }

   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }

   public String getLastName() {
       return lastName;
   }

   public void setLastName(String lastName) {
       this.lastName = lastName;
   }

   public Date getBirthDate() {
       return birthDate;
   }

   public void setBirthDate(Date birthDate) {
      this.birthDate = birthDate;
   }

   public String getEmailAddress() {
       return emailAddress;
   }

   public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public Date getLastUpdatedDate() {
        return lastUpdatedDate;
    }

    public void setLastUpdatedDate(Date lastUpdatedDate) {
        this.lastUpdatedDate = lastUpdatedDate;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public String getLastUpdatedBy() {
        return lastUpdatedBy;
    }

    public void setLastUpdatedBy(String lastUpdatedBy) {
        this.lastUpdatedBy = lastUpdatedBy;
    }
}

Next is the HibernateUtils class where I am creating the sessionFactory

package com.hibernateOnline.data;

public class HibernateUtil 
{
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            return configuration
                   .buildSessionFactory(new StandardServiceRegistryBuilder()
                            .applySettings(configuration.getProperties())
                            .build());
            } catch (Exception e) {
              e.printStackTrace();
              throw new RuntimeException(
                "There was an error building the factory");
            }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

Now the configuration file "hibernate.cfg.xml" which I am using for configuration of MySQL database and to mapped the User class

<?xml version='1.0' encoding='utf-8'?>
<!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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
        <property name="connection.username">****</property>
        <property name="connection.password">*****</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>


        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <mapping class="com.hibernateOnline.data.entities.User"/>

  </session-factory>

</hibernate-configuration>

After done with all those things when I am trying to run the program it showing below error it is not able to find out the User class

DEBUG - begin
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.hibernateOnline.data.entities.User
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1451)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:678)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:670)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:665)
    at com.hibernateOnline.data.Application.main(Application.java:25)

Can anyone please help me out to solve this problem??

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29

4 Answers4

0

You need to add the hbm.xml to cfg.xml

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
0

you need change org.hibernate to javax.persistence in annotation Entity

0

JPA annotations and Hibernate config files don't work together. An entity defined by annotating a class with JPA annotation @Entity will not be visible to hibernate configuration file as an entity. Hence, it is not able to find it and gives and error.

You need to choose one between JPA OR Hibernate. Then -

  1. If you choose to use JPA then you need to make a persistence.xml file. Look at the link - https://docs.jboss.org/hibernate/orm/3.6/quickstart/en-US/html/hibernate-gsg-tutorial-jpa.html for all the settings. In JPA settings you will need to provide ORM adapter/provider for which you can use Hibernate provider. But remember that all your annotations/settings/configurations will need to be as per JPA standards.

  2. Else, if you want to use Hibernate then similarly do annotations/settings/configurations as per Hibernate.

Dhruv Rai Puri
  • 1,335
  • 11
  • 20
0

as mentioned by @Dhruv rai Puri.

JPA annotations and Hibernate config files don't work together.. To get a solution to this question, you may visit No Persistence provider for EntityManager named. Complete solution to this question is provided using Hibernate 5.2.5 Final.

Community
  • 1
  • 1