2

I got this entities:

@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
}

When I try to get the kpis list from ProcessVersion I always get 0. I check the sql generated by hibernate and I see this sentece wich is very 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=?;

The question is why the sentece has 3 inner with stage..Two from KpiTemporary and One from KpiCounter the problem is this will never have the 3 values because the kpi is temporary or is counter but not both.

Can anyone tell me something about?. Is there any extra config to apply. I am using JPA 2.1 specification with hinerbate 4.3.5.Final

adrian.riobo
  • 495
  • 5
  • 11

1 Answers1

1

The problem is that you have 3 mappings of:

@ManyToOne(optional = false)

On both counter and temporary. The false means that the mapping is 1..M -> 1, while true would mean the mapping is 0..M ->1, I think you would want the latter. Change those to true and see if you get the sql you desire.

Jeff Wang
  • 1,837
  • 1
  • 15
  • 29