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: one-to-one mapping with formula
PostPosted: Tue Aug 26, 2008 9:15 am 
Newbie

Joined: Tue Aug 26, 2008 8:57 am
Posts: 3
Newbie in trouble, hope you can help.

At the moment I have two tables account and arrears_notice; there can be 0 or 1 rows on the arrears notice table.

Code:
<class
    name="com.xxx.Account"
    table="ACCOUNT"
>
    <id
        name="accountId"
        type="java.lang.Long"
        column="ACCOUNT_ID"
        unsaved-value="0"
    >
    .......
    <one-to-one name="arrearsNotice"
       class="com.xxx.ArrearsNotice"
       fetch="join"
       lazy="no-proxy"
       property-ref="account" />
    ......
</class>

   <class name="com.xxx.ArrearsNotice" table="ARREARS_NOTICE" lazy="true" >
        <id name="arrearsNoticeId" column="ARREARS_NOTICE_ID" unsaved-value="0">
            <generator class="sequence">
                <param name="sequence">seq_arrears_notice</param>
            </generator>
        </id>
        <many-to-one name="account"
          class="com.xxx.Account"
          column="ACCOUNT_ID"
          not-null="true"
          lazy="no-proxy" />
        ......
    </class>


I've now added an activeNotice column to the arrears notice table. There will only ever be one active notice row (active_notice = 1).

I want to change the mapping in the account table so that it only joins to the active row (or no row). As is, it falls over when there are multiple rows on the notice table. I don't want to change the mapping to a set, I'd like to leave it as a one-to-one.

I've tried several ways of doing this using 'formula' but am really struggling and would appreciate any help.

I'm using hibernate 3 and an Oracle database.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2008 5:10 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I have done something that is very similar. Though it was a long time ago and now that I look at it I can't really explain why it is working. In any case, I think it translates to something like this for your mappings.

In mapping for ArrearsNotice:

Code:
<properties
   name="activeArrearsNotice"
   >
   <property
      name="activeNotice"
      type="int"
      column="ACTIVE_NOTICE"
      not-null="true"
   />
   <many-to-one
      name="account"
      class="com.xxx.Account"
      column="ACCOUNT_ID"
      not-null="true"
      lazy="no-proxy"
   />
</properties>


And in the mapping for Account:

Code:
<one-to-one
   name="arrearsNotice"
   class="com.xxx.ArrearsNotice"
   fetch="join"
   lazy="no-proxy"
   property-ref="activeArrearsNotice">
  <formula>1</formula>
  <formula>ACCOUNT_ID</formula>
</one-to-one>


I think that the two formulas in the one-to-one mapping is mapped to the property and many-to-one mapping in the order they appear in the mapping documents thus creating a condition like the one below:

Code:
ARREARS_NOTICE.ACTIVE_NOTICE = 1 and ARREARS_NOTICE.ACCOUNT_ID = ACCOUNT.ACCOUNT_ID


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 5:47 am 
Newbie

Joined: Tue Aug 26, 2008 8:57 am
Posts: 3
Thank you so much, that works now. I'd tried something like that, but I thought I had to reference active_notice in the formula. I can kind of see how it works now and why it's account_id instead.

You've saved me a lot of re-writing.


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.