我编写了以下对象:
@Entity
public class Report implements Serializable {
@Id
private String url;
@LazyCollection(LazyCollectionOption.FALSE)
@ElementCollection()
private List<ArrayList<KeyPhrase>> keywordReports = new ArrayList<>();
@LazyCollection(LazyCollectionOption.FALSE)
@ElementCollection()
private List<KeyPhrase> allKeyPhrases = new ArrayList<>();
@LazyCollection(LazyCollectionOption.FALSE)
@ElementCollection()
private List<KeyPhrase> allOtherPhrases = new ArrayList<>();
@LazyCollection(LazyCollectionOption.FALSE)
@ElementCollection()
private List<KeyPhrase> topGroup = new ArrayList<>();
public Report(String url, List<ArrayList<KeyPhrase>> keywordReports, List<KeyPhrase> allKeyPhrases,
List<KeyPhrase> allOtherPhrases, List<KeyPhrase> topGroup) {
this.url = url;
this.keywordReports = keywordReports;
this.allKeyPhrases = allKeyPhrases;
this.allOtherPhrases = allOtherPhrases;
this.topGroup = topGroup;
}
}
当我尝试通过存储库保存新报告时,收到以下错误消息:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing:
我读到错误消息指出可以通过添加 CASCADE.ALL 注释来解决的问题。但是,使用我正在使用的注释,我不知道在哪里指定级联。我尝试了几个注释,但编译器总是告诉我这是指定级联的非法位置。
我也未能成功尝试添加此注释:
"@Cascade(org.hibernate.annotations.CascadeType.ALL)"
仅供引用,我在程序中还有另一个实体,我已经在其中完成了此操作:
@LazyCollection(LazyCollectionOption.FALSE)
@ElementCollection()
private List<String> related = new ArrayList<>();
有趣的是,这个就像一个魅力。
任何人都可以给我提示如何解决级联问题,或者我的代码可能还有什么问题吗?
提前谢谢!
最佳答案
您必须指定您的报告和关键短语之间的关系。喜欢
1.@OneToOne
2.@OneToMany
3.@ManyToMany
执行此操作时,您可以指定级联属性。例如。
@OneToMany(cascade = {CascadeType.ALL})
private List<KeyPhrase> allKeyPhrases = new ArrayList<>();
关于java - 如何在 Hibernate Spring 中级联 CollectionElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47815143/