Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Correct way to set foreign keys for Mapping

The problem is that you have a foreign key constraint in both directions. 
This means if either is deleted first it will violate a constraint, so there
is no way to delete both objects together.

When you remove the Order, you can set the topItem to null first, this will
then cause an update of the foreign key to occur before the delete to
resolve the constraint.  In the latest version of EclipseLink, this update
should occur automatically.

You can also remove the constraint, or change you model to have "top" be a
field of Item, so that the topItem is just a method that search the items,
instead of a field.



sdt_dev wrote:
> 
> I have the following classes and mappings
> 
> Class Order {
> 
> //this will reference one of the items from the items List below.
> @OneToOne
> Items topItem;
> 
> @OneToMany(mappedby="orderP" , cascade=CascadeType.ALL)
> List<Items> items = new ArrayList<Items>
> 
> }
> 
> Class Items {
> @ManyToOne()
> Order orderP;
> 
> }
> 
> And in the db I have in table Order : topItem_id foreignKey references
> Items(id)
> 
> and for table Items have the : orderP_id foreignKey references Order(id)
> 
> Now when I delete the entity Order using remove(order), the items List all
> get cascaded for delete , but a foreign key violation is thrown for
> topItem_id , so to resolve this do we just remove/not use the foreign key
> constraint on topItem_id ?
> 
> But then the constraint was placed so that we dont insert an item_id that
> does not exist.
> 
> 


-----
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
Blog:  http://java-persistence-performance.blogspot.com/ Java Persistence
Performance 
-- 
View this message in context: http://old.nabble.com/Correct-way-to-set-foreign-keys-for-Mapping-tp31534496p31542846.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top