openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Timothy Fanelli <...@timfanelli.com>
Subject Inconsistent execution order of INSERT statements using cascading persist
Date Thu, 07 Aug 2008 14:40:44 GMT
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="E_ORDER")
@NamedQueries({...})
public class NewOrder implements Serializable {
	public NewOrder() {
		this.date = new Timestamp(System.currentTimeMillis());
	}
	
	private final static Logger LOG = Logger.getLogger( NewOrder.class.getName() );

	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="ORDERID_SEQ")
	@SequenceGenerator(name="ORDERID_SEQ",sequenceName="ORDER_ID",allocationSize=1)
	@Column(name = "NEWORDER_ORDER_ID",updatable=false,nullable=false)
	private Integer orderIdentification;

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

	@ManyToOne(fetch = FetchType.EAGER,cascade=ALL)
	@JoinColumn(name = "NEWORDER_CUSTOMER_ID", referencedColumnName = "CUSTOMER_IDENTIFICATION")
	private Customer customer;

	@OneToMany(fetch = FetchType.EAGER,cascade={ALL},mappedBy="newOrder")
	private Set<NewOrderLineItem> items = new HashSet<NewOrderLineItem>();
 
	// Set and get methods here...
}


and


@Entity
@Table(name = "E_NEWORDER_ORDERLINE")
public class NewOrderLineItem implements Serializable {
	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="LINEITEMID_SEQ_GEN")
	@SequenceGenerator(name="LINEITEMID_SEQ_GEN", allocationSize=1, sequenceName="NEWORDERLINE_ID_SEQ")
	@Column(name="NEWORDER_LINEITEM_ID",nullable=false,updatable=false)
	private Integer id;

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

	@Column(name="NEWORDER_ITEM_ID",nullable=false)
	private String itemId;
	
	@Column(name = "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 <property
name="openjpa.jdbc.SchemaFactory" value="native(ForeignKeys=true)"/> 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



Mime
View raw message