简体   繁体   English

JPA标准ManyToMany加入还是不加入?

[英]JPA Criteria ManyToMany Join or No Join?

i have 2 Entities with the following Metamodels. 我有2个实体与以下Metamodels。

@StaticMetamodel(SearchIn.class)
public class SearchIn_ {
    public static volatile SingularAttribute<SearchIn, Integer> id;
    public static volatile SingularAttribute<SearchIn, String> searchString;
    public static volatile SetAttribute<SearchIn, Take> takes;
    // Other Attributes...
}

@StaticMetamodel(Take.class)
public class Take_ {
    public static volatile SingularAttribute<Take, Integer> id;
    // Other Attributes...
}

The relationship is a ManyToMany mapped by SearchIn.Class and Take.Class has no referene to SearchIn.Class. 该关系是由SearchIn.Class映射的ManyToMany,而Take.Class没有指向SearchIn.Class。

What i need now is all Takes that are mapped to a SearchIn with a specific searchString. 我现在需要的是使用特定searchString映射到SearchIn的所有Takes。 I want to use the criteria api but i can't figure out which entitity i need as Root and how to do the joins with that. 我想使用标准api,但我无法确定我需要哪个权限作为Root以及如何与其进行联接。 I saw some other questions that are similar but not realy what i want and i dont get it to work :/ 我看到了其他一些类似的问题,但不是真正的我想要的东西,我不能让它工作:/

So can somone give me a Hint and help me to build this up? 所以,somone可以给我一个提示并帮助我构建它吗?

Why don't you try a simple join: 你为什么不尝试一个简单的连接:

Root<SearchIn> root = criteriaQuery.from(SearchIn.class);
Join<SearchIn, Take> takeJoin = root.join(SearchIn_.takes);
criteriaQuery.where(builder.equal(root.get(SearchIn_.searchString), "bla"));


TypedQuery<SearchIn> typedQuery = entityManager.createQuery(criteriaQuery);
return typedQuery.getResultList();

your return value will be a List of SearchIns. 您的返回值将是SearchIns列表。 and you just call the getet for your Takes. 你只需要为你的Takes打电话给getet。 I haven't tried if it's running. 如果它正在运行我还没试过。 I just copied it from some of my code snippets. 我只是从我的一些代码片段中复制了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM