-->
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.  [ 6 posts ] 
Author Message
 Post subject: Fetching referenced table with dynamic static/values?
PostPosted: Tue Jan 31, 2006 6:36 pm 
Beginner
Beginner

Joined: Wed Nov 02, 2005 3:29 pm
Posts: 20
Hibernate version:
3.1.1

Mapping documents:
<class name="ca.ubc.nmc.contacts.db.NcContact" table="NC_CONTACT">
<many-to-one name="orgUnitIdAndorgUnitType" class="ca.ubc.nmc.contacts.db.NcOrgUnit" fetch="select">
<column name="ORG_UNIT_ID" length="10" />
<column name="ORG_UNIT_TYPE" length="4" />
</many-to-one>
</class>

<class name="ca.ubc.nmc.contacts.db.NcOrgUnit" table="NC_ORG_UNIT">
<composite-id name="id" class="ca.ubc.nmc.contacts.db.NcOrgUnitId">
<key-property name="orgUnitId" type="string">
<column name="ORG_UNIT_ID" length="10" />
</key-property>
<key-property name="orgUnitTypeCde" type="string">
<column name="ORG_UNIT_TYPE_CDE" length="4" />
</key-property>
</composite-id>
</class>

Because of some strange application programming, the value
that would normally be in the column ORG_UNIT_TYPE
is determined at run time so I'm wondering if it is possible
to modify the query that is used to retrieve the NcOrgUnit
row when I do a "from NcContact" so when I reference
NcContact.orgUnitIdAndorgUnitType I get the appropriate value?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 9:06 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I don't think I fully understand the question, but is it possible that you want to use the formula attribute instead of the column attribute? This will make the column read-only, though, so you may want two mappings for the same column, one using formula and one using column. Alternatively, you could keep the existing mapping and add a new member to the pojo that implements the heuristics for you. I.e. the pojo cound have a getORG_UNIT_TYPE and a getORG_UNIT_TYPERuntime.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 4:08 pm 
Beginner
Beginner

Joined: Wed Nov 02, 2005 3:29 pm
Posts: 20
I'm hibernate newbie so I'm not up the forumula use.

To rephrase my question:

I would like some way to modify the query used by a lazy load
for a compound key to use both a value from the parent row
and a value to be provided by my code.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 5:36 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I'd have to save that that's not something that data access layers do. That's busniess logic. You could fake it like this: use hibernate to map the value in question to an "internal" value that you don't advertise in your class' interface (getInternalOrgUnitType(), or whatever). Then write your class' accessor (getOrgUnitType()) with whatever logic you need to determine the value you want to return. Something like
Code:
public OrgUnitType getOrgUnitType()
{
  if (hasUserProvidedValue())
  {
    return getUserProvidedValue();
  }

  return getInternalOrgUnitType();
}

public void setOrgUnitType(OrgUnitType t)
{
  setInternalOrgUnitType(t);
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 5:52 pm 
Beginner
Beginner

Joined: Wed Nov 02, 2005 3:29 pm
Posts: 20
I realize it is a strange situation (and not one I would have designed if
I had written the initial implementation).

I don't really care about orgUnitType being available to
to the "outside world". What I do want is the ncOrgUnit value
for the NC_CONTACT table which right now is always null
because the lazy load query always fails due to orgUnitType
having the "wrong" value for the SELECT.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 7:02 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Right, I think I'm all caught up. You've got a relationship based on a two-column key, but the type column in NcContact, despite having the right name and type, will (almost) always have the wrong value.

There's probably no simple mapping that will fix this. The only simple in-mapping solution that I can think of applies only if the type value that you want is constant, or is stored in some other column in NcContact. In this case, you can change the many-to-one mapping in NcContact to use the "formula" element instead of the "column" element. A formula element passes simple SQL (not HQL) to the query, so you could replace <column name="ORG_UNIT_TYPE"/> in NcContact with <formula>4</formula> if the value was a constant 4; or you could use <formula>ORG_UNIT_OTHER_COLUMN_NAME</formula> if the value is in another column. You can also put other SQL in there; a UDF, a simple mathematical formula, etc. Obviously, this doesn't allow for a user-supplied value.

The in-java solution would be more flexible, but with a bit of an overhead. Map the relationship as a many-to-many with just the ORG_UNIT_ID column specified, and store the resulting NcOrgUnits in a Map. Then you can implement something like this:
Code:
public OrgUnit getOrgUnit()
{
  return getOrgUnitMap().get(getOrgUnitTypeToReallyUse());
}


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