org.hibernate.MappingException: Unknown entity: hibernate.Employee2? 400 报错
用eclipse 从数据库逆向生成Hibernate实体类。。出错
Exception in thread "main" org.hibernate.MappingException: Unknown entity: hibernate.Employee2 at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:580) at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1365) 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:562) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:546) at test.Test.main(Test.java:20) Employee2.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"> <!-- Generated 2012-8-12 19:37:51 by Hibernate Tools 4.0.0 --> <hibernate-mapping> <class name="hibernate.Employee2" table="EMPLOYEE2" schema="SCOTT"><!--是这里错了吗? --> <id name="id" type="long"> <column name="ID" precision="10" scale="0" /> <generator class="assigned" /> </id> <property name="name" type="string"> <column name="NAME" length="5" /> </property> <property name="email" type="string"> <column name="EMAIL" length="15" /> </property> <property name="hiredate" type="date"> <column name="HIREDATE" length="7" /> </property> </class> </hibernate-mapping>
package test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import hibernate.Employee2; public class Test { public static void main(String[] args) { Configuration cf=new Configuration().configure(); SessionFactory sf=cf.buildSessionFactory(); Session s=sf.openSession(); Employee2 employee2=new Employee2(123,"123","123@456.com",new java.util.Date()); Transaction tx= s.beginTransaction(); //s.save(employee2); s.persist(employee2); tx.commit(); } }
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="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.password">418gyz</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@0.0.0.0:1522:orcl2</property> <property name="hibernate.connection.username">scott</property> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> </session-factory> </hibernate-configuration> Employee2.java package hibernate; // Generated 2012-8-12 19:37:50 by Hibernate Tools 4.0.0 import java.util.Date; /** * Employee2 generated by hbm2java */ public class Employee2 implements java.io.Serializable { /** * */ private static final long serialVersionUID = 1L; private long id; private String name; private String email; private Date hiredate; public Employee2() { } public Employee2(long id) { this.id = id; } public Employee2(long id, String name, String email, Date hiredate) { this.id = id; this.name = name; this.email = email; this.hiredate = hiredate; } public long getId() { return this.id; } public void setId(long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public Date getHiredate() { return this.hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } }
在你的hibernate.cfg.xml里面没有引入 Employee2.hbm.xml
需要在hibernate.cfg.xml中加入
<mapping resource="com/Employee2.hbm.xml " />
具体路径,自己写。
######hibernate配置文件添加了映射文件吗
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。