我有一个场景,我需要在一个案例中加载所有子值,在另一个案例中加载一些特定的子值。我在两种情况下都使用单个bean,并使用命名查询编写查询。
@namedqueries{
@namedQuery(name="query1") = "select parent from Parent parent",
@namedQuery(name="query2") = "select parent from Parent parent",
}
Class Parent {
@manytomany
@join mentioned my join condition here //
List<Child> child ;
}
Class Child
{
String A;
String B;
String C;
@manytomany(mappedby = "child")
List<parent> parent ;
}
现在在我的查询2中我只需要加载字符串A而不是字符串B和字符串C. 我尝试使用
"select parent.child .A from Parent parent" as Query 2
但得到以下错误
"Attempting to navigate to relation field via multi-valued association and
jpql doesnt allow traversal through multi valued relationship. Try join instead"
所以关于如何继续这个的任何建议..
1) Should I have to create a new bean for each Query
2) Or Can we control the child object parameters in specific named queries
答案 0 :(得分:3)
当您说select parent.child
时,您正在访问某个集合,而根据HQL标准,您无法说select parent.child.A
这是错误的。您需要使用join来执行此操作,因为错误消息建议:
select c.A from parent as p join p.child as c
然后不,你不必为每个查询创建一个新的bean。