我有两个具有以下元模型的实体。
@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的所有Take。我想使用criteria api,但是我不知道我需要哪个实体作为Root,以及如何使用它进行连接。我看到了其他一些类似的问题,但并不是我真正想要的,我没有让它起作用:/
那么能不能有人给我点提示,帮我把它建起来?
发布于 2013-06-21 15:09:54
为什么不尝试一个简单的连接:
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列表。你只要给getet打电话就行了。如果它在运行,我还没有试过。我只是从我的一些代码片段中复制了它。
https://stackoverflow.com/questions/17063772
复制相似问题