Java Mailing List Archive

http://www.gg3721.com/

Home » users.openjpa »

Re: OpenJPA - two-sided relation between objects Issue

Craig L Russell

2008-08-12

Replies: Find Java Web Hosting

Author LoginPost Reply
I know I'm jumping into this issue late, but here goes:

In order for OpenJPA to manage the relationships, you need to define
the association between the classes by using the mappedBy="address"
notation on Address.phones. This way, OpenJPA knows about the
bidirectionality of the relationship and knows to populate both sides
to be consistent.

In order to return the addressId, you need to use the getId method of
address and not persist the addressId separate from the relationship,
or have a separate addressId field.

So, I'd think this would work:

class Phone {
public Long getAddressId()
{
return this.address.getId();
}

Basically, if you eagerly fetch Address, the Id will be available.

Then, as Pinaki said earlier, the only thing you might need to do to
make the getAddressId valid is to flush the relationship. Then the
code that depends on the relationship will still work after the create
transaction is either flushed or committed.

Craig

On Aug 12, 2008, at 3:35 PM, Madhu Kalaimalai wrote:

>
> Hi Pinaki
>
> The id is generated through the oracle sequence, so the id is not
> know till
> the SQLs are generated.
>
> The problem being having the addrId and Address having mapped to the
> one
> column ADDR_FK_ID of the Phone table as we wanted the old way of
> using the
> AddrId and new way of Address to work.
>
> When i do the one direction from Address to Phone, it says we are
> trying to
> set two different values to same object. For this case for the
> AddrId column
> mapping it sets null and for the Address mapping it sets the Address
> primary
> key.
>
> When I do the bidirectional mapping, the insert statements to Phone
> has
> addrId columns twice one for the AddrId and other for Address. It
> says the
> duplicate column in the insert statement.
>
> I am looking at the possibility of POJOs supporting the old and new
> way of
> persistence.
>
> Hope you got what i am trying to explain.
>
> Rgds
> Madhu
>
>
>
> Pinaki Poddar wrote:
>>
>> Hi,
>> The mapping you described originally in this post should work as
>> well.
>> Of course, the application must set the values of ADDR_FK_ID from
>> both
>> sides consistently.
>> For example, if there is one Address instance A1 with id 12345 and
>> A1.phones is a Set that contains two Phone instances P1 and P2.
>> Then the
>> application must ensure that for both P1 and P2 addressId field is
>> set to
>> 12345 before commit. The problem will be complicated only if
>> Address has
>> auto-generated identity.
>>
>
> --
> View this message in context: http://n2.nabble.com/OpenJPA---two-sided-relation-between-objects-Issue-tp687050p720633.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Craig L Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@(protected)
P.S. A good JDO? O, Gasp!


Attachment: smime.p7s (zipped)
©2008 gg3721.com - Jax Systems, LLC, U.S.A.