我假设您使用的是实现JPA 2.0的Hibernate版本。这是一个JPA 2.0解决方案,可以与任何兼容的实现一起使用。
请
uuids使用JPA的
@ElementCollection注释进行注释。不要使用
@CollectionOfElements其他一些答案中提到的Hibernate
。后者具有等效的功能,但已被弃用。
Foobar.java大致如下所示:
@Entitypublic class Foobar implements Serializable { // You might have some other id @Id private Long id; @ElementCollection private List<String> uuids; // Getters/Setters, serialVersionUID, ...}这里是你如何建立一个
CriteriaQuery选择所有
Foobar(胡)的
uuids含有“ABC123”。
public void getFoobars() {{ EntityManager em = ... // EM by injection, EntityManagerFactory, whatever CriteriaBuilder b = em.getCriteriaBuilder(); CriteriaQuery<Foobar> cq = b.createQuery(Foobar.class); Root<Foobar> foobar = cq.from(Foobar.class); TypedQuery<Foobar> q = em.createQuery( cq.select(foobar) .where(b.isMember("abc123", foobar.<List<String>>get("uuids")))); for (Foobar f : q.getResultList()) { // Do stuff with f, which will have "abc123" in uuids }}我在玩这个游戏时编写了一个独立的概念验证程序。我现在不能将其推出。如果您想将POC推送到github,请发表评论。



