網上這個錯誤的解決方法很多,我都試過了但是沒有一個行的,確認我的配置文件沒寫錯,請問是什麼問題?
報錯
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.hibernate.Student
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:781)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1520)
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:679)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
at com.hibernateservice.StudentTest.main(StudentTest.java:33)
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="SessionFac">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/hibernate/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-12-22 14:24:24 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping >
<class name="com.hibernate.Student" table="t_student">
<id name="id" column="stuId" type="java.lang.Integer">
<generator class="native"/>
</id>
<properties name="name" column="stuName" type="java.lang.String"></properties>
</class>
</hibernate-mapping>
Student.java:
package com.hibernate;
public class Student {
int id;
String name;
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
public Student() {
// TODO Auto-generated constructor stub
}
}
StudentTest.java:
package com.hibernateservice;
import com.hibernate.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class StudentTest {
public StudentTest() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
Configuration config = new Configuration().configure();
org.hibernate.service.ServiceRegistry registry =
new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
SessionFactory sessionFactory = config.buildSessionFactory(registry);
Session session = sessionFactory.openSession();
session.beginTransaction();
Student s = new Student();
s.setName("張三");
s.setId(0);
session.save(s);
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}
目錄:
你是使用配置文件配置entity的,把class那一串去了就行了