Java Mailing List Archive

http://www.gg3721.com/

Home » users.openjpa »

Re: OpenJPA - two-sided relation between objects Issue

Pinaki Poddar

2008-08-11

Replies: Find Java Web Hosting

Author LoginPost Reply

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.