线程“main”中的异常org.hibernate.MappingException:未知实体:org.hibernate.internal.SessionImpl

时间:2015-12-22 17:55:36

标签: java hibernate

这是我的第一个Hibernate应用程序。我在执行主类时遇到了这个错误。我试图在表学生中插入新记录。还可以说hibernate在没有任何问题的情况下连接到数据库!我不断得到这个MappingException:

Hibernate: drop table if exists student 
Hibernate: create table student  (student_id integer not null auto_increment, First_Name varchar(255), Last_Name varchar(255), Age integer,  primary key (student_id))
Dec 22, 2015 10:04:44 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.hibernate.internal.SessionImpl
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1146)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1358)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:116)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:683)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:675)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
at biztime.Manager.main(Manager.java:23)

有没有人有任何想法,因为我看了很多重复,但决议似乎对我不起作用。这是我的cfg.xml

我的hibernate.cfg.xml

   <!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>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/anwardb</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">create-drop</property>
    <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
    <mapping resource="biztime/student.hbm.xml"/>    
    </session-factory>
    </hibernate-configuration>

我的student.java

package biztime;
public class student 
{
    private int student_id;
    private String First_Name;
    private String Last_Name;
    private int Age;
    public int getStudent_id() 
    {
      return student_id;
    }

 public void setStudent_id(int student_id) 
  {
    this.student_id = student_id;
  }
public String getFirst_Name() 
{
    return First_Name;
}
public void setFirst_Name(String first_Name) 
{
    First_Name = first_Name;
}

public String getLast_Name() 
{
    return Last_Name;
}

public void setLast_Name(String last_Name) 
{
    Last_Name = last_Name;
}

  public int getAge() 
 {
    return Age;
 }

  public void setAge(int age)

  {
    Age = age;
  }
}

student.hbm.xml

     <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">


    <hibernate-mapping package="biztime">

    <class name="student" table="student ">
    <id name="student_id" column="student_id">
        <generator class="native"/>
    </id>
    <property name="First_Name" >
        <column name="First_Name"/>
    </property>
    <property name="Last_Name">
        <column name="Last_Name"/>
    </property>
    <property name="Age">
        <column name="Age"/>
    </property>
</class>

</hibernate-mapping>

我用这个主要来称呼它:

Manager.java

  package biztime;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;

   public class Manager 
  {

   public static void main(String[] args) 
  {

    Configuration con=new Configuration();
    con.configure();
    SessionFactory sf=con.buildSessionFactory();

    Session s1=sf.openSession();
    student  s=new student ();
    s.setFirst_Name("hello");
    s.setLast_Name("test");
    s.setStudent_id(1);
    s.setAge(20);
    s1.beginTransaction();
    s1.save(s1);
    s1.getTransaction().commit();
    s1.flush();
    s1.close();
    System.out.println("done");

    }
 }

2 个答案:

答案 0 :(得分:1)

您正在尝试保存s1 Session。保存s,即student(并将您的类名称大写)。

答案 1 :(得分:0)

我认为你不应该设置student_id,因为它是auto_increment字段。请尝试删除此行:s.setStudent_id(1);