Java Mailing List Archive

http://www.gg3721.com/

Home » users.openjpa »

Re: OpenJPA - two-sided relation between objects Issue

Pinaki Poddar

2008-08-12

Replies: Find Java Web Hosting

Author LoginPost Reply

Hi,
You can retain existing behavior but change the mapping. Is not that the
idea of O-R mapping?

public class Phone {
@ManyToOne
@ Column(name=" ADDR_FK_ID",nullable=true)
private Address address;

@Transient
private long addressId;

public Address getAddress() {
   return address;
}

// notice package level access. Only Address from the same package should
access
void setAddress(Address addr) {
    this.address = addr;
    this.addressId = addr.getId();
}

public long getAddressId() {
    return addressId;
}

// no setter for addressId



Madhu Kalaimalai wrote:
>
> Hi Pinaki
>
> Thanks for the reply.
>
> I tried the bidirectional too with nullable true on the many-to-one side.
>
> The problem being i have the existing code which uses addressId mapping
> <1> below, I can't replace this with Address <2> mapping below becoz the
> old code will not work which is using directly the identifier.
>
> The objective now is to bring the relation with out affecting the existing
> code which doesn't have the relations built
>
> <1>
> @Basic()
> @ Column(name=" ADDR_FK_ID",nullable=true)
> public Long getAddressId()
> {
> return this.addressId;
> }
>
> <2>
>
> Phone.java
> @ManyToOne
> @ Column(name=" ADDR_FK_ID",nullable=true)
> private Address address;
>
> When I try this option, I get the following error
>
> ORA-00957: duplicate column name
>
> as I have both the mapping for the ADDR_FK_ID ie the column and the
> many-to-one.
>
> My question is there a way to selective turn off the column mapping when
> going through the relation mapping . I thought of options like Embedded or
> using an Interface etc,
>
> As I said earlier, The existing code have simple pojos mapping one class
> to one table w/o relationships built. We want to bring in object relation
> for object graph persistence. Right now the developer writes code to
> persist the data row by row rather than passing the object graph to
> persist.
>
> Rgds
> Madhu
>
>
> Pinaki Poddar wrote:
>>
>> Hi,
>> 1. The mapping can be simplified as follows:
>> Address.java:
>> @OneToMany(mappedBy="address", cascade={CascadeType.ALL},
>> fetch=FetchType.LAZY)
>> private java.util.Set<Phone> phones;
>>
>> Phone.java
>> @ManyToOne
>> @ Column(name=" ADDR_FK_ID",nullable=true)
>> private Address address;
>>
>> 2. It will get rid of the two independent mappings trying to update the
>> same ADDR_FK_ID column.
>>
>> 3. Domain model should ensure referential consistency at object level.
>> For example,
>> Address.java:
>>   public void addPhone(Phone phone) {
>>      phones.add(phone);
>>      phone.setAddresss(this);
>>   }
>>
>> Phone.java:
>>  // notice package level accss
>>  void setAddress(Address addr) {
>>     this.address = addr;
>>  }
>>
>>  You can ask OpenJPA to manage consistency of inverse relations but at
>> the cost of slight performance penalty.
>>
>> 4. In O-R mapping, it makes more sense to work with object references
>> than with identifiers -- that is why Phone should declare a field of
>> Address type rather than a Long which is primary identifier of Address.
>>
>>
>
>

--
Sent from the OpenJPA Users mailing list archive at Nabble.com.

©2008 gg3721.com - Jax Systems, LLC, U.S.A.