You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by pi...@poczta.onet.pl on 2007/09/10 16:40:40 UTC

Cascade persist

I've got a question - is it possible to use openJPA with database that has set foreign key constraints?

I've got two tables - order and order item. Order item has a column with order_id, which is PK for order table. In Java classes I set relationship as OneToMany for Order (with Collection of OrderItems field) and ManyToOne for OrderItem (with field of Order type). I put Cascadetype.PERSIST, REMOVE and MERGE to OneToMany annotation. When I try to invoke persist method on EntityManger - sometimes I receive errors causes by OpenJPA tries to persist OrderItem object (with value form Order foreign key) before persisting of Order object - that cause database to throw exception that foreign key is not present in Order table. It's very odd for me - because I can persist for example 2 order object with no errors and then see this exception:
<openjpa-1.0.0-r420667:568756 nonfatal store error> org.apache.openjpa.util.StoreException: ERROR: insert or update on table "order_item" violates foreign key constraint "order_item_fk"

Is there any solution for this problem? Can I somehow point in which order object should be persist? In addition - I tried to persist order object that has list which consist of only one orderitem object.

I use OpenJPA 1.0.0 with PostgreSQL 8.2.4 and JDBC driver postgresql-8.2-505.jdbc3.jar and Glassfish v2 b57.

Thanks for any help!

Jakub Piechnik


Re: Cascade persist

Posted by Christian Defoy <ch...@gmail.com>.
As far as I know, you have to tell OpenJPA of the existence of the
foreign keys.  You can do that by using the @ForeignKey annotation.
Alternatively, you can add this property to your persistence.xml.  It
will tell OpenJPA to discover what it can about foreign keys:

<property name="openjpa.jdbc.SchemaFactory" value="native(ForeignKeys=true)" />

There is also a problem that I noticed a short while ago that persists
calls are performed out of order.  You can try to modify BrokerImpl so
that _transAddition attribute is a LinkedHashSet instead of a
HashSet...  You can probably identify this problem easily.  Look at
the SQL traces, do you see an insert statement without any column for
your order item?  You could also make it even more obvious by adding a
not null constraint on the column that contains the foreign key (just
for fun).

Christian

On 9/10/07, piechutm@poczta.onet.pl <pi...@poczta.onet.pl> wrote:
> I've got a question - is it possible to use openJPA with database that has set foreign key constraints?
>
> I've got two tables - order and order item. Order item has a column with order_id, which is PK for order table. In Java classes I set relationship as OneToMany for Order (with Collection of OrderItems field) and ManyToOne for OrderItem (with field of Order type). I put Cascadetype.PERSIST, REMOVE and MERGE to OneToMany annotation. When I try to invoke persist method on EntityManger - sometimes I receive errors causes by OpenJPA tries to persist OrderItem object (with value form Order foreign key) before persisting of Order object - that cause database to throw exception that foreign key is not present in Order table. It's very odd for me - because I can persist for example 2 order object with no errors and then see this exception:
> <openjpa-1.0.0-r420667:568756 nonfatal store error> org.apache.openjpa.util.StoreException: ERROR: insert or update on table "order_item" violates foreign key constraint "order_item_fk"
>
> Is there any solution for this problem? Can I somehow point in which order object should be persist? In addition - I tried to persist order object that has list which consist of only one orderitem object.
>
> I use OpenJPA 1.0.0 with PostgreSQL 8.2.4 and JDBC driver postgresql-8.2-505.jdbc3.jar and Glassfish v2 b57.
>
> Thanks for any help!
>
> Jakub Piechnik
>
>

RE: Cascade persist

Posted by CASERO Jaime <JC...@covansys.com>.
I guess you have some method like "addOrderItem" in "Order" class,
inserting the items in the order collection (just in "memory"). Once you
have all the items added in the order, you should be able to persist the
order in one step (JPA will take care of persisting the related
orderItems): something like:
Class Order
{
	@OneToMany(...)
	Collection<OrderItem> items;
	Public void add(OrderItem oItem)
	{	
		oItem.setOrder(this);
		items.add(oItem);
	}
}

Class OrderItem
{
	@ManyToOne
	Order order;
	Public void setOrder(Order o)
	{
		order = o;
	}
}

	


Order order1  = new Order(); 
OrderItem orderItem 1 = new OrderItem1();
OrderItem orderItem2 = new OrderItem2();
Order1.add(orderItem1);//internally you add the object to the
collection, and set the order parent of orderItem1 to order.
Order1.add(orderItem2); //internally you add the object to the
collection, and set the order parent of orderItem1 to order.
em.persist(order); //now both order and items are persisted in one step.


-----Original Message-----
From: piechutm@poczta.onet.pl [mailto:piechutm@poczta.onet.pl] 
Sent: Monday, September 10, 2007 4:41 PM
To: users@openjpa.apache.org
Subject: Cascade persist

I've got a question - is it possible to use openJPA with database that
has set foreign key constraints?

I've got two tables - order and order item. Order item has a column with
order_id, which is PK for order table. In Java classes I set
relationship as OneToMany for Order (with Collection of OrderItems
field) and ManyToOne for OrderItem (with field of Order type). I put
Cascadetype.PERSIST, REMOVE and MERGE to OneToMany annotation. When I
try to invoke persist method on EntityManger - sometimes I receive
errors causes by OpenJPA tries to persist OrderItem object (with value
form Order foreign key) before persisting of Order object - that cause
database to throw exception that foreign key is not present in Order
table. It's very odd for me - because I can persist for example 2 order
object with no errors and then see this exception:
<openjpa-1.0.0-r420667:568756 nonfatal store error>
org.apache.openjpa.util.StoreException: ERROR: insert or update on table
"order_item" violates foreign key constraint "order_item_fk"

Is there any solution for this problem? Can I somehow point in which
order object should be persist? In addition - I tried to persist order
object that has list which consist of only one orderitem object.

I use OpenJPA 1.0.0 with PostgreSQL 8.2.4 and JDBC driver
postgresql-8.2-505.jdbc3.jar and Glassfish v2 b57.

Thanks for any help!

Jakub Piechnik
 
Confidentiality Statement:
 
This message is intended only for the individual or entity to which it is addressed. It may contain privileged, confidential information which is exempt from disclosure under applicable laws. If you are not the intended recipient, please note that you are strictly prohibited from disseminating or distributing this information (other than to the intended recipient) or copying this information. If you have received this communication in error, please notify us immediately by return email.
-----------------------------