在我的struts操作中,我从数据库中检索一个对象X,获取对它包含的一对多集合的引用,向该集合添加一些对象并保存原始对象X. 问题是我没有抛出任何错误但是一对多集合引用的表没有添加任何新行。
继承我的行动代码......
public String saveSelectedDefinitions()
{
WordT wordT = wordDao.get(2);
Set<DefinitionT> storedDefs = wordT.getDefinitionTs();
for( int i=0; i<3; i++)
{
DefinitionT selectedDef = new DefinitionT();
selectedDef.setValue("abc");
selectedDef.setWordT(wordT);
storedDefs.add(selectedDef);
}
wordT.setDefinitionTs(storedDefs);
wordDao.save(wordT);
return SUCCESS;
}
继承我的WordT课程
@Entity
@Table(name = "word_t", schema = "public")
public class WordT implements java.io.Serializable {
private int id;
private Set<DefinitionT> definitionTs = new HashSet<DefinitionT>(0);
public WordT() {
}
public WordT(int id) {
this.id = id;
}
public WordT(int id, Set<DefinitionT> definitionTs) {
this.id = id;
this.definitionTs = definitionTs;
}
@Id
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "wordT")
public Set<DefinitionT> getDefinitionTs() {
return this.definitionTs;
}
public void setDefinitionTs(Set<DefinitionT> definitionTs) {
this.definitionTs = definitionTs;
}
}
并且继承我的DefinitionT类
@Entity
@Table(name = "definition_t", schema = "public")
public class DefinitionT implements java.io.Serializable {
private int id;
private WordT wordT;
public DefinitionT() {
}
public DefinitionT(int id, WordT wordT) {
this.id = id;
this.wordT = wordT;
}
public DefinitionT(int id, WordT wordT, String value, int typeId,
Boolean selected) {
this.id = id;
this.wordT = wordT;
}
@Id
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "word_id", nullable = false)
public WordT getWordT() {
return this.wordT;
}
public void setWordT(WordT wordT) {
this.wordT = wordT;
}
}
我使用Hibernate工具生成我的Daos和Pojos 如果我使用DefinitionDao来持久化一个definitionT实例,它会保持良好状态,因此表的完整性是可以的。为什么它不能保存集合而不给我任何错误,为什么它没有这样做?
答案 0 :(得分:1)
Yo在OneToMany关联上没有级联,因此您可能对单词实体执行的任何操作都不会级联到其定义。此外,由于定义实体是关联的所有者,因此您需要为Hibernate设置其word属性以保持关联。