P
P
P_Alexander2018-03-14 20:38:49
Java
P_Alexander, 2018-03-14 20:38:49

Why does not see the entity class? how to fix?

Started learning hibrnate and ran into a problem please help I am getting an
error

Exception in thread "main" org.hibernate.MappingException: Unknown entity: hibernate.pon.al.UserEntity
  at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620)
  at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1636)
  at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
  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:691)
  at org.hibernate.internal.SessionImpl.save(SessionImpl.java:683)
  at org.hibernate.internal.SessionImpl.save(SessionImpl.java:678)
  at hibernate.pon.al.util.Main.main(Main.java:21)

Java class without heteros and setors and that string
@Entity
@Table(name = "User", uniqueConstraints = {
        @UniqueConstraint(columnNames = "ID"),
        @UniqueConstraint(columnNames = "NICKNAME")
})
public class UserEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID",  length = 100)
    private Integer userID;

    @Column(name = "NICKNAME", length = 100)
    private String Nname;

    @Column(name = "FIRSTNAME", unique = false, length = 100)
    private String Fname;

    @Column(name = "LASTNAME", unique = false, length = 100)
    private String Lname;

    @Column(name = "PASSWORD", unique = false, length = 100)
    private String password;

    @Column(name = "PASSWORDTWO", unique = false, length = 100)
    private String passwordTwo;

    @Column(name = "ROLE", unique = false, length = 20)
    private String role;
}

file Hebrnate.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">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/testproj</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.username">alexroot</property>
        <property name="hibernate.connection.pool_size">20</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <mapping class="hibernate.pon.al.UserEntity"></mapping>
    </session-factory>
</hibernate-configuration>

class Util
public class HibernateUtil {
    private static SessionFactory sessionFactory;

    public static SessionFactory getSessionFactory(){
        if(sessionFactory == null){
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }
}


Main
SessionFactory sessionFactory =
                HibernateUtil.getSessionFactory();

        Session session = sessionFactory.openSession();
        session.beginTransaction();
        UserEntity entity = new UserEntity();
        entity.setFname("Alex");
        entity.setNname("ls");
        entity.setLname("Pon");
        entity.setPassword("qwe");
        entity.setPasswordTwo("qwe");
        entity.setRole("admin");
        session.save(entity);

        session.beginTransaction().commit();
        HibernateUtil.shutdown();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2018-03-14
@sergey-gornostaev

Problem in @Table(name = "User"). User is a reserved word in PostgreSQL, you cannot use it to name a table or field. Rename to Users, for example.

A
alexjdev, 2018-03-14
@alexjdev

Try changing the getSessionFactory method to:

public static SessionFactory getSessionFactory() {
   if (sessionFactory == null) {
            Configuration configuration = new Configuration();
            configuration.configure();
            sessionFactory = configuration.buildSessionFactory();
   }
   return sessionFactory;
 }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question