-->
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.  [ 2 posts ] 
Author Message
 Post subject: Querying properties of specific elements in a collection?
PostPosted: Tue Oct 16, 2007 10:02 am 
Newbie

Joined: Wed Sep 20, 2006 10:36 am
Posts: 1
Is it possible to query properties of either the first or last element in a one-to-many collection? I can't seem to get the functions index() or min/maxElement() to behave like I need them to. Here is an example:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Company.Business.Container, Company.Business" table="containers">
<id name="Id" type="Int32" column="id">
<generator class="native"/>
</id>

<bag name="ContainerItems" inverse="true" table="container_items" order-by="created_on DESC" lazy="true">
<key column = "container_id" />
<one-to-many class="Company.Business.ContainerItem, Company.Business" />
</bag>
</class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Company.Business.ContainerItem, URAC.Business" table="container_items">
<id name="Id" type="Int32" column="id">
<generator class="native"/>
</id>

<property name="CreatedOn" column="created_on" />
<many-to-one name="Container" class="Company.Business.Container, URAC.Business" column="container_id" unique="true" />
</class>
</hibernate-mapping>

I am trying to formulate a query that would allow me to say return the Container with the most recently added ContainerItem. In code this amounts to looking at container.ContainerItems[0].CreatedOn. Any help or direction would be appriciated. thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 1:11 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
I'm just quessing, but...

Given the orderby on the Bag, I think it returns as an ordered list (ordered by CreatedOn). This means you may be able to do this to get the most recent containerItem like so(not sure if this would work):
Code:
"from Container as container
   where container
       .ContainerItems[maxIndex(container.ContainerItems)].CreatedOn 
      = (select max(ci.CreatedOn) from ContainerItem ci)"


just a guess...

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


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