JPA OneToMany Association from superClass

后端 未结 1 1646
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 08:56

I’m trying to map the inheritance from the superclass LendingLine and the subclasses Line and BlockLine. LendingLine has an ManyToOne association with Lending.

When

相关标签:
1条回答
  • 2021-01-03 09:14

    Choose your type of superclass according to your needs:

    Concrete Class

    public class SomeClass {}

    Define your superclass as a concrete class, when you want to query it and when you use a new operator for further logic. You will always be able to persist it directly. In the discriminator column this entity has it's own name. When querying it, it returns just instances of itself and no subclasses.

    Abstract Class

    public abstract class SomeClass {}

    Define your superclass as an abstract class when you want to query it, but don't actually use a new operator, because all logic handled is done by it's subclasses. Those classes are usually persisted by its subclasses but can still be persisted directly. U can predefine abstract methods which any subclass will have to implement (almost like an interface). In the discriminator column this entity won't have a name. When querying it, it returns itself with all subclasses, but without the additional defined information of those.

    MappedSuperclass

    @MappedSuperclass public abstract class SomeClass {}

    A superclass with the interface @MappedSuperclass cannot be queried. It provides predefined logic to all it's subclasses. This acts just like an interface. You won't be able to persist a mapped superclass.

    For further information: JavaEE 7 - Entity Inheritance Tutorial


    Original message

    Your SuperClass LendingLine needs to define a @DiscriminatorValue as well, since it can be instantiated and u use an existing db-sheme, where this should be defined.

    0 讨论(0)
提交回复
热议问题