[英]Nested Collection Using Hibernate Criteria
I have 2 table Tab1
and Tab2
. 我有2个表
Tab1
和Tab2
。
Mapping of " Tab1
" to " Tab2
" is one-to-many
. “
Tab1
”到“ Tab2
”的映射是one-to-many
。
Code is like that : 代码是这样的:
private Collection< Tab2 > obj = new ArrayList< Tab2 >();
@javax.persistence.OneToMany(fetch=javax.persistence.FetchType.LAZY,
mappedBy = "Tab1")
public Collection< Tab2 > getTab2()
{
return Tab2;
}
public void setTab2( Collection<Tab2 > val )
{
this.Tab2 = val;
}
I want to fetch record from both table. 我想从两个表中获取记录。
For example : 例如 :
Tab1
has column: Tab1
有列:
Stu_Id, Stu_FirstName, Stu_LastName
Tab2
has column: Tab2
有列:
Stu_Id(foriegn Key),Stu_Subject, Stu_Teacher
Now I want to fetch Stu_id
from Tab1
and Stu_Subject
, Stu_Teacher
from Tab2
现在我想取
Stu_id
从Tab1
和Stu_Subject
, Stu_Teacher
从Tab2
You have different possibilities to solve your problem. 您有不同的可能性来解决您的问题。 Either you call a named query or you build your typed query with the
CriteriaBuilder
您可以调用命名查询,也可以使用
CriteriaBuilder
构建类型化查询
There are already lots of other questions dealing with OneToMany
-relations with either NamedQuery
or CriteriaBuilder
. 关于使用
NamedQuery
或CriteriaBuilder
OneToMany
关系,已经有很多其他问题。 It now depends on what you prefer. 现在取决于你喜欢什么。 I personally prefer
CriteriaBuilder
due to the JPA-Metamodel you can use and the refactoring convenience it gives. 我个人更喜欢
CriteriaBuilder
因为你可以使用JPA-Metamodel以及它提供的重构方便性。 (even though I wrote my own Wrapper to the CriteriaBuilder
to get a better SQL
-look-and-feel ;-)) (尽管我将自己的Wrapper写入
CriteriaBuilder
以获得更好的SQL
-look-and-feel ;-))
Example for NamedQuery
: Construct JPA query for a OneToMany relation NamedQuery
示例: 为OneToMany关系构造JPA查询
Example for TypedQuery
: JPA CriteriaQuery OneToMany TypedQuery
示例: JPA CriteriaQuery OneToMany
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.