这个问题困扰我好久了,实在找不到原因,只能来问一下大家了,我一对一关联的是文章表和内容表,据说某个属性可以延迟加载,但是好像比较麻烦,所以我就把文章表(TNews)和文章内容(TNewsContent)表搞成一对一关联,
下面是TNewsContent文件的配置文件和代码:
[code="java"]
<?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">
schema="dbo" catalog="hldb" lazy="true">
tnews
class="com.bhsc.entity.TNews"
constrained="true"/>
[/code]
[code="java"]
package com.bhsc.entity;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
@SuppressWarnings("serial")
public class TNewsContent implements java.io.Serializable {
// Fields
private Integer newsId;
private TNews tnews;
private String newsContent;
// Constructors
/** default constructor */
public TNewsContent() {
}
public TNews getTnews() {
return tnews;
}
public void setTnews(TNews tnews) {
this.tnews = tnews;
}
public String getNewsContent() {
return this.newsContent;
}
public void setNewsContent(String newsContent) {
this.newsContent = newsContent;
}
public boolean equals(Object o) {
if (!(o instanceof TNewsContent)){
return false;
}
TNewsContent tnewsContent = (TNewsContent) o;
return new EqualsBuilder().appendSuper(super.equals(o))
.append(this.newsContent, tnewsContent.getNewsContent()) .isEquals();
}
public int hashCode() {
return new HashCodeBuilder(17, 37). append(newsContent).
toHashCode();
}
public Integer getNewsId() {
return newsId;
}
public void setNewsId(Integer newsId) {
this.newsId = newsId;
}
}
[/code]
下面是TNews文件的配置文件和代码(我把其他无关的属性都去掉了):
[code="java"]package com.bhsc.entity;
/**
- TNews entity.
- @author MyEclipse Persistence Tools */
@SuppressWarnings("serial")
public class TNews implements java.io.Serializable {
// Fields
private Integer newsId;
private TNewsContent tnewsContent;
// Constructors
/** default constructor */
public TNews() {
}
/** full constructor */
public TNews(TNewsContent tnewsContent){
this.tnewsContent=tnewsContent;
}
// Property accessors
public Integer getNewsId() {
return this.newsId;
}
public void setNewsId(Integer newsId) {
this.newsId = newsId;
}
public TNewsContent getTnewsContent() {
return tnewsContent;
}
public void setTnewsContent(TNewsContent tnewsContent) {
this.tnewsContent = tnewsContent;
}
}[/code]
[code="java"]
<?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">
cascade="all" constrained="true" />
</class>
[/code]
下面是保存文章的代码:
[code="java"]
TNews tnew=new TNews();
TNewsContent tnewsContent=new TNewsContent();
tnewsContent.setNewsContent(jo.getString("newsContent"));
tnewsContent.setTnews(tnew);//不能省,不然content将不知道从何处取得键值
tnew.setTnewsContent(tnewsContent);
newsDAO.makePersitent(tnew);
return tnew.getNewsId();
[/code]
按理说,我已经配置了TNewsContent的主键获取策略为foreign,并且在保存的时候也加上了这句代码:
[code="java"]tnewsContent.setTnews(tnew);[/code]
那么保存的时候就应该能获得到键值了,可是却出现了下面的错误:
[code="java"]
2008-07-10 12:09:36,906 null id generated for:class com.bhsc.entity.TNewsContent; nested exception is org.hibernate.id.IdentifierGenerationException: null id generated for:class com.bhsc.entity.TNewsContent
2008-07-10 12:09:36,953 ERROR (org.springframework.transaction.interceptor.TransactionInterceptor:367) - Application exception overridden by commit exception
[/code]
[code="java"]null id generated for:class com.bhsc.entity.TNewsContent[/code]居然使这个原因,大概的意思就是TNewsContent没有主键生成把,可是我检查了好久始终不知道错在哪里?大家帮我看看吧
PS:
问题应该出在配置上,我把一对一关联去了,文章是可以保存的,可是检查不出来啊,郁闷
[b]问题补充:[/b]
首先谢谢XMLDB,可是我去掉一对一关联,单独的tnew是可以保存的,而且我发现级联删除不行的,只会删除掉tnew,不会删除tnewsContent,这个应该是配置问题吧,我说的对吗?