-->
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.  [ 5 posts ] 
Author Message
 Post subject: CollectionOfElements and Criterias
PostPosted: Fri Oct 27, 2006 6:10 am 
Newbie

Joined: Thu Jun 29, 2006 5:43 am
Posts: 5
Hibernate version: 3.2.0

Hi,

I would like to know, how to create a criteria including elements of a collection, eg:

Code:
@Entity
class User {

    private List<Long> _myElements;

    [b]@CollectionOfElements[/b]
    public List<Long> getMyElements(){
        return _myElements;
    }

}


I thought something like that:

Code:
DetachedCriteria criteria = DetachedCriteria.forClass(DBUser.class);
criteria.createAlias("myElements", "e")
criteria.add(Property.forName("e.<???>").eq(1L));


Is this possible?
What do I have to write for <???>. Or is there a different approach?

Thank you very much.

Stefan[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 01, 2006 4:28 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
What property of Long are you trying to query?
Look at the Restrictions class.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 1:19 pm 
Newbie

Joined: Thu Jun 29, 2006 5:43 am
Posts: 5
Hi,
First, thank you for your reply, but I really don't understand what you are asking me :-)

I have a collection of Long, the Object Long, the wrapper around the basic datatype long. So, i want to query the value of the Long ;-)

I have tried several ways, but it does not work:

When I want to create an alias like this :

Code:
DetachedCriteria criteria = DetachedCriteria.forClass(DBUser.class);
criteria.createAlias("myElements", "collection")
criteria.add(Property.forName("collection").eq(1L));


OR

Code:
DetachedCriteria criteria = DetachedCriteria.forClass(DBUser.class);
criteria.createAlias("myElements", "e")
criteria.add(Property.forName("e.element").eq(1L));


I get following exception:

Code:
    [junit] Caused by: org.hibernate.MappingException: collection was not an association: com.freiheit.ark.user.model.persistence.DBUser.myElements
    [junit]     at org.hibernate.type.CollectionType.getAssociatedEntityName(CollectionType.java:385)
    [junit]     at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPathEntityName(CriteriaQueryTranslator.java:207)
    [junit]     at org.hibernate.loader.criteria.CriteriaQueryTranslator.createCriteriaEntityNameMap(CriteriaQueryTranslator.java:191)
    [junit]     at org.hibernate.loader.criteria.CriteriaQueryTranslator.<init>(CriteriaQueryTranslator.java:81)
    [junit]     at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:59)
    [junit]     at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
    [junit]     at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)


But it is a collection and so why is it not an association?

OK, so I tried the diffrent way, don't create an alias (I know, the alias should make the join to the collection):

Code:
DetachedCriteria criteria = DetachedCriteria.forClass(DBUser.class);
criteria.add(Property.forName("myElements").eq(1L));


Of course, this will produce an error:

Code:
    [junit] Caused by: org.postgresql.util.PSQLException: Für den Parameter 1 wurde kein Wert angegeben.
    [junit]     at org.postgresql.core.v3.SimpleParameterList.checkAllParametersSet(SimpleParameterList.java:134)
    [junit]     at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:179)
    [junit]     at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:437)
    [junit]     at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:353)
    [junit]     at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:257)
    [junit]     at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:92)
    [junit]     at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
    [junit]     at org.hibernate.loader.Loader.getResultSet(Loader.java:1668)
    [junit]     at org.hibernate.loader.Loader.doQuery(Loader.java:662)
    [junit]     at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
    [junit]     at org.hibernate.loader.Loader.doList(Loader.java:2144)
    [junit]     ... 37 more


SO, once again, is it possible to query special elements in a collectionOfElements? And if yes,... how?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 7:39 pm 
Newbie

Joined: Mon Oct 25, 2004 9:18 am
Posts: 7
I would also like to know how to do this. It looks like the Criteria API doesn't allow you to query collections of elements.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 8:04 pm 
Newbie

Joined: Mon Oct 25, 2004 9:18 am
Posts: 7
Ah right. A bit more digging and I see this is an open issue HHH-869 with Hibernate. Back to HQL then.


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