Problem with @Inheritance - Constraint violated [message #767912] |
Mon, 19 December 2011 07:46 |
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 :
- ObjectOne
- Child
The object AbstractObject is automatically saved, when ObjectOne is persisted.
In my logs, the Objects are saved in this order :
- AbstractObject
- Child
- 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 16:02 |
Karsten Wutzke Messages: 124 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 16:04] Report message to a moderator
|
|
|
Re: Problem with @Inheritance - Constraint violated [message #768261 is a reply to message #768117] |
Mon, 19 December 2011 20:18 |
|
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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04144 seconds