-->
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.  [ 3 posts ] 
Author Message
 Post subject: how to query ElementCollection of String (Hibernate OGM 4.2)
PostPosted: Thu Aug 27, 2015 4:15 pm 
Newbie

Joined: Thu Aug 27, 2015 3:40 pm
Posts: 2
I have this Users class
Code:
@Entity
@Table(name = "Users")
public class Users implements  Serializable {

    private static final long serialVersionUID = -5638228567536191397L;

    @Id
    @Column(name = "_id")
    private ObjectId _id;
    private String name;

    @ElementCollection
    private List<String> mail;

//getter, setter ...
}


following is a json from mongodb Users collection.
where mail is a field of string array
Code:
{
    "_id" : ObjectId("55c59b30a5c2073240ffc274"),
    "name" : "john doe"
    "mail" : ["abc@blah.com"]
}


I want a very simple query which finds a user whose mail is 'abc@blah.com'

Following query returns me a null result, I'm not sure what I'm missing here.
Code:
TypedQuery<Users> query = em.createQuery("SELECT u FROM  Users u JOIN u.mail m WHERE m ='abc@blah.com' ", Users.class)
List<Users> users = query.getResultList();


I also tried with adding another embedded object but same result

helpful links:
http://in.relation.to/2015/03/17/hibernate-ogm-times-2413-and-42-beta-1-released/#H-QueryImprovements
http://antoniogoncalves.org/2009/11/01/mapping-and-querying-a-list-of-primitive-types-with-jpa-2-0

Any help would be appreciated

Thanks


Top
 Profile  
 
 Post subject: Re: how to query ElementCollection of String (Hibernate OGM 4.2)
PostPosted: Fri Aug 28, 2015 3:58 am 
Hibernate Team
Hibernate Team

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

Your query is good, it's a bug on our side. I've filed https://hibernate.atlassian.net/browse/OGM-913 for this. Apparently the JOIN syntax for querying element collections currently only works with collections of component types (@Embeddable) but not with collections of primitive types, String etc.

As a workaround, you may either model an @Embeddable representing an e-mail (with a single property for the address) or you refrain to native queries for now (see http://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html_single/#ogm-mongodb-queries-native).

Cheers,

--Gunnar

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


Top
 Profile  
 
 Post subject: Re: how to query ElementCollection of String (Hibernate OGM 4.2)
PostPosted: Fri Aug 28, 2015 10:35 am 
Newbie

Joined: Thu Aug 27, 2015 3:40 pm
Posts: 2
Thanks Gunnar for your suggestion.

I already did a successful POC of embeddable object with single property when I was looking workaround for this problem.
The thing is we are consuming mongodb collection from third party source and we dont have a control over it.

As a final alternate solution I implement a native query that works for me.

Here is the solution for querying primitive types collection with native query.

Code:
        String strQuery = "{ $query : { 'mail' : 'abc@blah.com'}}";
        Query query = em.createNativeQuery(strQuery, Users.class);
        List<Users> users = query.getResultList();


Chares!
Naveed


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