[英]Abstract class inheritance in springboot
我对抽象类有点困惑,帮助会很好。
我有以下课程
public abstract class AbstractUser{
private String username;
private String password;
}
然后我有这门课
@Entity
public class Company extends AbstractUser{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String company_name
}
现在,当我启动应用程序并检查 h2-console 时,表 Company 只有 id 和 company_name。 不是抽象类变量。 有没有办法让它得到所有的变量?
先感谢您
要解决此问题,您需要在抽象类上添加 JPA 注释。 这不是 Spring Boot 特有的,它是 JPA 的事情。
您可以在抽象类上应用@MappedSuperclass
,这应该可以解决此问题。 查找更多: 继承超类属性
您可以在 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.