Exception in thread “main” org.hibernate.MappingException: Unknown entity: cqu.edu.entity.Students
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1451)
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:678)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:670)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:665)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
at com.sun.proxy.$Proxy19.save(Unknown Source)
at cqu.edu.entity.TestStudents.testSaveStudent(TestStudents.java:48)
at cqu.edu.entity.TestStudents.main(TestStudents.java:58)
Students.hbm.xml文件:
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE hibernate-mapping PUBLIC
“-//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd”>
<hibernate-mapping>
<class name=”cqu.edu.entity.Students” table=”STUDENTS”>
<id name=”sid” type=”java.lang.String” length=”8″>
<generator class=”assigned”/>
</id>
<property name=”sname” type=”java.lang.String”></property>
<property name=”gender” type=”java.lang.String”></property>
<property name=”birthday” type=”java.util.Date”></property>
<property name=”address” type=”java.lang.String”></property>
</class>
</hibernate-mapping>
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”>
<hibernate-configuration>
<session-factory >
<property name =”connection.username”>root</property>
<property name =”connection.password”>123456</property>
<property name= “connection.driver_class”>com.mysql.jdbc.Driver </property > <!– 驱动路径 –>
<property name= “connection.url”>jdbc:mysql://127.0.0.1:3306/students_database?useUnicode=true&characterEncoding=UTF-8</property ><!– 连接数据库字符串 –>
<property name= “hibernate.dialect”>org.hibernate.dialect.MySQLDialect</property >
<property name =”show_sql”>true</property> <!– 显示SQL语句 –>
<property name =”format_sql”>true</property>
<property name =”hbm2ddl.auto”>update</property>
<!– 配置使用session.getCurrentSession()方法 –>
<property name= “hibernate.current_session_context_class” >thread</property >
<property name=”javax.persistence.validation.mode”>none</property>
<!– hbm.xml –>
<mapping resource =”cqu/edu/entity/Users.hbm.xml”/>
<mapping resource =”cqu/edu/entity/yStudents.hbm.xml”/>
</session-factory >
</hibernate-configuration>
测试类:
@Test
public void testSaveStudent(){
Configuration cfg = new Configuration();
cfg.configure();
Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
session.beginTransaction();
Transaction tx=session.beginTransaction();
// cfg.addClass(cqu.edu.entity.Students.class);
Students student1=new Students(“S0000001″,”张三”,”男”,new Date(),”重庆”);
Students student2=new Students(“S0000002″,”李四”,”男”,new Date(),”海南”);
Students student3=new Students(“S0000003″,”王五”,”男”,new Date(),”四川”);
session.save(student1);
session.save(student2);
session.save(student3);
tx.commit();
}
本人明明已经配置了mapping为什么还说找不到本人的实体类?麻烦大家帮本人看看,谢谢
5
<mapping resource =”cqu/edu/entity/yStudents.hbm.xml”/>
你瞅瞅,这个什么鬼
5
<mapping resource =”cqu/edu/entity/yStudents.hbm.xml”/>去掉yStudents.hbm.xml左边的y
10
<property name=”mappingLocations”>
<list>
<value>classpath:org/model/*.hbm.xml</value>
</list>
</property>
这样的路径映射吧?
10
10
你的映射路径写错了