-->
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.  [ 3 posts ] 
Author Message
 Post subject: Conditional Column Mappings
PostPosted: Fri Jul 09, 2010 2:04 pm 
Newbie

Joined: Mon May 17, 2010 11:29 am
Posts: 12
I have a schema that I am trying to map within a file. I would like to add a property on my object for 'name'. The name is derived from one of two fields in the schema, subDir1 or subDir2. If subDir2 is not null then use subDir2, else subDir1. Is there anyway I can map this conditionally in the hibernate file? I can handle this in the object itself but I would like to map the field if possible.

Thanks for any advice.


Top
 Profile  
 
 Post subject: Re: Conditional Column Mappings
PostPosted: Wed Sep 19, 2012 3:00 am 
Newbie

Joined: Wed Sep 19, 2012 2:58 am
Posts: 2
I realise this is an old post, and apologise for re-opening it, but i haven't found an answer (or even really a similar question) anywhere else.
Have you managed to sort this out?
Cheers


Top
 Profile  
 
 Post subject: Re: Conditional Column Mappings
PostPosted: Thu Sep 27, 2012 3:16 am 
Newbie

Joined: Wed Sep 19, 2012 2:58 am
Posts: 2
just in case anyone else has this issue, thought i'd lay out my solution - may not work for a lot of people, but worked in my case.

Originally I had this
Code:
   
<property name="income" type="MoneyCurrency">
      <column name="income"/>
      <column name="currency"/>
    </property>
   
    <property name="previousYearIncome" type="MoneyCurrency">
      <column name="previousYearIncome"/>
      <column name="currency" />
    </property>


the moneyCurrencyType must map onto a column for the amount of money, and one for its currency.
The front end will either have income OR previousYearIncome - never both! So we did not want to make a previousYearCurrency field in the DB, and it is reasonable to have the currency db field for both.

I could find no way to conditionally change the column names at runtime (i.e, only use the 'income' block, but sometimes it maps to income, and sometimes to previousYearIncome)

My solution was to decouple the amount and currency, using new, private variables, so:
Code:
    <property name="incomeValue" type="java.lang.Double">
       <meta attribute="scope-set">private</meta>
      <column name="income"/>
    </property>
   
    <property name="previousYearIncomeValue" type="java.lang.Double">
       <meta attribute="scope-set">private</meta>
      <column name="previousYearIncome"/>
    </property>

    <property name="currency" type="java.lang.String">
       <meta attribute="scope-set">private</meta>
      <column name="currency"/>
    </property>

and then provide getters and setters to the original variables
Code:
<meta attribute="class-code">
/** income getter */
   public Money getIncome() {
        double income = incomeValue == null ? 0d : incomeValue.doubleValue();
        CurrencyEnum currency = CurrencyEnum.getEnum(this.currency);
        return new Money(income, currency);
    }
   
      /** previousYearIncome getter */
   public Money getPreviousYearIncome() {
        double previousYearIncome = previousYearIncomeValue == null ? 0d : previousYearIncomeValue.doubleValue();
        CurrencyEnum currency = CurrencyEnum.getEnum(this.currency);
        return new Money(previousYearIncome, currency);
    }

   /** income setter */
   public void setIncome(Money income) {
        this.incomeValue = new Double(income.getAmount());
        this.currency = income.getCurrency().getName();
    }
   
      /** previousYearIncome setter */
   public void setPreviousYearIncome(Money previousYearIncome) {
        this.previousYearIncomeValue = new Double(previousYearIncome.getAmount());
        this.currency = previousYearIncome.getCurrency().getName();
    }
</meta>

note, these are no longer getters and setters exactly, as they don't represent a class variable - there is no 'Money income' variable - only the method to construct it.


This lets the java continue to use Money income & Money previousYearIncome, but hibernate only uses the separate incomeValue, previousYearIncomeValue, and currency values.


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