我如何解决它在线程“主要” org.hibernate.MappingException异常:未知实体

时间:2018-07-19 13:20:06

标签: java postgresql hibernate

我在线程“ main” org.hibernate.MappingException中遇到异常:未知实体:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.koushik.javabrains.dto.UserDetails
    at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693)
    at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1485)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:120)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
    at org.koushik.hibernate.HibernateTest.main(HibernateTest.java:24)

1。

<hibernate-configuration>

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
    <property name="connection.username">postgres</property>
    <property name="connection.password">16101995</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

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

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

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

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>

 <mapping class="org.koushik.javabrains.dto.UserDetails"/>     


</session-factory>

2。

   package org.koushik.javabrains.dto;

     @Entity

     public class UserDetails {

    @Id
private int userID;
private String userName;


public int getUserID() {
    return userID;
}
public void setUserID(int userID) {
    this.userID = userID;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}

    }

3。

  package org.koushik.hibernate;


 import org.hibernate.Session;
    import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  import org.koushik.javabrains.dto.UserDetails;

     public class HibernateTest {

public static void main(String[] args)  {

    UserDetails user=new UserDetails();
    user.setUserID(1);
    user.setUserName("First User");


    SessionFactory sessionFactory= new Configuration().configure().buildSessionFactory() ;

    Session session= sessionFactory.openSession();

    session.beginTransaction();

    session.save(user);

    session.getTransaction().commit();
    session.getTransaction().commit(); 
       session.close();



}

    }

0 个答案:

没有答案