开发者

Hibernate different type pojo mapping

I have a problem with a mapping for this structure that i designed

public abstract class A<T> {
 private int discriminator_value = -1;
 private T value;
 //...
}

public class One extends A<String> {

 public One(){
  setDiscriminatorValue(1);
 }
 //...
}

public class Two extends A<BigDecimal> {

 public Two(){
  setDiscriminatorValue(2);
 }
 //...
}

public class Three extends A<Date> {

 public Three(){
  setDiscriminatorValue(3);
 }
 //...
}


public class TheTargetSolution {
  private Long info1;
  private Long info2;
  private Long info3;
  private A targetPojo; 
 //...  
}

The table structure

 THE_TARGET_SOLUTION_TABLE
  - INFO_1   NUMBER(10)
  - INFO_2   NUMBER(10)
  - INFO_3   NUMBER(10)
  - DISCRIM  NUMBER(2)
  - TEXT_A   NVARCHAR2(200 BYTE)
  - NUMBER_A NUMBER(10)
  - DATE_A   DATE

The main thing is that we need to have in TheTargetSolution the targetPojo that is type of A class and this class does not have mapping, (we need more info to entity and thees is no option to do this using relation many to many in this case all data has to be stored in one table) that is instance of type that apply to discriminator.

Resuming

When the DISCRIM column value is 1 the targetPojo should by type of One

When the DISCRIM column value is 2 the targetPojo should by type开发者_高级运维 of Two

When the DISCRIM column value is 3 the targetPojo should by type of Three

Any suggestions ?


This is typical scenario for inheritance, with table-per-class-hierarchy. If you use xml, see the linked docs. If using annotations, use

@MappedSuperclass
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="DISCRIM", discriminatorType=INTEGER)
public abstract class A {
   // properties here
}

And then

@Entity
@DiscriminatorValue("1")
public class Two extends A<BigDecimal>


Take a look at this: https://github.com/candybon/fasttrack it provide a solution that cooperate all the pojo into one table

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜