2014-09-05 135 views
2

我得到這個實體:繼承JPA 2.1

@Entity 
@Table(name = "PROCESS_VERSION") 
public class ProcessVersion implements Serializable { 
    private static final long serialVersionUID = 1L; 
    private static final String SEQ_NAME = "PROCESS_VERSION_id_SEQ"; 
    @Id 
    @Basic(optional = false) 
    @NotNull 
    @SequenceGenerator(name = "seq", sequenceName = SEQ_NAME, allocationSize = 1) 
    @GeneratedValue(generator = "seq", strategy = GenerationType.SEQUENCE) 
    @Column(name = "ID") 
    private BigDecimal id; 
    @OneToMany(mappedBy = "idProcessVersion", cascade = CascadeType.ALL) 
    private List<Stage> stageList; 
    @OneToMany(mappedBy = "idProcessVersion", cascade = CascadeType.ALL) 
    private List<Kpi> kpiList; 

    //Getters and Setters 
} 

@Entity 
@Table(name = "KPI") 
@Inheritance(strategy=InheritanceType.JOINED) 
@DiscriminatorColumn(
name="ID_CATEGORY", discriminatorType=DiscriminatorType.INTEGER) 
public class Kpi implements Serializable { 
    private static final long serialVersionUID = 1L; 
    private static final String SEQ_NAME = "KPI_id_SEQ"; 
    @Id 
    @Basic(optional = false) 
    @NotNull 
    @SequenceGenerator(name = "seq", sequenceName = SEQ_NAME, allocationSize = 1) 
    @GeneratedValue(generator = "seq", strategy = GenerationType.SEQUENCE) 
    @Column(name = "ID") 
    private BigDecimal id; 
    @JoinColumn(name = "ID_PROCESS_VERSION", referencedColumnName = "ID") 
    @ManyToOne(optional = false) 
    private ProcessVersion idProcessVersion; 
    @JoinColumn(name = "ID_CATEGORY", referencedColumnName = "ID", insertable = false, updatable =  false) 
    @ManyToOne(optional = false) 
    private KpiCategory idCategory; 

    //Getters and Setters 
} 

@Entity 
@Table(name = "KPI_TEMPORARY") 
@DiscriminatorValue("1") 
public class KpiTemporary extends Kpi implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @JoinColumn(name = "ID_STAGE_SOURCE", referencedColumnName = "ID") 
    @ManyToOne(optional = false) 
    private Stage idStageSource; 
    @JoinColumn(name = "ID_STAGE_TARGET", referencedColumnName = "ID") 
    @ManyToOne(optional = false) 
    private Stage idStageTarget; 

    //Getters and Setters 
} 

@Entity 
@Table(name = "KPI_COUNTER") 
@Inheritance(strategy=InheritanceType.JOINED) 
@DiscriminatorValue(KpiCategory.CATEGORY_COUNTER) 
public class KpiCounter extends Kpi implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @JoinColumn(name = "ID_STAGE", referencedColumnName = "ID") 
    @ManyToOne(optional = false) 
    private Stage idStage; 

    //Getters and Setters 
} 

當我試圖從ProcessVersion KPI的名單,我總是得到0。我檢查由Hibernate生成的SQL,我看到這個sentece至極非常strage:

select kpilist0_.ID_PROCESS_VERSION as ID_PROCESS_VERSION5_14_0_, kpilist0_.ID as ID2_5_0_, 
kpilist0_.ID as ID2_5_1_, kpilist0_.DESCRIPTION as DESCRIPTION3_5_1_, 
kpilist0_.ID_CATEGORY as ID_CATEGORY1_5_1_, kpilist0_.ID_PROCESS_VERSION as  ID_PROCESS_VERSION5_5_1_, 
kpilist0_.NAME as NAME4_5_1_, 
kpilist0_2_.ID_STAGE as ID_STAGE2_7_1_, 
kpilist0_3_.ID_STAGE_SOURCE as ID_STAGE_SOURCE2_9_1_, kpilist0_3_.ID_STAGE_TARGET as ID_STAGE_TARGET3_9_1_, 
kpicategor1_.ID as ID1_6_2_, kpicategor1_.NAME as NAME2_6_2_, 
stage2_.ID as ID1_15_3_, stage2_.DESCRIPTION as DESCRIPTION2_15_3_, 
stage2_.ID_PROCESS_VERSION as ID_PROCESS_VERSION5_15_3_, 
stage2_.ID_RESPONSABLY as ID_RESPONSABLY6_15_3_, stage2_.ID_STAGE as ID_STAGE3_15_3_, 
stage2_.NAME as NAME4_15_3_, processver3_.ID as ID1_14_4_, 
stage10_.ID as ID1_15_11_, stage10_.DESCRIPTION as DESCRIPTION2_15_11_, 
stage10_.ID_PROCESS_VERSION as ID_PROCESS_VERSION5_15_11_, stage10_.ID_RESPONSABLY as ID_RESPONSABLY6_15_11_, 
stage10_.ID_STAGE as ID_STAGE3_15_11_, stage10_.NAME as NAME4_15_11_, stage11_.ID as ID1_15_12_, 
stage11_.DESCRIPTION as DESCRIPTION2_15_12_, stage11_.ID_PROCESS_VERSION as ID_PROCESS_VERSION5_15_12_, 
stage11_.ID_RESPONSABLY as ID_RESPONSABLY6_15_12_, stage11_.ID_STAGE as ID_STAGE3_15_12_, 
stage11_.NAME as NAME4_15_12_ 

from KPI kpilist0_ 
left outer join KPI_COUNTER kpilist0_2_ on kpilist0_.ID=kpilist0_2_.ID 
left outer join KPI_TEMPORARY kpilist0_3_ on kpilist0_.ID=kpilist0_3_.ID 
inner join KPI_CATEGORY kpicategor1_ on kpilist0_.ID_CATEGORY=kpicategor1_.ID 
inner join STAGE stage2_ on kpilist0_3_.ID_STAGE_SOURCE=stage2_.ID 
left outer join PROCESS_VERSION processver3_ on stage2_.ID_PROCESS_VERSION=processver3_.ID 
inner join STAGE stage10_ on kpilist0_3_.ID_STAGE_TARGET=stage10_.ID 
inner join STAGE stage11_ on kpilist0_2_.ID_STAGE=stage11_.ID 

where kpilist0_.ID_PROCESS_VERSION=?; 

的問題是,爲什麼sentece具有KpiTemporary 3內與stage..Two,一個來自KpiCounter問題是,這將永遠不會有3個值,因爲KPI是臨時的或違反,但不能同時使用。

任何人都可以告訴我一些關於?是否有任何額外的配置適用。我使用JPA 2.1規範與hinerbate 4.3.5.Final

回答

1

的問題是,你有3個映射:

@ManyToOne(optional = false) 

在這兩個櫃檯和暫時的。錯誤意味着映射是1..M - > 1,而真實意味着映射是0..M - > 1,我想你會想要後者。改變這些爲真,看看你是否得到你想要的SQL。

+0

謝謝你這個工作 – 2014-09-18 14:26:48