Entity extension

I have a class called AbstractEntity that is annotated using @MappedSuperclass. Then I have a class called User (@Entity) that extends AbstractEntity. Both of them exist in a package called foo.bar.framework. When I use these two classes, everything works fine. But now I imported the jar containing these files into another project. I would like to reuse the User class and extend it with a few extra fields. I thought that @Entity public class User extends foo.bar.framework.User would do the trick, but I found that this User implementation only inherits fields from AbstractEntity, but nothing from foo.bar.framework.User. The question is, how can I make my second User class inherit all fields from the first class of the user entity?

Both implementations of the user class have different table names defined with @Table (name = "name").

My classes are as follows

 package foo.bar.framework; @MappedSuperclass abstract public class AbstractEntity { @Id @GeneratedValue(strategy = GenerationType.AUTO) protected Long id; @Column(nullable = false) @Version protected Long consistencyVersion; ... }
package foo.bar.framework; @MappedSuperclass abstract public class AbstractEntity { @Id @GeneratedValue(strategy = GenerationType.AUTO) protected Long id; @Column(nullable = false) @Version protected Long consistencyVersion; ... } 
 package foo.bar.framework; @Entity @Table(name = "foouser") public class User extends AbstractEntity { protected String username; protected String password; .... }
package foo.bar.framework; @Entity @Table(name = "foouser") public class User extends AbstractEntity { protected String username; protected String password; .... } 
 package some.application; @Entity @Table(name = "myappuser") public class User extends foo.bar.framework.User { protected String firstname; protected String lastname; protected String email; .... }
package some.application; @Entity @Table(name = "myappuser") public class User extends foo.bar.framework.User { protected String firstname; protected String lastname; protected String email; .... } 

Using the above code, EclipseLink will create a table named "myappuser" containing the fields "id", "consistencyVersion", "firstname", "lastname" and "email". The "username" and "password" fields are not created in the table - and this is the problem I encountered.

+4
source share
1 answer

With JPA, the default inheritance strategy (that is, when not specified) is SINGLE_TABLE : there is only one table in the inheritance hierarchy, and all fields are stored in the base class table.

If you want the table for each class in the inheritance hierarchy and each table contain columns for all inherited fields, you need to use the TABLE_PER_CLASS strategy.

 package foo.bar.framework; @MappedSuperclass @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) abstract public class AbstractEntity { @Id @GeneratedValue(strategy = GenerationType.AUTO) protected Long id; @Column(nullable = false) @Version protected Long consistencyVersion; ... } 
+4
source

Source: https://habr.com/ru/post/1304706/


All Articles