hibernate配置导致的运行时报错 5C

报错信息:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.hibernate.impl.SessionImpl
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:628)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1366)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
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:563)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:551)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
at test.StudentTest.main(StudentTest.java:23)

配置文件1.hibernate.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">

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:32080/hibernate</property>
    <property name="connection.username">system</property>
    <property name="connection.password">system</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
    <mapping resource="com/tao/hibernate/model/Student.hbm.xml"/>
</session-factory>

配置文件2.Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">







测试类StudentTest.java:

package test;

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

import com.tao.hibernate.model.Student;

public class StudentTest
{
public static void main(String[] args)
{
Student s = new Student();
s.setId(1);
s.setAge(23);
s.setName("ds");

    Configuration cfg = new Configuration();
    SessionFactory sf = cfg.configure().buildSessionFactory();
    Session session = sf.openSession();

    session.beginTransaction();
    session.save(session);
    session.getTransaction().commit();
    session.close();
    sf.close();

}

}

实体类:Student.java
package com.tao.hibernate.model;

public class Student
{

private int     id;
private String  name;
private int     age;

public int getId()
{
    return id;
}

public void setId(int id)
{
    this.id = id;
}

public String getName()
{
    return name;
}

public void setName(String name)
{
    this.name = name;
}

public int getAge()
{
    return age;
}

public void setAge(int age)
{
    this.age = age;
}

@Override
public String toString()
{
    return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}

}

7个回答

Student.hbm.xml

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







rtyrtfyhgfhgfhgfhfghgfhgfhgf

rtyrtfyhgfhgfhgfhfghgfhgfhgf

sadasdasdasdasdasdasdasdasdasdasd

实体映射的错误,看看你配置文件的位置有没有放错。或者.cfg.xml文件中有没有写mapper啊等等。希望对你有用图片说明

hibernate jar包有错误吧! 你看你的lab下面有没有Hibernate的jar包

Unknown entity: org.hibernate.impl.SessionImpl,配置文件看了一下关于student的貌似没有问题,有可能就是你少了什么jar包,建议jar包导全

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!