-->
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.  [ 8 posts ] 
Author Message
 Post subject: Criteria query with collection of values (Set<String>)
PostPosted: Wed Feb 04, 2009 10:20 am 
Newbie

Joined: Wed Feb 04, 2009 9:13 am
Posts: 4
Hello all,

I am quite new with Hibernate, I have really tried to look for information on the following issue in documentation and books, but I could not find any definitive information regarding this... So if any of you could help, it would really be appreciated !

Problem: I cannot manage to perform a criteria based query on a simple collection (Set<String>)

Hibernate version: 3.2.6.ga
Database version: MySQL 5.0.67

I have this simple (simplified for the forum of course) Member class :
Code:
public class Member {
  private Long rowId;
  private Set<String> gids = new HashSet<String>();
 
  public Long getRowId() { return rowId; }
  public void setRowId(Long rowId) { this.rowId = rowId; }

  public Set<String> getGids() {
    return gids;
  }

  public void setGids(Set<String> gids) {
    this.gids = gids;
  }
}


and its hbm file
Code:
  <class name="org.company.project.Member" table="MEMBER">

      <id name="rowId" column="rowId">
        <generator class="native"/>
      </id>

      <set name="gids" table="MEMBER_GIDS">
        <key column="rowId"/>
        <element type="string" column="gid" length="64" not-null="true"/>
      </set>

  </class>


My first attempts following example of the book "Java Persistence with Hibernate" were as follows :

Test 1 :
Code:
List<Member> mbrList = session.createCriteria(Member.class)
                               .add(Restrictions.eq("gids.gid", "1234"));


Throws an exception :
Code:
org.hibernate.QueryException: could not resolve property: gids.gid of: com.jalios.jcms.dbmember.DBMember
   at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:44)


which is quite normal since gids is not a Set of hibernate entity, just a simple Set of String...

Test 2 :
Code:
List<Member> mbrList = session.createCriteria(Member.class)
                               .createCriteria("gids").add(Restrictions.eq("gid", "1234"))


throws this exception :

Code:
org.hibernate.MappingException: collection was not an association: com.jalios.jcms.dbmember.DBMember.gids
   at org.hibernate.type.CollectionType.getAssociatedEntityName(CollectionType.java:447)


indicating that the collection is not an association, but I could not add a <one-to-many> relationship in the hbm for a set of String...


I have found these two posts which I think are related to my question, but they seem to indicate this is not possible... :


Could someone please tell me if this is possible with Hibernate, and if so, help me find the appropriate configuration/querycode to use ?
If this is not possible, what workaround can i use ?

Thanks for your help !


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 4:38 am 
Newbie

Joined: Wed Feb 04, 2009 9:13 am
Posts: 4
Does anybody have an idea on this ?

I could understand if collections of String were not supported for Criteria query, but a definitive answer would be nice...
Also maybe it should be added to the documentation...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 6:27 am 
Newbie

Joined: Mon Jul 02, 2007 6:16 am
Posts: 3
Hi jaquemet,

did you find a solution/answer for this question?

thanks
Becay


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 1:37 pm 
Newbie

Joined: Wed Feb 04, 2009 9:13 am
Posts: 4
Hi Becay,

After some research, we found there is no solution using the simple Criteria API.
Its a known limitation in Hibernate :
http://opensource.atlassian.com/project ... se/HHH-869
I highly suggest you to vote for this improvement !

You can workaround this problem using SQL restrictions (provided you know your fields and table name).
Code:
Restrictions.sqlRestriction(String sql, ...)

http://www.hibernate.org/hib_docs/v3/ap ... tions.html

I don't have the code available right now, I can give you an example later if you want.


Olivier


Top
 Profile  
 
 Post subject: Re: Criteria query with collection of values (Set<String>)
PostPosted: Mon Oct 19, 2009 4:52 pm 
Newbie

Joined: Mon Oct 19, 2009 4:47 pm
Posts: 2
Hi Jaquemet,

do you still have the solution for this problem? Can i take a look at the code?

thanks,
ferry


Top
 Profile  
 
 Post subject: Re: Criteria query with collection of values (Set<String>)
PostPosted: Tue Oct 20, 2009 3:19 am 
Newbie

Joined: Wed Feb 04, 2009 9:13 am
Posts: 4
ferrylauda wrote:
do you still have the solution for this problem? Can i take a look at the code?


Hi Ferry, I'm sorry but I did not kept the code which uses Restrictions.sqlRestriction.

If I use the sample example above, it is probably something like this :
Code:
    crit.add(Restrictions.sqlRestriction("INNER JOIN MEMBER MEMBER_GIDS on MEMBER.gids = MEMBER_GIDS.rowId WHERE MEMBER_GIDS.gid= '1234';"));


Warning : I don't remember the real working solution, and I have not tested the code just above !
This is just a guess looking back at some of my previous notes.

Good luck.
Olivier


Top
 Profile  
 
 Post subject: Re: Criteria query with collection of values (Set<String>)
PostPosted: Tue Oct 20, 2009 5:57 pm 
Newbie

Joined: Mon Oct 19, 2009 4:47 pm
Posts: 2
Hi Olivier,

Thanks for your help, i'll try it.

ferry


Top
 Profile  
 
 Post subject: Re: Criteria query with collection of values (Set<String>)
PostPosted: Fri Jun 10, 2011 5:24 pm 
Newbie

Joined: Thu Jan 07, 2010 4:31 pm
Posts: 2
Did you solve this with the workaround above? Can you post the working workaround?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.