如何在JOINED继承中查询jpa实体,而不创建相同的表?

时间:2017-06-27 04:04:04

标签: java hibernate jpa inheritance

我正在尝试使用hibernate Joined strategy来实现继承层次结构,如下图所示。

由于Joined策略为实体创建表而不管实体类是Concrete还是Abstract。

我不想为" CompanyEmployee"创建单独的表格。所以我将它声明为映射的超类,但我应该能够以多态方式查询这个类/子类。

由于它是映射超类,我不能这样做,如果我将它声明为实体,它将创建我想要避免的表。

那么,有什么方法可以实现这个目标吗?我正在考虑混合继承,但从下面引用它似乎不是一个好的解决方案。

  

混合继承

     

我们应该从一节中说明将继承类型混合在一起的做法开始本节   继承层次结构目前在规范之外。我们包括它因为它既有用   而且很有意思,但是我们正在提供一个警告,它可能无法依赖这种行为,   即使您的供应商支持它。

Inheritance hierarchy

@Entity
@Table(name="j_employee")
@Inheritance(strategy=InheritanceType.JOINED) 
@DiscriminatorColumn(name="emp_type", discriminatorType=DiscriminatorType.STRING)
public abstract class JEmployee extends AuditLog implements Serializable {

@Id
@Basic
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="employee_id")
private Integer employeeId;

@Basic
@Temporal(TemporalType.DATE)
@Column(name="join_date")
private Date joinDate;

@OneToOne
@JoinColumn(name="person_id", nullable=false)
private Person person;

@Column(name="emp_type", updatable=false, insertable=false)
private String empType;


//Getters and Setters
}




@Entity
@Table(name="j_contract_employee")
@DiscriminatorValue(value="JContractEmployee")
@PrimaryKeyJoinColumn(name="contract_employee_id", referencedColumnName="employee_id")
public class JContractEmployee extends JEmployee implements Serializable {

@Basic
@Column(name="daily_rate")
private Integer dailyRate;

@Basic
@Column(name="term")
private Integer term;

//Getters and Setters
}


//Don't want to create table for this class, but I should able to query this clas/subclasses in polymorphic way
@MappedSuperclass
public abstract class JCompanyEmployee extends JEmployee implements Serializable {

@Basic
@Column(name="vacation")
private Integer vacation;

//Getters and Setters
public Integer getVacation() {
    return vacation;
}

public void setVacation(Integer vacation) {
    this.vacation = vacation;
}
}



@Entity
@Table(name="j_part_time_employee")
@Access(AccessType.FIELD)
@DiscriminatorValue(value="JPartTimeEmployee")
@PrimaryKeyJoinColumn(name="part_time_employee_id", referencedColumnName="employee_id")
public class JPartTimeEmployee extends JCompanyEmployee implements Serializable {

@Basic
@Column(name="hourly_rate")
private Integer hourlyRate;

//Getters and Setters 
}



@Entity
@Table(name="j_full_time_employee")
@Access(AccessType.FIELD)
@DiscriminatorValue(value="JFullTimeEmployee")
public class JFullTimeEmployee extends JCompanyEmployee implements Serializable {

@Basic
@Column(name="salary")
private Integer salary;

@Basic
@Column(name="penion")
private Integer pension;

//Getters and Setter
}

0 个答案:

没有答案