我正在使用Hibernate4和Spring,我正在尝试将Term类映射到Document类。术语可能出现在许多文档中,文档可能包含许多术语。我已经阅读了这篇文章,并尝试了一些修复,包括重写equals和hashCode方法。
在我的数据库中,术语和文档存储在由关系表调解的分隔表中。在这里的代码中,它被称为jtable。我正在使用hibernates多对多映射,但每次我尝试获取一个term对象时,它只是删除关系表中的行。
以下是hibernate的show sql发出的示例sql输出:
Hibernate:SELECT * FROM tags WHERE token='rose'
Hibernate:delete from jtable where tag_id=?
以下是相关课程和配置:
public class Term {
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Set<Document> getDocuments() {
return documents;
}
public void setDocuments(Set<Document> documents) {
documents = documents;
}
@Override
public boolean equals(Object obj) {
if(obj.getClass() != getClass()){
return false;
}
Term term = (Term) obj;
if (hashCode() == term.hashCode()){
return true;
}
return false;
}
@Override
public int hashCode(){
return word.hashCode();
}
private String word;
private int id;
private Set<Document> documents = new HashSet<Document>();
}
文档类:
public class Document implements Comparable<Document> {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public URL getUrl() {
return url;
}
public void setUrl(URL url) {
this.url = url;
}
public Integer getLength() {
return length;
}
public void setLength(Integer length) {
this.length = length;
}
public Double getRank() {
return rank;
}
public void setRank(Double rank) {
this.rank = rank;
}
@Override
public boolean equals(Object obj) {
if(obj.getClass() != getClass()){
return false;
}
Document document = (Document) obj;
if (hashCode() == document.hashCode()){
return true;
}
return false;
}
@Override
public int hashCode(){
int hash = 1;
hash *= 1 + (length == null ? 0 :length);
hash *= 2 + (rank == null ? 0 :rank);
hash *= 3 + (title == null ? 0 : title.hashCode());
hash *= 4 + (url.hashCode());
return hash;
}
@Override
public int compareTo(Document document) {
return rank.compareTo(document.getRank());
}
private int id;
private Integer length;
private Double rank;
private String title;
private URL url;
}
Document.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">
<hibernate-mapping>
<class name="package.Document" table="documents">
<id name="id" type="java.lang.Integer">
<column name="document_id" />
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="title" length="255" not-null="false" />
</property>
<property name="length">
<column name="length" not-null="false"/>
</property>
<property name="rank" type="java.lang.Double">
<column name="rank" not-null="false" />
</property>
<property name="url" type="org.hibernate.type.UrlType">
<column name="url" length="255" not-null="false" />
</property>
</class>
</hibernate-mapping>
Term.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">
<hibernate-mapping>
<class name="package.Term" table="tags">
<id name="id" type="java.lang.Integer">
<column name="tag_id" />
<generator class="native" />
</id>
<property name="word" type="string">
<column name="token" not-null="true" unique="true" />
</property>
<set name="documents" table="jtable"
inverse="false" lazy="true" fetch="select" cascade="all" >
<key>
<column name="tag_id" not-null="true" />
</key>
<many-to-many entity-name="package.Document">
<column name="document_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
相关的上下文配置部分:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.user}"/>
<property name="password" value="${database.password}"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>Document.hbm.xml</value>
<value>Term.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
我一直在看这个错误,所以任何答案都会受到赞赏。