Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Problem with @Inheritance - Constraint violated
Problem with @Inheritance - Constraint violated [message #767912] Mon, 19 December 2011 02:46 Go to next message
Alex Mising name is currently offline Alex Mising name
Messages: 5
Registered: September 2011
Junior Member
Hi,

I've a problem on a project with some inheritance in JPA with EclipseLink. I've following classes :

1) Ref (is some references)
@Entity
@Table(name = "REF")
public class Ref {
	// Some fields...
}


2) AbstractObject is one abstract object, with several implementation (so is an abstract class, which is specified in other classes)
@Entity
@Table(name = "ABSTRACT_OBJECT")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "TYPE_OBJECT")
public abstract class AbstractObject {
	@Id
	private Long id;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "FK_REF")
	private Ref ref;

	@Column(name = "TYPE_OBJECT")
	private String typeObject
}


3) ObjectOne is one of this implementation, there may be other objects...
@Entity
@Table(name = "OBJECT_ONE")
@DiscriminatorValue("ONE")
public class ObjectOne extends AbstractObject {
	// Some fields...
}


4) Child is a child of ObjectOne (and only this)
@Entity
@Table(name = "CHILED")
public class Child {
	@Id
	private Long id;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn("FK_OBJECT_ONE")
	private ObjectOne objectOne;

	// Some fields...
}


I've allready one Ref instance in the database, and I'm saving (persist) the two following instances :


  1. ObjectOne
  2. Child


The object AbstractObject is automatically saved, when ObjectOne is persisted.

In my logs, the Objects are saved in this order :


  1. AbstractObject
  2. Child
  3. ObjectOne


--> Result, my Constraint on child (foreign key to ObjectOne) is violated !!

Why the Child is persisted before the ObjectOne ? I've assume that the AbstractObject and ObjectOne are atomicaly saved, but it isn't so.

What can I change to correct this ?

Im moment, I've a workarround : my Foreign Key is done between Child and AbstractObject, but it's not fine.


Thanks for your help !
Re: Problem with @Inheritance - Constraint violated [message #768117 is a reply to message #767912] Mon, 19 December 2011 11:02 Go to previous messageGo to next message
Karsten Wutzke is currently offline Karsten Wutzke
Messages: 112
Registered: July 2009
Senior Member
I think @DiscriminatorValue's are valid for concrete sub classes only, so chances are you are essentially confusing the code generator.

Your child classes should be marked with distinct values: @DiscriminatorValue("SUB_OBJECT_ONE_n") each. If you don't intend to instantiate ObjectOne's omit the discriminator value and mark it as abstract, otherwise it should be correct to mark each instantiable class with an @DiscriminatorValue.

HTH
Karsten

[Updated on: Mon, 19 December 2011 11:04]

Report message to a moderator

Re: Problem with @Inheritance - Constraint violated [message #768261 is a reply to message #768117] Mon, 19 December 2011 15:18 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

In JPA foreign keys are assumed to reference to root id (in the AbstractObject table) not the child table.

Can you change your constraint to the root table?

Otherwise, what version are you using, this issue may have been fixed.
If not, you can use a DescriptorCustomizer to work around the issue by setting, setHasMultipleTableConstraintDependecy(true) on the AbstractObject descriptor.


James : Wiki : Book : Blog : Twitter
Re: Problem with @Inheritance - Constraint violated [message #768975 is a reply to message #768261] Wed, 21 December 2011 02:14 Go to previous message
Alex Mising name is currently offline Alex Mising name
Messages: 5
Registered: September 2011
Junior Member
I've already implemented the Workarround with constraint... in DB is the constraint to the root ID... but it isn't nice.

My version of eclipselink is 2.3.1

I will test your another solution
Re: Problem with @Inheritance - Constraint violated [message #768976 is a reply to message #768261] Wed, 21 December 2011 02:14 Go to previous message
Alex is currently offline Alex
Messages: 65
Registered: July 2009
Member
I've already implemented the Workarround with constraint... in DB is the constraint to the root ID... but it isn't nice.

My version of eclipselink is 2.3.1

I will test your another solution
Previous Topic:ClassNotFoundException on EclipseLink MOXy
Next Topic:remove without removing
Goto Forum:
  


Current Time: Mon Sep 01 10:07:29 EDT 2014

Powered by FUDForum. Page generated in 0.01811 seconds