繁体   English   中英

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

[英]JPA Criteria ManyToMany Join or No Join?

我有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...
}

该关系是由SearchIn.Class映射的ManyToMany,而Take.Class没有指向SearchIn.Class。

我现在需要的是使用特定searchString映射到SearchIn的所有Takes。 我想使用标准api,但我无法确定我需要哪个权限作为Root以及如何与其进行联接。 我看到了其他一些类似的问题,但不是真正的我想要的东西,我不能让它工作:/

所以,somone可以给我一个提示并帮助我构建它吗?

你为什么不尝试一个简单的连接:

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();

您的返回值将是SearchIns列表。 你只需要为你的Takes打电话给getet。 如果它正在运行我还没试过。 我只是从我的一些代码片段中复制了它。

暂无
暂无

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

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