Hello everyone,
I'm using mongodb, jpa and hibernate ogm and I have problems with a query.
I have two entities: User and Entitlement, with a unidirectional ManyToMany association between them, so an Entitlement contains a list of User.
I'm trying to get all entitlements whose users field contains a specific user.
Code:
@Entity
public class User {
@Id
private String id;
private String name;
private String surname;
//getters and setters...
}
Code:
@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...
}
I configured hibernate to store association information in a dedicated document per association. The following is the persistence.xml file
Code:
<?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>
</persistence>
My objective is to get all entitlements whose "users" field contains a specific user.
I tried the following queries:
1. This query returns all entitlements present in db, without any filters
Code:
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.
Code:
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;
With this query I get the following error:
Code:
java.lang.UnsupportedOperationException: cannot doAfterTransactionCompletion lookups on collections
3.
Code:
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;
This query produces this error:
Code:
java.lang.UnsupportedOperationException: Unrecognized property type: org.hibernate.type.SetType(com.example.mongodb.model.Entitlement.users)
None of these works.
Can someone suggest me how to write the query?
Thanks in advice.