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: HELP : hibernate one-to-one mapping
PostPosted: Mon Mar 16, 2009 9:32 am 
Newbie

Joined: Fri Mar 13, 2009 10:36 am
Posts: 10
I am trying to map Account object to Transaction object.

I have a ReferenceId (type:long) field in Transaction which should hold the AccountId

The accountid is the primary key for Account and is of type long.

The mapping for Account is

<one-to-one name="Transaction" class="a.b.Transaction" cascade="save-update" fetch="select" property-ref="ReferenceId" />


When the account object is saved the ReferenceId is always set as 0.


can any one help me figure out why?

Any help or comments would he highly appreciated.

Thanks


Top
 Profile  
 
 Post subject: Re: HELP : hibernate one-to-one mapping
PostPosted: Mon Mar 16, 2009 2:56 pm 
Newbie

Joined: Fri Jun 06, 2008 2:03 pm
Posts: 9
dsouzaroy wrote:
I am trying to map Account object to Transaction object.

I have a ReferenceId (type:long) field in Transaction which should hold the AccountId

The accountid is the primary key for Account and is of type long.

The mapping for Account is

<one-to-one name="Transaction" class="a.b.Transaction" cascade="save-update" fetch="select" property-ref="ReferenceId" />


When the account object is saved the ReferenceId is always set as 0.
Could you post your code? It would be better that way.

can any one help me figure out why?

Any help or comments would he highly appreciated.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 17, 2009 4:40 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Plz post the mapping of the ReferenceId field also.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 17, 2009 10:35 am 
Newbie

Joined: Fri Mar 13, 2009 10:36 am
Posts: 10
Mapping For Transaction.

<class name="Transaction" table="Transaction" >
<id name="id">
<generator class="native"/>
</id>
<property name="referenceId" type="int"/>
<property name="name"/>
</class>


Code

In Account

public void setTransaction(Transaction trans) {
trans.setReferenceId(this.id);
this.transaction = trans;
}


Hibernate code

Session session = sessFactory.openSession();

Account a = new Account();

Transaction trans = new Transaction();
trans.setName("First Transaction");

a.setTransaction(trans);

Transaction tx = session.beginTransaction();
session.save(a);
tx.commit();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 12:39 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Code:
public void setTransaction(Transaction trans) {
trans.setReferenceId(this.id);
this.transaction = trans;
}

Here the this.id will be zero only coz both are new objects for which id is not assigned.

Why dont you make referenceId as reference to the Account object. Like:
<many-to-one name="referenceId" class="Account" unique="true"/>

I think it ll work.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 7:27 am 
Newbie

Joined: Fri Mar 13, 2009 10:36 am
Posts: 10
I could but I have a specific requirement where the referenceId might not always refer to Account object.

Also why does Hibernate not late resolve the value of referenceId ?
It works with wrapper objects like String , Integer ..

Any ideas ?


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.