Juan Ramos
Juan Ramos

Reputation: 577

JPA Inheritance attributes from non entity class

I saw many posts around this problem however many of the answers that I found was to use the @MappedSuperclass annotation which in my case destroys the modular design that I made.

Imagine that you have two class, one is a Entity class and the other is in another module that is not a Entity class:

Module M:

@Entity
public class A {
 //Properties
}

Module M':

public class SuperA {
   String name;
   String age;

   //getters and setters
}

I want to re-use the properties of SuperA to be mapped in the A entity. But I don't want to let the Module M' neither the class SuperA about the persistence knowledge(e.g. Avoiding @MappedSuperclass annotation).

How should I approx this? I thought in a Adapter pattern for the A class, but I am not sure...

Upvotes: 0

Views: 435

Answers (1)

Maciej Kowalski
Maciej Kowalski

Reputation: 26572

I can see two options:

1) You have to add an XML based Hibernate config as part of your entire Persistence Unit. Having in mind avoiding any additional change in module M:

<component name = "SuperA">
        // properties
</component>

Here you would register the SuperA class as an embeddable.

2) Extend SuperA in the first module and mark that class as @Embeddable. Then in each entity that needs it just create a field and annotate it with @Embedded.

Upvotes: 1

Related Questions