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.  [ 4 posts ] 
Author Message
 Post subject: Accessing list-index from list elements
PostPosted: Thu Dec 01, 2005 5:52 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Can I get hibernate to put the list index into entities within the list? Or even better, can I put the nesting entity into the nested entity (allowing me to use getList().indexOf(this))? Consider this mapping fragment:

Code:
<class name="configuration" ...>
  ...
  <list name="routers" ...>
    <key column="ConfigID"/>
    <list-index column="ConfNum"/>
    <many-to-many column="RouterID" class="Router"/>
  </list>
  ...
</class>


What has to be in the Router class to allow it to know about the configuration when it is loaded via this list. If it is loaded independently, then the configuration details would be unknown (null configuration entity, or ConfNum = -1 if that's all I can work with). I don't want to put a full-fledged bidriectional mapping in Router, because a router's configurations mean nothing to it in my data model: all that makes sense is a configuration as it applies to a router.

At the moment I'm putting a reference to the configuration into the router in business logic after it's loaded. It's working fine, but it has to be done in the 5 or 10 busniess logic methods that load configurations, and I can see it becoming a maintenance issue in the future.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 6:45 pm 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
Can you use a Set instead of a List and put the 'index' property in the element?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 6:58 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I was using a set before, but it was causing problems. Plus using a list means that hibernate nicely creates the index values for me.

The index column (ConfNum) is in my link table, and I'd like the ConfNum value to be stored in my data entity. My original solution was to use a set with a composite-element, which could take advantage of the parent tag. I used the same class for both the composite-element and the many-to-one inside it, and added a getThis()/setThis() no-op pair of methods so that ConfNum (in the link table) and the rest of the value fields (in the entity table) were being set into the same object. It was working fine for reads on test data, but when I tried inserting new data I was always getting an unsaved transient exception, presumably because hibernate was trying to save getThis() before my entity.. but it was my entity.

The problem only exists because I want different functionality when loading this router object from the set/map/list as opposed to loading it directly: it's not uni- or bi-directional, it's a one-and-a-half-directional mapping :). I realise I'm being awkward here, and I'm happy to live with my current solution, I was just keen to find something more elegant.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 10:50 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
if the association is bi-directional, you could just do:

router.getConfiguration().getRouters().indexOf( router );

sucks in that the routers collection would either need to be loaded or would get loaded though...


Another option would be to map the index w/ update="false" insert="false" on the Router class, but that info would only be accurate as of the last time the thing was read from the database.


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