子类

时间:2015-08-04 12:01:52

标签: java hibernate

我是这个注释的新手。请帮我解决下面的问题。

我的场景就像我有BasicInfo(Super Class,它没有表),Employee(table = EmployeeTable)和Managertable = ManagerTable)。

这里“ReportedPerson”是应该在Manager中使用的变量..ReportedPerson字段应该在Superclass中(因为我可以在这么多类中扩展这个超类)

我不想在Employee.So中使用这个变量来覆盖这个变量,我在Employee类中给出了注释@Transient BasicInfo类中的@Column(name =“ReportedPerson”)。

但是它没有用。可以帮我解决如何在Employee类中覆盖这个变量。

//Basic Info class(Super class)
@MappedSuperclass
public abstract class BasicInfo implements java.io.Serializable {       
    @Column(name = "name")
    protected String name = EMPTY_STRING;
    @Column(name = "age")
    protected Integer age = null; 
    @Column(name = "ReportedPerson")
    protected Integer ReportedPerson = null; 
}
//Employee class
@Entity
@Table (name ="EmployeeTable")
public class Employee extends BasicInfo{
    @Id
    @Column(name="EmployeeID")
    private String Id;
    @Transient
    protected Integer ReportedPerson = null; 

}
//Manager class
@Entity
@Table (name ="ManagerTable")
public class Manager extends BasicInfo{
    @Id
    @Column(name="ManagerID")
    private String Id;

}

1 个答案:

答案 0 :(得分:0)

最好的方法是再创建一个超类:

@MappedSuperclass
public abstract class BasicManagerInfo extends BasicInfo implements java.io.Serializable {       
    @Column(name = "ReportedPerson")
    protected Integer ReportedPerson = null; 
}

并从BasicManager中扩展Manager,从BasicInfo扩展Employee:

@MappedSuperclass
public abstract class BasicInfo implements java.io.Serializable {       
    @Column(name = "name")
    protected String name = EMPTY_STRING;
    @Column(name = "age")
    protected Integer age = null; 
}

//Employee class
@Entity
@Table (name ="EmployeeTable")
public class Employee extends BasicInfo{
    @Id
    @Column(name="EmployeeID")
    private String Id;
}

//Manager class
@Entity
@Table (name ="ManagerTable")
public class Manager extends BasicManagerInfo{
    @Id
    @Column(name="ManagerID")
    private String Id;

}