-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Inheritance mix in Hibernate 3.2.0 CR1 (annotations)
PostPosted: Fri Sep 22, 2006 6:46 am 
Newbie

Joined: Mon Mar 13, 2006 3:12 pm
Posts: 2
Hi,

I need help with Hibernate 3.2. CR1 and I hope that you can help me.
I need to implement following inheritance tree using Hibernate annotation:

Code:
(1) PersistentData::PeriodDependentMasterData::NetRegion
(2) PersistentData::PeriodDependentMasterData::Carrier::ExternalCarrier
(3) PersistentData::PeriodDependentMasterData::Carrier::UICCarrier


For (1) inheritance leg I'm using the InheritanceType.JOINED strategy.
For the legs (2) and (3), starting from Carrier class, I like to use the
InheritanceType.SINGLE_TABLE strategy. See following section:

Code:
@MappedSuperclass
abstract class PersistentData implements Serializable {
}

@Entity
@Table(name="PERIODDEPENDENDMASTERDATA")
@Inheritance(strategy=InheritanceType.JOINED)
abstract class PeriodDependentMasterData extends PersistentData {
}

@Entity
@Table(name="NETREGION")
@PrimaryKeyJoinColumn(name="ID")
public class NetRegion extends PeriodDependentMasterData {
}

@Entity
@Table(name="CARRIER")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="MAPPINGTYPE_ID",discriminatorType=DiscriminatorType.INTEGER)
@DiscriminatorValue("10")
public abstract class Carrier extends PeriodDependentMasterData {
}

@Entity
@DiscriminatorValue("11")
public class ExternalCarrier extends Carrier {
}

@Entity
@DiscriminatorValue("13")
public class UICCarrier extends Carrier {
}


Because this code doesn't work for me I like to ask you following questions:
    1. Can I mix InheritanceType.SINGLE_TABLE and InheritanceType.JOINED strategies?
    2. If yes then what is wrong with my code?
    3. Where can I find tutorials or sample code for inheritance strategy minxing? I found only following page: http://www.hibernate.org/hib_docs/v3/re ... tance.html


Regards
Christoph


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 23, 2006 9:45 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
Well just use SINGLE_TABLE strategy, and then use SecondaryTables for your child entities:

Code:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(schema = "scott", name = "tb_animal")
@SequenceGenerator(name = SimpleDomainObject.SEQUENCE_ALIAS, allocationSize = 1, sequenceName = "scott.sq_animal")
@DiscriminatorColumn(name = "brand")
public abstract class Animal extends SimpleDomainObject {
    @ManyToOne
    @JoinColumn(name = "fk_anm_mate")
    private Animal mate;

    public Animal getMate() {
        return mate;
    }

    public void setMate(Animal mate) {
        this.mate = mate;
    }
}


Code:
@Entity
@DiscriminatorValue("cat")
@SecondaryTable(schema = "scott", name = "tb_cat",
        pkJoinColumns = {@PrimaryKeyJoinColumn(name = "pk_animal", referencedColumnName = "id")})
public class Cat extends Animal {
    @Column(name = "color", table = "tb_cat")
    private String color;
    @Column(name = "name", table = "tb_cat")
    private String name;
    @Column(name = "birth_date", table = "tb_cat")
    private Date birthDate;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }
}


Code:
public class Dog extends Animal {
    @Column(name = "race", table = "tb_dog")
    private String race;

    @Column(name = "weight", table = "tb_dog")
    private Float weight;

    public String getRace() {
        return race;
    }

    public void setRace(String race) {
        this.race = race;
    }

    public Float getWeight() {
        return weight;
    }

    public void setWeight(Float weight) {
        this.weight = weight;
    }
}

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.