读书人

hibernate实体映射错误

发布时间: 2013-08-14 14:27:55 作者: rapoo

hibernate实体映射异常
八月 13, 2013 6:11:05 下午 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
八月 13, 2013 6:11:05 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.1.Final}
八月 13, 2013 6:11:05 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
八月 13, 2013 6:11:05 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
八月 13, 2013 6:11:05 下午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
八月 13, 2013 6:11:05 下午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
八月 13, 2013 6:11:05 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/luojia/zwc/domain/User.hbm.xml
八月 13, 2013 6:11:05 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/luojia/zwc/domain/Review.hbm.xml
八月 13, 2013 6:11:05 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/luojia/zwc/domain/Topic.hbm.xml
八月 13, 2013 6:11:05 下午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Exception in thread "main" org.hibernate.MappingException: Association references unmapped class: com.luojia.zwc.domain.Topic
at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2517)
at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2803)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:69)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1603)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1362)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1747)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1798)
at com.luojia.zwc.test.HibernateTest.main(HibernateTest.java:11)


hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>


<!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="hibernate.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">zeng</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/forum</property>
<property name="hibernate.hbm2ddl.auto">create</property>

<mapping resource="com/luojia/zwc/domain/User.hbm.xml"/>
<mapping resource="com/luojia/zwc/domain/Review.hbm.xml"/>
<mapping resource="com/luojia/zwc/domain/Topic.hbm.xml"/>

</session-factory>
</hibernate-configuration>

User.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 package="com.luojia.zwc.domain">
<class name="User" table="user_table">
<id name="userId" column="user_id" type="integer">
<generator class="identity"/>
</id>
<property name="userName" column="username" not-null="true" type="string" length="50"/>
<property name="password" column="password" not-null="true" type="string" length="20"/>
<property name="gender" column="gender" not-null="true" type="char"/>
<property name="registerTime" column="register_time" not-null="true" type="date"/>
<set name="topics" inverse="true">
<key column="user_id"/>
<one-to-many class="com.luojia.zwc.domain.Topic"/>
</set>
</class>


</hibernate-mapping>


Topic.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="Topic" table="topic_table">
<id name="topicId" column="topic_id" type="integer">
<generator class="identity"/>
</id>
<property name="title" column="title" type="string" not-null="true" length="50"/>
<property name="content" column="content" type="string" not-null="true" length="300"/>
<property name="publishTime" column="publish_time" type="date" not-null="true"/>
<set name="reviews" inverse="true">
<key column="topic_id"/>
<one-to-many class="com.luojia.zwc.domain.Review"/>
</set>
</class>
</hibernate-mapping>


Review.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="Review" table="review_table">
<id name="reviewId" column="review_id" type="integer">
<generator class="identity"/>
</id>
<property name="content" column="content" type="string" not-null="true" length="300"/>
<property name="reviewTime" column="review_time" type="date" not-null="true"/>
<many-to-one name="user" class="com.luojia.zwc.domain.User" column="user_id" not-null="true"/>
<many-to-one name="topic" class="com.luojia.zwc.domain.Topic" column="topic_id" not-null="true"/>
</class>
</hibernate-mapping>



HibernateTest.java
package com.luojia.zwc.test;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class HibernateTest {
public static void main(String args[]) {
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session sess = sf.openSession();
Transaction ts = sess.beginTransaction();


ts.commit();
sess.close();
sf.close();
}
}

[解决办法]
Topic.hbm.xml和Review.hbm.xml里的<hibernate-mapping>是不是忘记定package了?
<hibernate-mapping package="com.luojia.zwc.domain">

读书人网 >J2EE开发

热点推荐