2

I have three classes having class hierarchy as

  • ParentClass.java

    having commons properties used for both ChildClass1 and ChildClass2.
    
  • ChildClass1 extends ParentClass

    having properties used for this ChildClass1 + it also use some of the common properties from parent class

  • ChildClass2 extends ParentClass

    having properties used for this ChildClass2 + it also use some of the common properties from parent class

This all properties are available into table with two columns

**Key               value**     Type
---------------------------------------
propertyKey1    propertyValue1   Child1
propertyKey2    propertyValue2   Child1
propertyKey3    propertyValue3   Child2
propertyKey4    propertyValue4   Child2
propertyKey5    propertyValue5   CommonPorperty
..              ..               ..
propertyKeyn    propertyValuen   ..

Now I am not sure that how to load them from hibernate inheritance ?

Apology for silly question...

Thanks in advance

Ravi Parmar
  • 1,392
  • 5
  • 24
  • 46
  • Are you talking about two kinds of properties? A property of a hibernate-entity is a java-field having a specific name but it looks like your property-name is a value in the table.... please rewrite. – Grim Dec 07 '15 at 09:43
  • Checking on descriminator... – Siva Dec 07 '15 at 09:50
  • Possible duplicate of http://stackoverflow.com/questions/16772370/when-to-use-discriminatorvalue-annotation-in-hibernate – Madushan Perera Dec 07 '15 at 12:07

1 Answers1

1

You need to put an additional column 'Discriminator' to inform Hibernate which instance should be loaded when you're working on same table with multiple types. See example:

@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Table(name = "PARENT_TABLE")
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.INTEGER)
public abstract class ParentEntity implements java.io.Serializable {

    private long id;
    private String commonValue1;

    @Id      
    @Column(name = "ID", nullable = false)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Column(name = "Common_Value1")
    public String getCommonValue1(){
        return commonValue1;
    }

    public void setCommonValue1(String commonValue1){
        this.commonValue1 = commonValue1;
    }
}


@Entity
@DiscriminatorValue("1")
public class ChildEntity1 extends ParentEntity {

    private String child1Value;

    @Column(name = "Child1_Value")
    public String getChild1Value(){
        return child1Value;
    }

    public void setChild1Value(String child1Value){
        this.child1Value = child1Value;
    }
}

@Entity
@DiscriminatorValue("2")
public class ChildEntity2 extends ParentEntity {

    private String child2Value;

    @Column(name = "Child2_Value")
    public String getChild2Value(){
        return child2Value;
    }

    public void setChild2Value(String child2Value){
        this.child2Value = child2Value;
    }
}
hsnkhrmn
  • 961
  • 7
  • 20
  • Thanks @hsnkhrmn..But there is no column named as 'Child1_Value'. and Child2_Value, then how we can use this ? Here my problem is there is no specific column regarding to child class – Ravi Parmar Dec 07 '15 at 10:09
  • The values I used are just for example, you need to change it according to your data model. Basicly you'll put common columns (and do mapping) on your Parent entity, specific columns to a class is to specific tables (Child1 or Child2) – hsnkhrmn Dec 07 '15 at 10:13
  • Yes you are right.... hsnkhrmn.. BUt here I dont have any specific column for chidl1 or child2 . Only propertykye is specific to child1 or child2. – Ravi Parmar Dec 07 '15 at 10:43
  • 1
    I see, those fields has no mapping on DB. They are just transient fields on your entitiy right? If so you need to mark their getters with @Transient to not to confuse hibernate. Child classes may or may not have additional columns, it's not necessary. Hibernate just understands it via Discriminator column (you should add if not there) – hsnkhrmn Dec 07 '15 at 10:46