You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by tvogel <tv...@msn.com> on 2016/03/30 19:33:26 UTC

Wrong order for delete of child when deleting parent

The order of deletes for OpenJPA 2.3.x are happening in the wrong order and I
can't figure out why.

Given these JPA mappings

	// grandparent
	@Entity
	@Table(name = "three_phase_motor_input")
	public class ThreePhaseMotorInput implements IThreePhaseMotorInput,
Serializable {
		@Id
		@GeneratedValue(strategy = GenerationType.IDENTITY)
		private Long id;
		@Version
		private Integer version;

		@OneToOne(orphanRemoval = true, cascade = CascadeType.ALL, optional =
true, targetEntity = UnapprovedThreePhaseMotor.class)
		@JoinColumn(name = "unapproved_id")
		private IThreePhaseMotor unapprovedMotor;

	// parent
	@Entity
	@Table(name = "unapproved_three_phase_motor")
	public class UnapprovedThreePhaseMotor extends ThreePhaseMotor {
		@OneToMany(orphanRemoval = true, cascade = CascadeType.ALL, fetch =
FetchType.LAZY, targetEntity = UnapprovedThreePhaseWire.class)
		@JoinColumn(name = "motor_id", referencedColumnName = "id", nullable =
false)
		@OrderColumn(name = "idx")
		private List<IThreePhaseWire> wires;


	// child - Abstract
	@MappedSuperclass
	@Access(AccessType.FIELD)
	public abstract class ThreePhaseWire implements IThreePhaseWire,
Serializable {
		@Id
		@GeneratedValue(strategy = GenerationType.IDENTITY)
		private Long id;

	// child concrete
	@Entity
	@Table(name = "unapproved_three_phase_wire")
	public class UnapprovedThreePhaseWire extends ThreePhaseWire {


Order of SQL statements in log file when setting unapprovedMotor in
grandparent to null

	 572353115 UPDATE three_phase_motor_input SET status = ?, version = ?,
approved_id = ?, unapproved_id = ? WHERE id = ? AND version = ?
[params=(int) 1, (int) 8, (null) null, (null) null, (long) 896, (int) 7]
	 1683522521 DELETE FROM unapproved_three_phase_motor WHERE id = ? AND
version = ? [params=(long) 209938, (int) 1]
	 446470297 DELETE FROM unapproved_three_phase_wire WHERE id = ?
[params=(long) 1394]

This causes a foreign key constraint error since the child still exists when
the parent delete statement is executed.
	 



--
View this message in context: http://openjpa.208410.n2.nabble.com/Wrong-order-for-delete-of-child-when-deleting-parent-tp7589053.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Wrong order for delete of child when deleting parent

Posted by Mark Struberg <st...@yahoo.de.INVALID>.
You might also do a normal em.remove() in the right order manually. Most probably no need to use a native query.

LieGrue,
strub






> On Monday, 4 April 2016, 19:44, tvogel <tv...@msn.com> wrote:
> > I don't have a solution but a workaround.  In my service layer, I wrote
> native sql queries to handle the delete manually.
> Tim
> 
> 
> 
> --
> View this message in context: 
> http://openjpa.208410.n2.nabble.com/Wrong-order-for-delete-of-child-when-deleting-parent-tp7589053p7589070.html
> 
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
> 

RE: Wrong order for delete of child when deleting parent

Posted by tvogel <tv...@msn.com>.
I don't have a solution but a workaround.  In my service layer, I wrote
native sql queries to handle the delete manually.
Tim



--
View this message in context: http://openjpa.208410.n2.nabble.com/Wrong-order-for-delete-of-child-when-deleting-parent-tp7589053p7589070.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

RE: Wrong order for delete of child when deleting parent

Posted by Boblitz John <jo...@bertschi.com>.
Hello Tim,

My "Non expert" opinion is that your entities look okay, although to be sure, you would need to add Unique Constraints to them.  

According to "The Book", that is supposed to work:

13.2. Unique Constraints
...
"The unique constraints you define are used during table
creation to generate the proper database constraints, and may also be used at runtime to order INSERT, UPDATE , and DELETE
statements. For example, suppose there is a unique constraint on the columns of field F. In the same transaction, you remove an
object A and persist a new object B, both with the same F value. The JPA runtime must ensure that the SQL deleting A is sent to
the database before the SQL inserting B to avoid a unique constraint violation."

Unfortunately, that failed for me when I tried it - which is the reason for the Jira link posted earlier.  There is apparently a Bug in the code ....


See this for the discussion that led to the Jira:  http://openjpa.208410.n2.nabble.com/Order-of-SQL-Statements-td7580252.html

Cheers

John

PS - if you find a solution, please share!


> -----Original Message-----
> From: tvogel [mailto:tvogel@msn.com]
> Sent: Donnerstag, 31. März 2016 13:51
> To: users@openjpa.apache.org
> Subject: RE: Wrong order for delete of child when deleting parent
> 
> John,
>   Thanks for taking the time to read, reply and for the issue link.
> 
>   I switched from a @MappedSuperclass to @Entity with
> @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS).  The issue is
> still present.
> 
>   Do my mappings look correct for a uni-directional one-to-one mapping
> owned by the many side (for motor to wire)?
> 
> Tim
> 
> 
> 
> --
> View this message in context: http://openjpa.208410.n2.nabble.com/Wrong-
> order-for-delete-of-child-when-deleting-parent-tp7589053p7589056.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.

RE: Wrong order for delete of child when deleting parent

Posted by tvogel <tv...@msn.com>.
John,
  Thanks for taking the time to read, reply and for the issue link.

  I switched from a @MappedSuperclass to @Entity with @Inheritance(strategy
= InheritanceType.TABLE_PER_CLASS).  The issue is still present.

  Do my mappings look correct for a uni-directional one-to-one mapping owned
by the many side (for motor to wire)?

Tim



--
View this message in context: http://openjpa.208410.n2.nabble.com/Wrong-order-for-delete-of-child-when-deleting-parent-tp7589053p7589056.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

RE: Wrong order for delete of child when deleting parent

Posted by Boblitz John <jo...@bertschi.com>.
This sounds like it is related to this issue https://issues.apache.org/jira/browse/OPENJPA-2211

John

> -----Original Message-----
> From: tvogel [mailto:tvogel@msn.com]
> Sent: Mittwoch, 30. März 2016 19:33
> To: users@openjpa.apache.org
> Subject: Wrong order for delete of child when deleting parent
> 
> The order of deletes for OpenJPA 2.3.x are happening in the wrong order
> and I can't figure out why.
> 
> Given these JPA mappings
> 
> 	// grandparent
> 	@Entity
> 	@Table(name = "three_phase_motor_input")
> 	public class ThreePhaseMotorInput implements
> IThreePhaseMotorInput, Serializable {
> 		@Id
> 		@GeneratedValue(strategy = GenerationType.IDENTITY)
> 		private Long id;
> 		@Version
> 		private Integer version;
> 
> 		@OneToOne(orphanRemoval = true, cascade =
> CascadeType.ALL, optional = true, targetEntity =
> UnapprovedThreePhaseMotor.class)
> 		@JoinColumn(name = "unapproved_id")
> 		private IThreePhaseMotor unapprovedMotor;
> 
> 	// parent
> 	@Entity
> 	@Table(name = "unapproved_three_phase_motor")
> 	public class UnapprovedThreePhaseMotor extends
> ThreePhaseMotor {
> 		@OneToMany(orphanRemoval = true, cascade =
> CascadeType.ALL, fetch = FetchType.LAZY, targetEntity =
> UnapprovedThreePhaseWire.class)
> 		@JoinColumn(name = "motor_id", referencedColumnName
> = "id", nullable =
> false)
> 		@OrderColumn(name = "idx")
> 		private List<IThreePhaseWire> wires;
> 
> 
> 	// child - Abstract
> 	@MappedSuperclass
> 	@Access(AccessType.FIELD)
> 	public abstract class ThreePhaseWire implements IThreePhaseWire,
> Serializable {
> 		@Id
> 		@GeneratedValue(strategy = GenerationType.IDENTITY)
> 		private Long id;
> 
> 	// child concrete
> 	@Entity
> 	@Table(name = "unapproved_three_phase_wire")
> 	public class UnapprovedThreePhaseWire extends ThreePhaseWire {
> 
> 
> Order of SQL statements in log file when setting unapprovedMotor in
> grandparent to null
> 
> 	 572353115 UPDATE three_phase_motor_input SET status = ?,
> version = ?, approved_id = ?, unapproved_id = ? WHERE id = ? AND version =
> ?
> [params=(int) 1, (int) 8, (null) null, (null) null, (long) 896, (int) 7]
> 	 1683522521 DELETE FROM unapproved_three_phase_motor WHERE
> id = ? AND version = ? [params=(long) 209938, (int) 1]
> 	 446470297 DELETE FROM unapproved_three_phase_wire WHERE id
> = ?
> [params=(long) 1394]
> 
> This causes a foreign key constraint error since the child still exists when the
> parent delete statement is executed.
> 
> 
> 
> 
> --
> View this message in context: http://openjpa.208410.n2.nabble.com/Wrong-
> order-for-delete-of-child-when-deleting-parent-tp7589053.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Wrong order for delete of child when deleting parent

Posted by tvogel <tv...@msn.com>.
I will upgrade to 2.4.1 as soon as karaf releases their 4.0.5 which has it as
part of the base installation.  I'll post back this month on the results.

If the issue is still present in 2.4.1, I will try to create a smallish
example.  Do you have a template / starter that I could use for this
example?

Tim



--
View this message in context: http://openjpa.208410.n2.nabble.com/Wrong-order-for-delete-of-child-when-deleting-parent-tp7589053p7589069.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.