-->
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.  [ 11 posts ] 
Author Message
 Post subject: Filtering by child (many-to-many) relationship
PostPosted: Tue Sep 14, 2004 5:56 pm 
Newbie

Joined: Tue Sep 14, 2004 3:07 pm
Posts: 7
I am trying to filter a Enterprises Collection by segments (many-to-many relationship) but I get a Non-persistent collection Exception

My java code is:


objects=session.find(
"select Enterprise from Enterprise in class adama.supplier.Enterprise where deleted != ? order by name",
new Boolean(true),Hibernate.BOOLEAN);

Long seg=new Long(14);
Collection ents=hibernate.filter(objects, "where ? in this.segments", seg, Hibernate.LONG);
log.info("I found "+ents.size()+" enterpises with this segment");


Mapping documents:
<class
name="adama.supplier.Enterprise"
table="Enterprise"
>

<id
name="oid"
column="oid"
type="long"
>
<generator class="native">
</generator>
</id>

<property
name="name"
type="java.lang.String"
column="name"
/>


<property
name="deleted"
type="boolean"
column="deleted"
/>


<bag role="segments" table="SupplierSegmentEnterpriseXref">
<key column="enterprise"/>
<many-to-many column="segment" class="adama.supplier.SupplierSegment"/>
</bag>

</class>




<class
name="adama.supplier.SupplierSegment"
table="SupplierSegment"
>

<id
name="oid"
column="oid"
type="long"
>
<generator class="native">
</generator>
</id>

<property
name="descr"
type="java.lang.String"
column="descr"
/>

<property
name="name"
type="java.lang.String"
column="name"
/>


<bag role="suppliers" table="SupplierSegmentEnterpriseXref">
<key column="segment"/>
<many-to-many column="enterprise" class="adama.supplier.Enterprise"/>
</bag>

</class>

Full stack trace of any exception that occurs:
SEVERE: Error filtering enterprises:cirrus.hibernate.TransientObjectException: Collection was not yet persistent

Name and version of the database you are using:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 6:08 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Hibernate.filter() works on persisted collections i.e. collections that are property of a persistent entity.

For example:

Code:
hibernate.filter(myEnterprise.segments, "where ? = this", seg, Hibernate.LONG);


Where myEnterprise is an Enterprise instance loaded previously...

This filtering applies to a collection of a single entity - which is not what you want. It seems you need something like:

Code:
Query query = session.createQuery("from Enterprise as e where e.deleted=:isDeleted and :mySegment in e.segments");
query.setParameter("isDeleted", Boolean.TRUE);
query.setParameter("mySegment", new Long(14));

List ents = query.list();


Top
 Profile  
 
 Post subject: Yes, that is what I need
PostPosted: Tue Sep 14, 2004 6:46 pm 
Newbie

Joined: Tue Sep 14, 2004 3:07 pm
Posts: 7
Yes. That is what I need, but it does not work.
I get a "SEVERE: Error filtering enterprises:cirrus.hibernate.QueryException: IN expected [from Enterprise as e where e.deleted=:isDeleted and :mySegment in e.segments]"

I don't know what is missing.... :(


Top
 Profile  
 
 Post subject: Re: Yes, that is what I need
PostPosted: Tue Sep 14, 2004 6:51 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
goldberg wrote:
Yes. That is what I need, but it does not work.
I get a "SEVERE: Error filtering enterprises:cirrus.hibernate.QueryException: IN expected [from Enterprise as e where e.deleted=:isDeleted and :mySegment in e.segments]"

I don't know what is missing.... :(


My mistake... Try this one:

Code:
Query query = session.createQuery("from Enterprise as e where e.deleted=:isDeleted and :mySegment in elements(e.segments)");


elements() keyword was missing to refer to the elements of the segments collection... Should be ok now (bit too late here - need some sleep ;)

Turn on SQL output and you will see the difference (log.4.logger.net.sf.hibernate.SQL=DEBUG)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 6:55 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Perhaps you could also do something like this:

Code:
select enterprise

from
          Enterprise as enterprise
left join enterprise.segments as segment

where
    enterprise.deleted = :isDeleted
and segment            = :mySegment


But then you may have have duplicates...


Top
 Profile  
 
 Post subject: Almost done...
PostPosted: Tue Sep 14, 2004 7:04 pm 
Newbie

Joined: Tue Sep 14, 2004 3:07 pm
Posts: 7
I try that again and get the same result:

SEVERE: Error filtering enterprises:cirrus.hibernate.QueryException: IN expected [from Enterprise as e where e.deleted!=:isDeleted and :mySegment in elements(e.segments)]

And I try another adjust:

Query query = sessioncreateQuery("from Enterprise in class adama.supplier.Enterprise where Enterprise.deleted!=:isDeleted and :mySegment in elements(Enterprise .segments)");
query.setParameter("isDeleted", Boolean.TRUE);
query.setParameter("mySegment", new Long(segment));
objects = query.list();

and get the follow Exception:

SEVERE: Error filtering enterprises:cirrus.hibernate.QueryException: unindexed collection before [] [from Enterprise in class adama.supplier.Enterprise where Enterprise.deleted!=:isDeleted and :mySegment in elements(Enterprise.segments)]

Any idea? :.([/i]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 7:20 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
This error comes from the first example I guess - not the second with the 'left join'...

Note sure yet why (need some sleep) but the :mySegment parameter must be an instance of "SupplierSegment" and not its ID (in both examples) !

If you don't have the SuppliedSegment entity loaded yet then this approach should do:

Code:
select enterprise

from
          Enterprise as enterprise
left join enterprise.segments as segment

where
    enterprise.deleted = :isDeleted
and segment.id         = :mySegmentId


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 7:22 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Check the docs,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 15, 2004 3:09 pm 
Newbie

Joined: Tue Sep 14, 2004 3:07 pm
Posts: 7
I try te above QUERY, but the hibernate still trhowning a "IN Expected".

I try the 11.8 Chapther, but I get the same unindexed collection result.

I just trying to do it:

select Enterprise from Enterprise,SupplierSegmentEnterpriseXref where SupplierSegmentEnterpriseXref.segment=14 and Enterprise.oid=SupplierSegmentEnterpriseXref.enterprise and Enterprise.deleted !=1

...with the follow map file:


<bag role="segments" table="SupplierSegmentEnterpriseXref">
<key column="enterprise"/>
<many-to-many column="segment" class="adama.supplier.SupplierSegment"/>
</bag>

I already try the session.createSQLQuery, but my hibernate does not seem to have this method.

Where can I search more documentation, hints,.... :(


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 15, 2004 4:34 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
goldberg wrote:
I try te above QUERY, but the hibernate still trhowning a "IN Expected".


Sorry but can't believe the example 2 posts before can throw the "IN expected" exception - there is simply no IN involved ...

Try to post a complete stack trace and an excerpt of the Hibernate log at debug level... It might help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 16, 2004 6:30 am 
Newbie

Joined: Tue Sep 14, 2004 3:07 pm
Posts: 7
I will try that.
Thank you for all your help.
Beleive me:You help me a lot.
I will trace all the system, step by step.


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