-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: JPQL - Query manytomany association (mongodb + jpa + hibern)
PostPosted: Fri Oct 13, 2017 9:09 am 
Newbie

Joined: Fri Oct 13, 2017 8:29 am
Posts: 1
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.


Top
 Profile  
 
 Post subject: Re: JPQL - Query manytomany association (mongodb + jpa + hibern)
PostPosted: Tue Oct 17, 2017 6:30 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi, currently, Hibernate OGM does not support HQL query on on one to many associations.

If you need to run a query in this situation your best bet is to use a native query.

Davide


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.