JPA SingleTable inheritance with @OneToMany problem

View: New views
2 Messages — Rating Filter:   Alert me  

JPA SingleTable inheritance with @OneToMany problem

by tiemykim :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a parent class with three subclasses inheriting from it using Single Table inheritance. I then have another class which has a parent class object with a @OneToMany - @ManyToOne relationship. I get an error when trying to create the database tables and was wondering if anyone had any insight into why, perhaps I am missing or am using the incorrect annotations. Thanks for your time.

Below is the error:

"Multiple possible other sides to relationship
from: AnotherClass/parentClasses
1. SubclassA/otherClass
2. SubclassB/otherClass
Looking for type: SubclassB
Add @OneToMany(targetEntity=xx.class) to fix as I don't parse the collection type."

Below is my code, the contents of many of the classes have been omitted.

Entity(name="ParentClass ")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.STRING)
public abstract class ParentClass {
@ManyToOne()
OtherClass other;

private String type;
}

Entity(name="SubclassA")
@DiscriminatorValue("A")
public class SubclassA extends ParentClass {}

Entity(name="SubclassB")
@DiscriminatorValue("B")
public class SubclassB extends ParentClass {}

Entity(name="SubclassC")
@DiscriminatorValue("C")
public class SubclassC extends ParentClass {}

@Entity(name="OtherClass")
public class OtherClass {

@OneToMany(mappedBy="OtherClass ",targetEntity=ParentClass.class,cascade=CascadeType.PERSIST)
private List< ParentClass > parentClasses = new ArrayList< ParentClass >();
}

Re: JPA SingleTable inheritance with @OneToMany problem

by christopher delahunt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I haven't seen this error, but the ManyToOne is named "other" where as
in the OneToMany, you have it mappedby "OtherClass" - it should be
mappedby "other".

Best Regards,
Chris

tiemykim wrote:

> I have a parent class with three subclasses inheriting from it using Single
> Table inheritance. I then have another class which has a parent class object
> with a @OneToMany - @ManyToOne relationship. I get an error when trying to
> create the database tables and was wondering if anyone had any insight into
> why, perhaps I am missing or am using the incorrect annotations. Thanks for
> your time.
>
> Below is the error:
>
> "Multiple possible other sides to relationship
> from: AnotherClass/parentClasses
> 1. SubclassA/otherClass
> 2. SubclassB/otherClass
> Looking for type: SubclassB
> Add @OneToMany(targetEntity=xx.class) to fix as I don't parse the collection
> type."
>
> Below is my code, the contents of many of the classes have been omitted.
>
> Entity(name="ParentClass ")
> @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
> @DiscriminatorColumn(name="type",
> discriminatorType=DiscriminatorType.STRING)
> public abstract class ParentClass {
> @ManyToOne()
> OtherClass other;
>
> private String type;
> }
>
> Entity(name="SubclassA")
> @DiscriminatorValue("A")
> public class SubclassA extends ParentClass {}
>
> Entity(name="SubclassB")
> @DiscriminatorValue("B")
> public class SubclassB extends ParentClass {}
>
> Entity(name="SubclassC")
> @DiscriminatorValue("C")
> public class SubclassC extends ParentClass {}
>
> @Entity(name="OtherClass")
> public class OtherClass {
>
> @OneToMany(mappedBy="OtherClass
> ",targetEntity=ParentClass.class,cascade=CascadeType.PERSIST)
> private List< ParentClass > parentClasses = new ArrayList< ParentClass >();
> }
>