我正在使用mongodb,jpa和hibernate ogm,我遇到了查询问题。 我有两个实体:User和Entitlement,它们之间具有单向ManyToMany关联,因此Entitlement包含User列表。 我正在尝试获取其用户字段包含特定用户的所有权利。
@Entity
public class User {
@Id
private String id;
private String name;
private String surname;
//getters and setters...
}
@Entity
public class Entitlement {
@Id
private String id;
@ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST,
CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH })
private Set<User> users = new HashSet<User>();
private String name;
//getters and setters...
}
我将hibernate配置为将关联信息存储在每个关联的专用文档中。以下是persistence.xml文件
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="org.hibernate.ogm.tutorial.jpa" transaction-type="JTA">
<!-- Use Hibernate OGM provider: configuration will be transparent -->
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<class>com.example.mongodb.model.User</class>
<class>com.example.mongodb.model.Entitlement</class>
<properties>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" />
<property name="hibernate.ogm.datastore.provider" value="mongodb" />
<property name="hibernate.ogm.datastore.host" value="localhost"/>
<property name="hibernate.ogm.datastore.database" value="mydb"/>
<property name="hibernate.ogm.datastore.create_database" value="true"/>
<property name="hibernate.ogm.datastore.document.association_storage" value="ASSOCIATION_DOCUMENT"/>
</properties>
</persistence-unit>
我的目标是获取“用户”字段包含特定用户的所有权利。 我尝试了以下查询:
此查询返回db中存在的所有权利,不带任何过滤器
User u = em.find(User.class, id);//id is the user id that I use to find the entitlements
List<Entitlement> list = em
.createQuery(
"select e from Entitlement e fetch all properties where :user MEMBER OF e.users",
Entitlement.class).setParameter("user", u)
.getResultList();
return list;
2
User u = em.find(User.class, id);
HashSet<User> userSet = new HashSet<User>();
userSet.add(u);
List<Entitlement> list = em
.createQuery(
"select e from Entitlement e fetch all properties where e.users In (:user)",
Entitlement.class).setParameter("user", userSet)
.getResultList();
return list;
使用此查询,我收到以下错误:
java.lang.UnsupportedOperationException:无法对集合执行事后查询完成查找。
3
List<Entitlement> list = em
.createQuery(
"select e from Entitlement e inner join fetch e.users u where u.id IN (:id) ",
Entitlement.class)
.setParameter("id", Arrays.asList(id)).getResultList();
return list;
此查询产生此错误:
java.lang.UnsupportedOperationException:无法识别的属性类型:org.hibernate.type.SetType(com.example.mongodb.model.Entitlement.users)
这些都不起作用。