在我的 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 实例,它会很好地持久化,因此表完整性是可以的。为什么它不保存集合,也不给我任何关于它为什么没有这样做的错误?