-->
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: redundant table problem in hibernate
PostPosted: Thu Nov 12, 2009 6:33 am 
Regular
Regular

Joined: Fri May 22, 2009 4:50 am
Posts: 59
Hi,

I might be lacking in my knowledge as following might be expected behavior.
I want to persist an object of class Position which is has a member of class Balance as follows:

Class Position{
private balance = new Balance();
}

where
Class Balance{
private Map money = new HashMap();
}

Now my mapping file for above is as follows:

Code:
<class name="Position" table="tbl_position">
...
    <many-to-one name="balance" class="Balance"
     column="balance_id"
     cascade="all"
     unique="true"/>
...
</class>

<class name="Balance" table="tble_balance">
     <id name="id" column="balance_id">
      ...
     </id>         
     <map name="monetary" table="tbl_money">
      ...
     </map>                         
</class>


Now the problem is, above mapping results in 3 tables. but my second table tble_balance is having just one column balance_id.
Is it possible to avoid this table(without changing the java object structure) as I can have balance_id column in tbl_money and work with just 2 tables?

I know if money Map is direct member of Position(as follows), it will result in 2 tables but I can't change java object.
Class Position{
private Map money = new HashMap();
}


Kindly share your information.

Thanks & Regards.


Top
 Profile  
 
 Post subject: Re: redundant table problem in hibernate
PostPosted: Thu Nov 12, 2009 9:04 am 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
You may map the "balance" as a component. Fields inside the component will be mapped to an object of the wanted class

Code:
<class name="Position" table="tbl_position">
...
    <component name="balance" class="Balance">
        <id name="id" column="balance_id">
      ...
        </id>         
        <map name="monetary" table="tbl_money">
      ...
        </map>   
    </component>
...
</class>


Top
 Profile  
 
 Post subject: Re: redundant table problem in hibernate
PostPosted: Fri Nov 20, 2009 7:43 am 
Regular
Regular

Joined: Fri May 22, 2009 4:50 am
Posts: 59
Hi,

Thanks for your reply. Yes it can be done by using component is this case.
But its not possible to avoid the same problem in case of more complicated mappings. like for eg.

Class Account{
private Map<String, Payment> payemnts = new Map<String, Payment>
}

Class Payment{
private Money installments;
}

Class Money{
private int[] amounts;
}

So mapping for above would be..

Code:
<class name="Account" table="tbl_account">
     <id name="aid" column="a_id">
      ...
     </id>
      ...   
     <map name="payments" table="tbl_payments">
            ...
           <many-to-one name="installments"
       column="inst_id"
       class="Money"
       unique="true"
       cascade="all"/>
     </map>
</class>

<class name="Money" table="tbl_money">
   <id name="id" column="m_id">
        <generator class="sequence">
             <param name="sequence">m_sequence</param>
        </generator>
      </id>
   <array name="amounts" table="tbl_amount">
      <key column="id"/>
      <list-index column="amount_index"/>
      <element column="value"/>
   </array>
</class>



In this case, again its difficult to avoid table table_money with single column m_id.
Any suggestions??


Thanks again.


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.