-->
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.  [ 3 posts ] 
Author Message
 Post subject: fun with discriminator and inheritance
PostPosted: Thu Sep 01, 2005 11:00 am 
Newbie

Joined: Wed Aug 24, 2005 6:35 am
Posts: 4
Location: Austria
Hi!

After several hours of trail and error I hope that maybe someone of you can help me with my problem.
I use these simplified mapping files:

father.hbm.xml
Code:
<hibernate-mapping auto-import="true">
  <class name="Father" abstract="true">
    <id name="id" type="long">
      <generator class="native" />
    </id>
    <discriminator type="string" column="discr" />
    <property type="string" name="fathersName" />
  </class>
</hibernate-mapping>

brother.hbm.xml
Code:
<hibernate-mapping auto-import="true">
  <union-subclass name="Brother" extends="Father" abstract="true">
    <property type="string" name="brothersName" />
  </union-subclass>
</hibernate-mapping>

sister.hbm.xml
Code:
<hibernate-mapping auto-import="true">
  <subclass name="Sister" extends="Father" discriminator-value="sister">
    <property type="string" name="sistersName" />
  </subclass>
</hibernate-mapping>

and i want to generate with hbm2java this code:

father.java
Code:
public class Father  implements java.io.Serializable {
    private Long id;
    private String fathersName;   

    public Father() {
    }
    public Father(Long id) {
        this.id = id;
    }   
    public Long getId() {
        return this.id;
    }   
    public void setId(Long id) {
        this.id = id;
    }
    public String getFathersName() {
        return this.fathersName;
    }   
    public void setFathersName(String fathersName) {
        this.fathersName = fathersName;
    }
}

brother.java
Code:
public class Brother extends Father implements java.io.Serializable {
    private String brothersName;

    public Brother() {
    } 
    public String getBrothersName() {
        return this.brothersName;
    }   
    public void setBrothersName(String brothersName) {
        this.brothersName = brothersName;
    }
}

sister.java
Code:
public class Sister extends Father implements java.io.Serializable {

    private String sistersName;

    public Sister() {
    }
    public String getSistersName() {
        return this.sistersName;
    }   
    public void setSistersName(String sistersName) {
        this.sistersName = sistersName;
    }
}


The result of hbm2ddl should look like this,
Code:
create table Brother (
    id int8 not null,
    fathersName varchar(255),
    brothersName varchar(255),
    primary key (id)
);
create table Father (
    id int8 not null,
    discr varchar(255) not null,
    fathersName varchar(255),
    sistersName varchar(255),
    primary key (id)
);

and that's what I get:
Code:
create table Brother (
    id int8 not null,
    discr varchar(255) not null,
    fathersName varchar(255),
    sistersName varchar(255),
    brothersName varchar(255),
    primary key (id)
);
create table Father (
    id int8 not null,
    discr varchar(255) not null,
    fathersName varchar(255),
    sistersName varchar(255),
    primary key (id)
);

The classes are generated correct.
But, how can i achieve that the tables Father and Brother are generated the way i need them (no column "sistersName" in table Brother) and how, if its even possible, can i get rid of the "discr" column in the brother table?

cheers,
gekko

Hibernate version: 3


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 11:06 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Hibernate does not support mixing <subclass> and <union-subclass> in a single hierarchy.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 4:33 am 
Newbie

Joined: Wed Aug 24, 2005 6:35 am
Posts: 4
Location: Austria
Hi Gavin!

Thank's for your prompt response, but i am not really stisfied with your answer. Is there any "deeper" reason why hibernate dosen't support it? I want to use the mapping information for both, generating the java classes and the ER schema. I thought i can use the mapping files to become the central point of development and adding new entities just results in applying changes to the mapping files.
I wonder whether, in this particular case, it Would be a practicable solution if I put a dummy <subclass> mapping between brother and father? How do you solve such problems?

tia,
gekko


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.