Return-Path: Delivered-To: apmail-openjpa-users-archive@locus.apache.org Received: (qmail 71570 invoked from network); 7 Aug 2008 14:41:37 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 7 Aug 2008 14:41:37 -0000 Received: (qmail 46837 invoked by uid 500); 7 Aug 2008 14:41:36 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 46820 invoked by uid 500); 7 Aug 2008 14:41:36 -0000 Mailing-List: contact users-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@openjpa.apache.org Delivered-To: mailing list users@openjpa.apache.org Received: (qmail 46809 invoked by uid 99); 7 Aug 2008 14:41:36 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Aug 2008 07:41:36 -0700 X-ASF-Spam-Status: No, hits=3.2 required=10.0 tests=HTML_MESSAGE,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [64.233.184.226] (HELO wr-out-0506.google.com) (64.233.184.226) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Aug 2008 14:40:39 +0000 Received: by wr-out-0506.google.com with SMTP id 69so369444wri.5 for ; Thu, 07 Aug 2008 07:40:49 -0700 (PDT) Received: by 10.90.82.8 with SMTP id f8mr5042782agb.112.1218120049120; Thu, 07 Aug 2008 07:40:49 -0700 (PDT) Received: from ?128.153.11.220? ( [128.153.11.220]) by mx.google.com with ESMTPS id 18sm17218319agb.12.2008.08.07.07.40.45 (version=SSLv3 cipher=RC4-MD5); Thu, 07 Aug 2008 07:40:48 -0700 (PDT) Subject: Inconsistent execution order of INSERT statements using cascading persist From: Timothy Fanelli To: users@openjpa.apache.org Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="=-vOTRI4y+pZrxZ60A8ta9" Date: Thu, 07 Aug 2008 10:40:44 -0400 Message-Id: <1218120044.20643.18.camel@localhost> Mime-Version: 1.0 X-Mailer: Evolution 2.22.3.1 Sender: Tim Fanelli X-Virus-Checked: Checked by ClamAV on apache.org --=-vOTRI4y+pZrxZ60A8ta9 Content-Type: multipart/alternative; boundary="=-BIh/KPuBvH0oR5qeRBp+" --=-BIh/KPuBvH0oR5qeRBp+ Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Hello - I'm hoping someone can help me out here... I have two related entities using a bi-direction one-to-many relationship, as follows: @Entity @Table(name=3D"E_ORDER") @NamedQueries({...}) public class NewOrder implements Serializable { public NewOrder() { this.date =3D new Timestamp(System.currentTimeMillis()); } =09 private final static Logger LOG =3D Logger.getLogger( NewOrder.class.getNa= me() ); @Id @GeneratedValue(strategy=3DGenerationType.SEQUENCE,generator=3D"ORDERID_SE= Q") @SequenceGenerator(name=3D"ORDERID_SEQ",sequenceName=3D"ORDER_ID",allocati= onSize=3D1) @Column(name =3D "NEWORDER_ORDER_ID",updatable=3Dfalse,nullable=3Dfalse) private Integer orderIdentification; @Basic @Temporal(TemporalType.DATE) @Column(name =3D "NEWORDER_DATE") private Timestamp date; @ManyToOne(fetch =3D FetchType.EAGER,cascade=3DALL) @JoinColumn(name =3D "NEWORDER_CUSTOMER_ID", referencedColumnName =3D "CUS= TOMER_IDENTIFICATION") private Customer customer; @OneToMany(fetch =3D FetchType.EAGER,cascade=3D{ALL},mappedBy=3D"newOrder"= ) private Set items =3D new HashSet(); =20 // Set and get methods here... } and @Entity @Table(name =3D "E_NEWORDER_ORDERLINE") public class NewOrderLineItem implements Serializable { @Id @GeneratedValue(strategy=3DGenerationType.SEQUENCE, generator=3D"LINEITEMI= D_SEQ_GEN") @SequenceGenerator(name=3D"LINEITEMID_SEQ_GEN", allocationSize=3D1, sequen= ceName=3D"NEWORDERLINE_ID_SEQ") @Column(name=3D"NEWORDER_LINEITEM_ID",nullable=3Dfalse,updatable=3Dfalse) private Integer id; @ManyToOne(cascade=3D{CascadeType.ALL}) @JoinColumn(name=3D"NEWORDER_ORDER_ID", nullable=3Dfalse) private NewOrder newOrder; @Column(name=3D"NEWORDER_ITEM_ID",nullable=3Dfalse) private String itemId; =09 @Column(name =3D "NEWORDER_QUANTITY") private Integer quantity; // Set and get methods here... } So the NewOrder entity has a one-to-many relationship with the NewOrderLineItem entity, which is the owning side. My application creates a new NewOrder instance and add's several NewOrderLineItem instances to it, via an add method which maintains both sides of the relationship (by setting "this" as the neworder on the neworderlineitem, and then adding that line item to the order's hashset). I then persist my new order, and expect that cascading persistence will take care of the rest. What I expect to see in my openjpa.jdbc.SQL trace output is something like: SELECT NEXTVAL FOR ORDERID_SEQ SELECT NEXTVAL FOR NEWORDERLINE_ID_SEQ SELECT NEXTVAL FOR NEWORDERLINE_ID_SEQ SELECT NEXTVAL FOR NEWORDERLINE_ID_SEQ INSERT INTO E_NEWORDER ( NEWORDER_ORDER_ID, NEWORDER_DATE, NEWORDER_CUSTOMER_ID ) VALUES ( ?, ?, ? ) INSERT INTO E_NEWORDER_LINEITEM ( NEWORDER_LINEITEM_ID, NEWORDER_ORDER_ID, NEWORDER_ITEM_ID, NEWORDER_QUANTITY ) VALUES ( ?, ?, ?, ? ) INSERT INTO E_NEWORDER_LINEITEM ( NEWORDER_LINEITEM_ID, NEWORDER_ORDER_ID, NEWORDER_ITEM_ID, NEWORDER_QUANTITY ) VALUES ( ?, ?, ?, ? ) INSERT INTO E_NEWORDER_LINEITEM ( NEWORDER_LINEITEM_ID, NEWORDER_ORDER_ID, NEWORDER_ITEM_ID, NEWORDER_QUANTITY ) VALUES ( ?, ?, ?, ? ) However, it appears as though MOST of the time, the INSERT into the E_NEWORDER (the parent table) does not occur before the attempted inserts into the E_NEWORDER_ORDERLINE table; thus violating my referential integrity and throwing a foreign key violation SQL Exception. Sometimes though, everything works as it should and nothing goes wrong. Any idea why OpenJPA (1.1.0) would not properly insert the parent entity first? Also - I had attempted to have OpenJPA using = but we use custom data types in our schema that weren't being properly validated... so that wasn't really an option for me. Any help would be greatly appreciated! -Tim Fanelli --=-BIh/KPuBvH0oR5qeRBp+ Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hello -

I'm hoping someone can help me out here... I have two related entities usin= g a bi-direction one-to-many relationship, as follows:

@Entity
@Table(name=3D"E_ORDER")
@NamedQueries({...})
public class NewOrder implements Serializable {
	public NewOrder() {
		this.date =3D new Timestamp(System.currentTimeMillis());
	}
=09
	private final static Logger LOG =3D Logger.getLogger( NewOrder.class.getNa=
me() );

	@Id
	@GeneratedValue(strategy=3DGenerationType.SEQUENCE,generator=3D"ORDER=
ID_SEQ")
	@SequenceGenerator(name=3D"ORDERID_SEQ",sequenceName=3D"ORD=
ER_ID",allocationSize=3D1)
	@Column(name =3D "NEWORDER_ORDER_ID",updatable=3Dfalse,nullable=
=3Dfalse)
	private Integer orderIdentification;

	@Basic
	@Temporal(TemporalType.DATE)
	@Column(name =3D "NEWORDER_DATE")
	private Timestamp date;

	@ManyToOne(fetch =3D FetchType.EAGER,cascade=3DALL)
	@JoinColumn(name =3D "NEWORDER_CUSTOMER_ID", referencedColumnNam=
e =3D "CUSTOMER_IDENTIFICATION")
	private Customer customer;

	@OneToMany(fetch =3D FetchType.EAGER,cascade=3D{ALL},mappedBy=3D"n=
ewOrder")
	private Set<NewOrderLineItem> items =3D new HashSet<NewOrderLi=
neItem>();
=20
	// Set and get methods here...
}

and

@Entity
@Table(name =3D "E_NEWORDER_ORDERLINE")
public class NewOrderLineItem implements Serializable {
	@Id
	@GeneratedValue(strategy=3DGenerationType.SEQUENCE, generator=3D"LINE=
ITEMID_SEQ_GEN")
	@SequenceGenerator(name=3D"LINEITEMID_SEQ_GEN", allocationSize=
=3D1, sequenceName=3D"NEWORDERLINE_ID_SEQ")
	@Column(name=3D"NEWORDER_LINEITEM_ID",nullable=3Dfalse,updatable=
=3Dfalse)
	private Integer id;

	@ManyToOne(cascade=3D{CascadeType.ALL})
	@JoinColumn(name=3D"NEWORDER_ORDER_ID", nullable=3Dfalse)
	private NewOrder newOrder;

	@Column(name=3D"NEWORDER_ITEM_ID",nullable=3Dfalse)
	private String itemId;
=09
	@Column(name =3D "NEWORDER_QUANTITY")
	private Integer quantity;

	// Set and get methods here...
}

So the NewOrder entity has a one-to-many relationship with the NewOrderLine= Item entity, which is the owning side. My application creates a new NewOrde= r instance and add's several NewOrderLineItem instances to it, via an add m= ethod which maintains both sides of the relationship (by setting "this= " as the neworder on the neworderlineitem, and then adding that line i= tem to the order's hashset). I then persist my new order, and expect that c= ascading persistence will take care of the rest.

What I expect to see in my openjpa.jdbc.SQL trace output is something like:=

SELECT NEXTVAL FOR ORDERID_SEQ
SELECT NEXTVAL FOR NEWORDERLINE_ID_SEQ
SELECT NEXTVAL FOR NEWORDERLINE_ID_SEQ
SELECT NEXTVAL FOR NEWORDERLINE_ID_SEQ
INSERT INTO E_NEWORDER ( NEWORDER_ORDER_ID, NEWORDER_DATE, NEWORDER_CUSTOME= R_ID ) VALUES ( ?, ?, ? )
INSERT INTO E_NEWORDER_LINEITEM ( NEWORDER_LINEITEM_ID, NEWORDER_ORDER_ID, = NEWORDER_ITEM_ID, NEWORDER_QUANTITY ) VALUES ( ?, ?, ?, ? )
INSERT INTO E_NEWORDER_LINEITEM ( NEWORDER_LINEITEM_ID, NEWORDER_ORDER_ID, = NEWORDER_ITEM_ID, NEWORDER_QUANTITY ) VALUES ( ?, ?, ?, ? )
INSERT INTO E_NEWORDER_LINEITEM ( NEWORDER_LINEITEM_ID, NEWORDER_ORDER_ID, = NEWORDER_ITEM_ID, NEWORDER_QUANTITY ) VALUES ( ?, ?, ?, ? )

However, it appears as though MOST of the time, the INSERT into the E_NEWOR= DER (the parent table) does not occur before the attempted inserts into the= E_NEWORDER_ORDERLINE table; thus violating my referential integrity and th= rowing a foreign key violation SQL Exception. Sometimes though, everything = works as it should and nothing goes wrong.

Any idea why OpenJPA (1.1.0) would not properly insert the parent entity fi= rst?

Also - I had attempted to have OpenJPA using <property name=3D"open= jpa.jdbc.SchemaFactory" value=3D"native(ForeignKeys=3Dtrue)"= /> but we use custom data types in our schema that weren't being properl= y validated... so that wasn't really an option for me.

Any help would be greatly appreciated!
-Tim Fanelli


--=-BIh/KPuBvH0oR5qeRBp+-- --=-vOTRI4y+pZrxZ60A8ta9 Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQBImwlse3BRtlmdHM4RApc7AKCATzF/6sycS7WFvF91XZxhh5x66QCfaG5G Js8jpaBQVza9EriwKjDhMu0= =zZGv -----END PGP SIGNATURE----- --=-vOTRI4y+pZrxZ60A8ta9--