简体   繁体   English

springboot中的抽象类继承

[英]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.现在,当我启动应用程序并检查 h2-console 时,表 Company 只有 id 和 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.要解决此问题,您需要在抽象类上添加 JPA 注释。 This is not specific to Spring Boot, it's JPA thing.这不是 Spring Boot 特有的,它是 JPA 的事情。

You can apply @MappedSuperclass on your abstract class and that should resolve this issue.您可以在抽象类上应用@MappedSuperclass ,这应该可以解决此问题。 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.您可以在 AbstractUser 上使用 @MappedSuperclass Jpa 注释,还可以考虑将 id 属性移动到抽象类。

@MappedSuperclass
public abstract class AbstractUser{

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

    private String username;

    private String password;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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