-->
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.  [ 6 posts ] 
Author Message
 Post subject: Queries on embedded/associated entities are not supported
PostPosted: Mon Sep 15, 2014 3:09 pm 
Newbie

Joined: Mon Sep 15, 2014 2:57 pm
Posts: 3
Hi,

I have been exploring Hibernate OGM and MongoDB. I was working on scenario given below (taken easy for better explanation).

As we know Employee and Department entities are associated by Many to One relationship. I am trying to query Employee entity whose Department title is 'X'.

The Problem is when i try to do that, I get the following error.

java.lang.UnsupportedOperationException: Queries on embedded/associated entities are not supported yet.
at org.hibernate.ogm.datastore.mongodb.query.parsing.impl.MongoDBPropertyHelper.convertToPropertyType(MongoDBPropertyHelper.java:37)
at org.hibernate.hql.ast.spi.SingleEntityQueryBuilder.addComparisonPredicate(SingleEntityQueryBuilder.java:83)
at org.hibernate.hql.ast.spi.SingleEntityQueryRendererDelegate.addComparisonPredicate(SingleEntityQueryRendererDelegate.java:231)
at org.hibernate.hql.ast.spi.SingleEntityQueryRendererDelegate.predicateEquals(SingleEntityQueryRendererDelegate.java:209)
at org.hibernate.hql.ast.render.QueryRenderer.predicate(QueryRenderer.java:5197)
at org.hibernate.hql.ast.render.QueryRenderer.searchCondition(QueryRenderer.java:4871)
at org.hibernate.hql.ast.render.QueryRenderer.whereClause(QueryRenderer.java:2347)
at org.hibernate.hql.ast.render.QueryRenderer.querySpec(QueryRenderer.java:2202)
at org.hibernate.hql.ast.render.QueryRenderer.queryExpression(QueryRenderer.java:2105)
at org.hibernate.hql.ast.render.QueryRenderer.queryStatement(QueryRenderer.java:1744)
at org.hibernate.hql.ast.render.QueryRenderer.queryStatementSet(QueryRenderer.java:1657)
at org.hibernate.hql.ast.render.QueryRenderer.statement(QueryRenderer.java:653)
at org.hibernate.hql.ast.spi.QueryRendererProcessor.process(QueryRendererProcessor.java:51)
at org.hibernate.hql.QueryParser.parseQuery(QueryParser.java:82)
at org.hibernate.ogm.datastore.mongodb.query.parsing.impl.MongoDBBasedQueryParserService.parseQuery(MongoDBBasedQueryParserService.java:40)
at org.hibernate.ogm.query.impl.OgmQueryTranslator.getQuery(OgmQueryTranslator.java:168)
at org.hibernate.ogm.query.impl.OgmQueryTranslator.getLoader(OgmQueryTranslator.java:130)
at org.hibernate.ogm.query.impl.OgmQueryTranslator.list(OgmQueryTranslator.java:125)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)


Top
 Profile  
 
 Post subject: Re: Queries on embedded/associated entities are not supported
PostPosted: Tue Sep 16, 2014 5:11 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi,
can you show us the query you are using and the entities classes?

Keep in mind that at the moment OGM does not support join queries and select on embedded for MongoDB .

Cheers,
Davide


Top
 Profile  
 
 Post subject: Re: Queries on embedded/associated entities are not supported
PostPosted: Tue Sep 16, 2014 1:55 pm 
Newbie

Joined: Mon Sep 15, 2014 2:57 pm
Posts: 3
Hi Davide,

Thanks for reply.

Below is the code snippet of what I am trying to do.

Code:
@Entity
public class Employee extends AbstractModel{

   private String name;
   private int age;
   private Department department;

   @Column(name="emp_name", nullable=false)
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   @Column(name="age", nullable=false)
   public int getAge() {
      return age;
   }

   public void setAge(int age) {
      this.age = age;
   }

   @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   @JoinColumn(name="dept_id", nullable=false)
   public Department getDepartment() {
      return department;
   }

   public void setDepartment(Department department) {
      this.department = department;
   }
   
}


Code:
@Entity
public class Department extends AbstractModel {

   private String name;
      
   @Column(name="dept_name", nullable=false)
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
   
}


And the JUnit Test Class:
Code:
public class EmployeTest {

   private EntityManager getEntityManager() {
      
      EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(ApplicationConstants.PERSISTENCE_UNIT_NAME);
      return entityManagerFactory.createEntityManager();
   }

   
   @Test
   public void findEmployee() {
      
      StringBuilder queryString = new StringBuilder();
      queryString.append("select e from Employee e where e.department.name=:name");
      
      EntityManager entityManager = getEntityManager();
      Query query = entityManager.createQuery(queryString.toString());
      query.setParameter("name", "x");
      
      System.out.println(query.getResultList().size());
   }
}


I have removed the unnecessary code from classes and kept just the important one.

When i run the test case, I get the error like :

Code:
java.lang.UnsupportedOperationException: Queries on embedded/associated entities are not supported yet.
   at org.hibernate.ogm.datastore.mongodb.query.parsing.impl.MongoDBPropertyHelper.convertToPropertyType(MongoDBPropertyHelper.java:37)
   at org.hibernate.hql.ast.spi.SingleEntityQueryBuilder.addComparisonPredicate(SingleEntityQueryBuilder.java:83)
   at org.hibernate.hql.ast.spi.SingleEntityQueryRendererDelegate.addComparisonPredicate(SingleEntityQueryRendererDelegate.java:231)
   at org.hibernate.hql.ast.spi.SingleEntityQueryRendererDelegate.predicateEquals(SingleEntityQueryRendererDelegate.java:209)
   at org.hibernate.hql.ast.render.QueryRenderer.predicate(QueryRenderer.java:5197)
   at org.hibernate.hql.ast.render.QueryRenderer.searchCondition(QueryRenderer.java:4871)
   at org.hibernate.hql.ast.render.QueryRenderer.whereClause(QueryRenderer.java:2347)
   at org.hibernate.hql.ast.render.QueryRenderer.querySpec(QueryRenderer.java:2202)
   at org.hibernate.hql.ast.render.QueryRenderer.queryExpression(QueryRenderer.java:2105)
   at org.hibernate.hql.ast.render.QueryRenderer.queryStatement(QueryRenderer.java:1744)
   at org.hibernate.hql.ast.render.QueryRenderer.queryStatementSet(QueryRenderer.java:1657)
   at org.hibernate.hql.ast.render.QueryRenderer.statement(QueryRenderer.java:653)
   at org.hibernate.hql.ast.spi.QueryRendererProcessor.process(QueryRendererProcessor.java:51)
   at org.hibernate.hql.QueryParser.parseQuery(QueryParser.java:82)
   at org.hibernate.ogm.datastore.mongodb.query.parsing.impl.MongoDBBasedQueryParserService.parseQuery(MongoDBBasedQueryParserService.java:40)
   at org.hibernate.ogm.query.impl.OgmQueryTranslator.getQuery(OgmQueryTranslator.java:168)
   at org.hibernate.ogm.query.impl.OgmQueryTranslator.getLoader(OgmQueryTranslator.java:130)
   at org.hibernate.ogm.query.impl.OgmQueryTranslator.list(OgmQueryTranslator.java:125)
   at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
   at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
   at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
   at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
   at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
   at test.EmployeTest.findEmployee(EmployeTest.java:62)


As you can see, I have two entities and they are coupled with Many to One relationship.

Kindly help if i need to correct my data model structure.

Thanks ..


Top
 Profile  
 
 Post subject: Re: Queries on embedded/associated entities are not supported
PostPosted: Wed Sep 17, 2014 8:34 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

As Davide is saying, queries spanning several entities are not supported atm. MongoDB doesn't support joins natively, so this sort of query is not easily be mapped.

We may consider to add some clever routine in the future which takes the incoming JP-QL query and breaks it down into several queries executed against the store (here one to find the department by title, then another one to find the employees of these departments). But that's not trivial and also has implications on query performance.

To solve your problem, you may either break it down into two queries yourself or you redundantly save the department name within the employee documents, allowing to query for it with a single query. But of course you then need to make sure that the names stay in sync. OGM may help with this sort of de-normalization in the future as well, but we're not there yet.

Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Queries on embedded/associated entities are not supported
PostPosted: Sat Oct 04, 2014 11:14 am 
Newbie

Joined: Mon Sep 15, 2014 2:57 pm
Posts: 3
Hi Gunnar,

Thank you for suggestion.

I have found another problem performing select operation using JPQL. Let's take the same entities.

Query is like,
Code:
StringBuilder queryString = new StringBuilder();
queryString.append("select dept from Department dept ");
queryString.append("where UPPER(dept.name) = :name");
      
Query query = entityManager.createQuery(queryString.toString());
query.setParameter("name", "computer".toUpperCase());
System.out.println(query.getResultList().size());


Though my mongo database the records for department name 'computer', I am getting no results. I get '0' printed on console.

JPQL functions also are yet not supported ?


Top
 Profile  
 
 Post subject: Re: Queries on embedded/associated entities are not supported
PostPosted: Wed Oct 08, 2014 6:23 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

Right, JP-QL functions are not supported yet. You may give native queries a try in your case (see https://docs.jboss.org/hibernate/ogm/4.1/reference/en-US/html_single/#ogm-mongodb-queries). Note that this also is experimental at this point. If you have any input or requirements around that let us know.

Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.