简体   繁体   中英

Abstract class inheritance in springboot

I am a bit confused in regards to abstract classes and help would be great.

I have a the following classes

 public abstract class AbstractUser{
    private String username;
    private String password;
}

And then I have this class

@Entity
public class Company extends AbstractUser{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private String company_name
}

Now when I launch the application and I check the h2-console, the table Company only has the id and company_name. not the abstract classes variables. Is there a way to make it so it gets all the variables?

thank you in advance

To resolve this, you will need JPA annotation on your abstract class. This is not specific to Spring Boot, it's JPA thing.

You can apply @MappedSuperclass on your abstract class and that should resolve this issue. Find More: Inherit Super Class Properties

You can use @MappedSuperclass Jpa annotation on the AbstractUser, Also consider moving the id attribute to the abstract class.

@MappedSuperclass
public abstract class AbstractUser{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String username;

    private String password;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM