views:

51

answers:

1

Hello,

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 ?

A: 

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>
Bozho
Yes, and no. The condition what is messing here is that the class A can't be mapped, and should somehow be used as component. This because the data that will be used in A are form several table (different meaning of them) And when use table-per-class hierarchy i will have to redesign all logic in database
Vash
See the `@MappedSuperclass`. I don't know the xml equivallent, but this means A does not have a serpate table. Anyway, I can't see your whole database in order to make better judgement
Bozho
This might work for annotation notation of hibernate, but i don't found any equivalent for XML, nevertheless this can be accepted. Thank You for your time and effort.
Vash