-->
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: Using variables in the where attribute
PostPosted: Mon Apr 10, 2006 11:48 pm 
Newbie

Joined: Mon Apr 10, 2006 11:45 pm
Posts: 17
I know this should be easy to find but I can't seem to find it anywhere. I'm simply trying to use a variable in my class as part of the where clause when I'm declaring a one-to-many relationship. I assume there's a way to do this but I can't quite figure out the syntax.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 11:55 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The variable has to be a property of the owning object, and then you use it via the <key> element and property-ref attribute, as appropriate. If it's a constant, you can use the where attribute of the set/list/map element.

If that doesn't help, post some specifics. Table shapes and desired java results will allow me to write the mapping for you.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 12:09 am 
Newbie

Joined: Mon Apr 10, 2006 11:45 pm
Posts: 17
Here's what I have:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

<class name="Supremacy.DataAccess.DbObjects.Railroads, DataAccess" table="railroad">

<composite-id>
<key-property name="BeginRegion" column="beginregion"/>
<key-property name="EndRegion" column="endregion"/>
<key-property name="GameID" column="gameid"/>
</composite-id>

<bag name="Into" inverse="true" cascade="all" table="railroad">
<key>
<column name="beginregion"/>
<column name="endregion"/>
<column name="gameid"/>
</key>
<one-to-many class="Supremacy.DataAccess.DbObjects.Railroads, DataAccess"/>
</bag>
</class>

</hibernate-mapping>

(yes this is supposed to be a list of objects of the same type as the base object)

I essentially want a list of all railroads into this one region. I want that determined based off of the "EndRegion" property. So I would like my "Into" list to have a list of all of the other records in this table that have their "endregion" equal to this region's "beginregion." I would like to just add "where='endregion=this.BeginRegion'" or something like that in my bag declaration.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 12:31 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I'm pretty sure that your current map won't allow that. You need a key that doesn't include regions. In fact, I recommend having a RailroadId property and column, and don't use any of GameID, EndRegion or BeginRegion in Railroad's key.

Unfortunately for you, NHibernate is based off Hibernate2, so you can't use property-ref in one-to-many mappings (that's only supported in 3.1+). But you can use a unidirectional many-to-one mapping to accomplish precisely the same thing. The ultimate mapping will be something to this effect:
Code:
<class name="Supremacy.DataAccess.DbObjects.Railroads, DataAccess" table="railroad">

  <id name="RailroadId" column="RailroadId" type="integer>
    <generator class="native"/>
  </id>

  <!-- Maybe these are many-to-ones? -->
  <property name="BeginRegion" column="beginregion"/>
  <property name="EndRegion" column="endregion"/>
  <property name="GameID" column="gameid"/>

  <bag name="Into" inverse="true" cascade="all">
    <key>
      <column name="gameid"/>
      <column name="beginregion"/>
    </key>

    <many-to-one class="Supremacy.DataAccess.DbObjects.Railroads, DataAccess">
      <column name="gameid"/>
      <column name="endregion/>
    </many-to-one>
  </bag>
</class>
I'm not exactly sure what features NHibernate currently supports, and there are several abbreviations available in recent hibernate release that make this mapping easier to read (property-ref is added all over the place, very handy), but this or something like it should do what you need.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 12:36 am 
Newbie

Joined: Mon Apr 10, 2006 11:45 pm
Posts: 17
Not an ideal solution but it should work. Thanks


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.