JPA同级父/子,检索问题
我在检索“文件夹层次结构”类型的父/子关系时遇到一个问题 理想情况下,在普通 Oracle SQL 中,使用 CONNECT BY 子句会很容易。但对于 Hibernate,由于某种原因,它最终会出现双“左外连接”父对子和子对父的情况。
问题:当休眠拉取“Dashboard”文件夹的对象时,会以“Cartersian Product”的形式检索到,它确实会拉出父/子文件夹,但随后它还会将相同的子文件夹附加到Dashboard!。
这里有更多信息,我仅在以下代码片段中键入了关系。 表格:
DASHBOARD {
ID INTEGER PRIMARY KEY,
NAME VARCHAR2(100) ...
}
FOLDER {
ID INTEGER PRIMARY KEY,
DASHBOARD_ID INTEGER,
NAME VARCHAR2(100),
PARENT_FOLDER_ID INTEGER .....
}
public class DashBoard{
....
....
@OneToMany(mappedBy = "dashboard", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@OrderBy("NAME ASC")
@Sort(type = SortType.NATURAL)
public SortedSet<Folder> getFolders() {
return folders;
}
}
public class Folder{
....
....
@ManyToOne(fetch = FetchType.EAGER, optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "DASHBOARD_ID", nullable = false)
public DashBoard getDashBoard() {
return dashboard;
}
@OneToMany(mappedBy = "parent", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@OrderBy("NAME ASC")
@Sort(type = SortType.NATURAL)
public SortedSet<Folder> getChildFolders() {
return folders;
}
@ManyToOne(fetch = FetchType.EAGER, optional = true, cascade = CascadeType.ALL)
@JoinColumn(name = "PARENT_FOLDER_ID")
public Criterion getParent() {
return parent;
}
}
I have one issue with retrieval Parent/Child relationship of type "Folder Hierarchy" Ideally, in plain Oracle SQL, it would be easy to use CONNECT BY clause. But with Hibernate, some reason it ends up with Double "LEFT OUTER JOIN" Parent to Child and Child to Parent.
Problem: When hibernate pulls Object of "Dashboard" Folder are retrieved in "Cartersian product" Meaning, it does pulls out parent/child, but then it also attaches same child folders to Dashboard!.
Here is more information, I have ONLY typed relationships into following code fragment.
Tables:
DASHBOARD {
ID INTEGER PRIMARY KEY,
NAME VARCHAR2(100) ...
}
FOLDER {
ID INTEGER PRIMARY KEY,
DASHBOARD_ID INTEGER,
NAME VARCHAR2(100),
PARENT_FOLDER_ID INTEGER .....
}
public class DashBoard{
....
....
@OneToMany(mappedBy = "dashboard", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@OrderBy("NAME ASC")
@Sort(type = SortType.NATURAL)
public SortedSet<Folder> getFolders() {
return folders;
}
}
public class Folder{
....
....
@ManyToOne(fetch = FetchType.EAGER, optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "DASHBOARD_ID", nullable = false)
public DashBoard getDashBoard() {
return dashboard;
}
@OneToMany(mappedBy = "parent", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@OrderBy("NAME ASC")
@Sort(type = SortType.NATURAL)
public SortedSet<Folder> getChildFolders() {
return folders;
}
@ManyToOne(fetch = FetchType.EAGER, optional = true, cascade = CascadeType.ALL)
@JoinColumn(name = "PARENT_FOLDER_ID")
public Criterion getParent() {
return parent;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经放弃了关系 DashBoard -->文件夹,并由DAO层手动维护。这解决了休眠笛卡儿连接问题。我仍然认为可能有比这更好的解决方案,请告诉我。
谢谢,
I have dropped the relationship DashBoard --> Folder, and maintain it manually by DAO layer. This solves hibernate cartisian join issue. I would still think that there might be better solution than this, please let me know.
Thanks,