[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [eclipselink-users] OneToMany <-> ManyToOne relationship
|
Hello Jose,
I'm not sure what you mean when you state you have problems updating the
MicroMarket.zipCode in a Customer. Since the MicroMarket.zipCode is the
primary key, it is not allowed to be changed - you must remove the old
MicroMarket and insert a new one with the pk value you wanted to change
it to. Not doing so will have consequences to object identity and
strange behavior.
As Customer -> MicroMarket has cascade all set, when you call
em.remove(customer) it should cause the referenced MicroMarket to also
be removed. You will want to ensure that all references to these
objects are removed though, or it can cause them to become re-persisted
later on. For instance, unless the removed customer is also removed
from DiscountCode's collection of customers, you will still see it there
until it is refreshed. If the DiscountCode -> customer relation is
cascade merge or persist, there is a chance that operations on
discountCode will cause the customer to be inserted again, or will undo
the remove - calling persist/merge on a removed object re-persists it.
This could be what you see happening with the MicroMarket.
Best Regards,
Chris
Jose Alvarez de Lara wrote:
Hi,
I am developing an EJB 3.0 web app on Oracle WebLogic Server 11gR1
PatchSet 1, and I am having problems
on update/delete with the relationship.
I have an entity Customer with two children DiscountCode an
MicroMarket. The relation is many Customer has
one DiscountCode and many Customer has one MicroMarket. On the other
direction it leaves as one DiscountCode
has many Customer and one MicroMarket has many Customer.
This is my code snippet,
Customer.-
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="CUSTOMER_ID")
private long customerId;
private String addressline1;
private String addressline2;
private String city;
@Column(name="CREDIT_LIMIT")
private BigDecimal creditLimit;
private String email;
private String fax;
private String name;
private String phone;
private String state;
//bi-directional many-to-one association to DiscountCode
@ManyToOne(cascade=CascadeType.MERGE)
@JoinColumn(name="DISCOUNT_CODE")
private DiscountCode discountCodeBean;
//bi-directional many-to-one association to MicroMarket
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="ZIP_CODE")
private MicroMarket microMarket;
..........................................
}
MicroMarket.-
public class MicroMarket implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="ZIP_CODE")
private String zipCode;
@Column(name="AREA_LENGTH")
private BigDecimal areaLength;
@Column(name="AREA_WIDTH")
private BigDecimal areaWidth;
private BigDecimal radius;
//bi-directional many-to-one association to Customer
@OneToMany(mappedBy="microMarket", cascade=CascadeType.ALL)
private List<Customer> customers;
.......................................
}
The problem I am having is when I update the MicroMarket.zipCode in a
Customer I get another record of
MicroMarket instead of the updated MicroMarket.
On deletion a Customer it does not delete the MicroMarket.
Well, I work only with Customer. So what I would like is when I delete
a Customer
just only the MicroMarket child would be deleted.
The problem I have is that MicroMarket child is not deleted. That is
wrong.
On the other hand the table corresponding to the DiscountCode entity must
leaves as it is without no update, no insert and no delete.
Regards,
Jose
------------------------------------------------------------------------
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users