-->
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.  [ 9 posts ] 
Author Message
 Post subject: Accessing other columns in UserType impl
PostPosted: Wed Jul 12, 2006 6:46 pm 
Newbie

Joined: Tue Oct 19, 2004 8:04 pm
Posts: 8
Location: Newcastle, Australia
I need a variation on the classic MonetaryUserType (that uses two columns: one with amount, the other with currency).

I have a legacy system with ONE currency column in the table, being the default for all amounts. I can't map the same column multiple times so I need the code to just know what the column is called - it can then retrieve it (updating would be done against the currency column directly).

How can I retrieve the values for such a column when the generated SQL has given the column an alias? Is there a way of finding/calculating the alias? My current code is doing a 'startsWith' as the generated alias 'startsWith' a substring of the column name.

Thanks

Michael


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 9:42 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I think that in this case, you want to map the same column multiple times. Just use either insert="false" update="false", or formula instead of column, when defining the currency column.
Code:
<class ...>
  ...
  <property name="Income" type="Monetary">
    <column name="IncomeValue"/>
    <formula>Currency</formula>
  </property>
  <property name="Outgoings" type="Monetary">
    <column name="OutgoingsValue"/>
    <formula>Currency</formula>
  </property>
  ...
</class>

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 9:52 pm 
Newbie

Joined: Tue Oct 19, 2004 8:04 pm
Posts: 8
Location: Newcastle, Australia
Thanks - now I'll complicate things by saying its EJB3, which means I'm using annotations.

Do I have?

@Columns( columns=({@Column(name="amount_column"),@Formula("currency_column")})


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 11:02 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Don't know about formula in EJB3 annotations, but I know that @column has insertable and updatable attributes. Set them both to false and that should work too.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 11:26 pm 
Newbie

Joined: Tue Oct 19, 2004 8:04 pm
Posts: 8
Location: Newcastle, Australia
Thanks - I tried insert="false" update="false" - not quite the right syntax.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 11:47 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I think in annotations it would be
Code:
insertable=false updatable=false
No quotes.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 6:08 am 
Newbie

Joined: Tue Oct 19, 2004 8:04 pm
Posts: 8
Location: Newcastle, Australia
Unfortunately Hibernate won't allow a mix of columns that are insertable/updatable and those that aren't.

Also tried the @Formula but its not allowed inside the @Columns annotation.

Which, I think, brings me back to trying to do a simple mapping to only the amount column and then have the nullSafeGet somehow find the right column alias for the known currency column.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 6:21 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I don't know how its done with annotations, but you can create UserTypes that take parameters, by having the type extend ParameterizedUserType. Then make the name of the other column a parameter of the type. There are examples in the refdocs section 5.2.3.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 6:39 pm 
Newbie

Joined: Tue Oct 19, 2004 8:04 pm
Posts: 8
Location: Newcastle, Australia
Thanks - the problem is that I dont know what the alias is, so the parameter can't really help me.

If I have a join between tables that both have a default currency column with the same name then only the alias can differentiate them.

Unless I can pass something dynamic into a parameter like some meta data that allows me to call the getAliasName on the particular column.


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