已经郁闷两天了 hibernate 报Duplicate collection role mapping错误
自己做的一个hibernate的小程序,两个类TLendLog 和TBookMaster 还有它们的配置文件。
然后自己做了一个测试类 test1,里面只有一句话。 程序很简单,但是报 Duplicate collection role mapping错误
弄得我已经郁闷两天了,麻烦高手看一下。
package book.bean;
public class TLendLog implements java.io.Serializable {
private Long userMaster;
private Long bookMaster;
private String updatePerson;
public String getUpdatePerson() {
return updatePerson;
}
public void setUpdatePerson(String updatePerson) {
this.updatePerson = updatePerson;
}
public Long getUserMaster() {
return userMaster;
}
public void setUserMaster(Long userMaster) {
this.userMaster = userMaster;
}
public Long getBookMaster() {
return bookMaster;
}
public void setBookMaster(Long bookMaster) {
this.bookMaster = bookMaster;
}
}
package book.bean;
import java.util.*;
public class TBookMaster implements java.io.Serializable {
private Long bookId;
private String bookTitle;
private Set logs = new HashSet(0);
public Long getBookId() {
return this.bookId;
}
public void setBookId(Long bookId) {
this.bookId = bookId;
}
public String getBookTitle() {
return this.bookTitle;
}
public void setBookTitle(String bookTitle) {
this.bookTitle = bookTitle;
}
public Set getLogs() {
return logs;
}
public void setLogs(Set logs) {
this.logs = logs;
}
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="book.bean.TBookMaster" table="T_BOOK_MASTER" schema="SCOTT">
<id name="bookId" type="java.lang.Long">
<column name="BOOK_ID" precision="9" scale="0" />
<generator class="assigned" />
</id>
<property name="bookTitle" type="java.lang.String">
<column name="BOOK_TITLE" length="30" not-null="true" />
</property>
<set name="logs" inverse="true">
<key>
<column name="BOOK_ID" not-null="true" />
</key>
<one-to-many class="book.bean.TLengLog" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="book.bean.TLendLog" table="T_LEND_LOG" schema="SCOTT">
<id name="userMaster" type="java.lang.Long">
<column name="USER_ID" precision="9" scale="0" />
<generator class="assigned" />
</id>
<property name="bookMaster" type="java.lang.Long">
<column name="BOOK_ID" length="7" />
</property>
<property name="updatePerson" type="java.lang.String">
<column name="UPDATE_PERSON" length="10" />
</property>
</class>
</hibernate-mapping>
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">scott</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:P6LJRN65DVAYT5BMCC9WHR</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="myeclipse.connection.profile">ltt</property>
<property name="connection.password">tiger</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="book/bean/TBookMaster.hbm.xml" />
<mapping resource="book/bean/TLendLog.hbm.xml" />
</session-factory>
</hibernate-configuration>
package book.bean;
import org.hibernate.Session;
import book.hibernate.HibernateSessionFactory;
public class test1 {
public static void main(String[] args) {
Session s = HibernateSessionFactory.getSession();
}
}
注我把book.bean.TBookMaster的hibernate配置文件中的<set> .....</set>去掉,
程序正常运行,我在网上有看到过对这的解释,说是类名拼错了,但是我仔细检查过了,没写错。望大家帮忙仔细看一下是那里错了
先谢过了
[解决办法]
collection role mapping
lz在
<class name="book.bean.TBookMaster" table="T_BOOK_MASTER" schema="SCOTT">.....
<set name="logs" inverse="true">
<key>
<column name="BOOK_ID" not-null="true" />
</key>
<one-to-many class="book.bean.TLengLog" />
</set>
配置了一对多
而没有在<class name="book.bean.TLendLog" table="T_LEND_LOG" schema="SCOTT"> ...配置多对一
配置一下看看。
[解决办法]
<column name="BOOK_ID" not-null="true" />
你确定在数据库里BOOK_ID 是这个外键
[解决办法]
<set name="logs" inverse="true"> <key> <column name="BOOK_ID" not-null="true" /> </key> <one-to-many class="book.bean.TLengLog" /> </set>