我有3个不同类的对象由单个bean处理。这些对象对应于数据库中的3个不同的表。所有3个子类都共享其大部分字段,并且每个类都有一个唯一的属性。 Bean将检索并显示所有3类的所有现有条目的表。我创建了一个父类,因为我认为这将有助于简化Bean处理视图中所有条目的表的部分
//The parent class doesn't actually exist in the DB
public class Report{
//fields shared by all 3 subclasses
//Do I put @Column here?
private long id;
protected String sharedField1, sharedField2, sharedField3;
}
@Entity
@Table(name="report_type_a")
public class ReportA extends Report{
//only this field is unique to this particular child class
private String reportAField;
}
@Entity
@Table(name="report_type_b")
public class ReportB extends Report{
//only this field is unique to this particular child class
private String reportBField;
}
@Entity
@Table(name="report_type_c")
public class ReportC extends Report{
//only this field is unique to this particular child class
private String reportCField;
}
public class ReportBean{
//a list containing objects of all 3 types to be displayed to the user later
private ArrayList<Report> reportList;
}
Spring注释会放在父项还是子项中?更重要的是,这是解决此问题的好方法吗?放弃家长班会更有意义吗?
答案 0 :(得分:0)
您只需要这个,超类中的所有内容都不会成为其自己的表的一部分。
@MappedSuperclass
public class Report{
//shared fields
private long id;
//Do I put @Column here?
protected String sharedField1, sharedField2, sharedField3;
}
请参阅https://docs.oracle.com/javaee/5/api/javax/persistence/MappedSuperclass.html