-->
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.  [ 6 posts ] 
Author Message
 Post subject: Combination of inheritance strategies
PostPosted: Wed Apr 15, 2009 11:01 am 
Newbie

Joined: Wed Apr 15, 2009 10:45 am
Posts: 4
Hi!

I have the problem with combination of inheritance strategies in Hibernate. My class hierarchy is as follows:
Code:
    A
    |
    B
    |
    C
   /|\
  / | \
C1 C2 C3

Could you please explain is it possible (and how this may be achieved) to use separate tables (Joined Subclass Strategy) for classes A and B, but single table (Single Table per Class Hierarchy Strategy) for classes C, C1, C2 and C3? If yes, is it possible to place the descriminant in class B?

Thank you in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 15, 2009 12:48 pm 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Try This


Code:
<class name="A" table="a">
..............
..............
   <joined-subclass name="B" table="b">
   <key column="b_primary_key">
   ................
   ................

      <joined-subclas name="C" table ="c">
         <key column="c_primary_key">
         <discriminator column="column_name" type="string"/><!-- can use any other type also-->
         ...................
         ...................
         <subclass name="C1" discriminator-value="c1">
         ...................
         ...................
         </subclass>

         <subclass name="C2" discriminator-value="c2">
         ...................
         ...................
         </subclass>

         <subclass name="C3" discriminator-value="c3">
         ...................
         ...................
         </subclass>
      </joined-subclass>
   </joined-subclass>
</class>

</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 2:59 am 
Newbie

Joined: Wed Apr 15, 2009 10:45 am
Posts: 4
Thanks for your help, parmendratyagi!

But it seems to me that provided config is invalid. In accordance with hibernate-mapping-3.0.dtd joined-subclass can't contain neither discriminator nor subclass elements:
Code:
<!ELEMENT joined-subclass (
   meta*,
   subselect?,
   synchronize*,
   comment?,
    tuplizer*,
   key,
   (property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,
   joined-subclass*,
   loader?,sql-insert?,sql-update?,sql-delete?,
    resultset*,
   (query|sql-query)*
)>
   <!ATTLIST joined-subclass entity-name CDATA #IMPLIED>
   <!ATTLIST joined-subclass name CDATA #IMPLIED>
   <!ATTLIST joined-subclass proxy CDATA #IMPLIED>                   
   <!ATTLIST joined-subclass table CDATA #IMPLIED>                   
   <!ATTLIST joined-subclass schema CDATA #IMPLIED>
   <!ATTLIST joined-subclass catalog CDATA #IMPLIED>
   <!ATTLIST joined-subclass subselect CDATA #IMPLIED>
   <!ATTLIST joined-subclass dynamic-update (true|false) "false">
   <!ATTLIST joined-subclass dynamic-insert (true|false) "false">
   <!ATTLIST joined-subclass select-before-update (true|false) "false">
   <!ATTLIST joined-subclass extends CDATA #IMPLIED>                
   <!ATTLIST joined-subclass lazy (true|false) #IMPLIED>
   <!ATTLIST joined-subclass abstract (true|false) #IMPLIED>
   <!ATTLIST joined-subclass persister CDATA #IMPLIED>
   <!ATTLIST joined-subclass check CDATA #IMPLIED>                   
   <!ATTLIST joined-subclass batch-size CDATA #IMPLIED>
   <!ATTLIST joined-subclass node CDATA #IMPLIED>

and only class element can contain discriminator.

Could you please correct me if I'm wrong?

Thank you in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 4:19 am 
Newbie

Joined: Wed Apr 15, 2009 10:45 am
Posts: 4
Well, to be more concrete I've wrote the following simple code:
Code:
public class Discriminator {
    public Discriminator() {}
    public Discriminator(Long id) {this.id = id;}
    private Long id;
}

public class ClassA {
    private Long id;
    public String aName;
}

public class ClassB extends ClassA {
    public String bName;
    public Discriminator discr;
}

public class ClassC extends ClassB {
    public String cName;
}

public class ClassC1 extends ClassC {
    public String c1Name;
}

public class ClassC2 extends ClassC {
    public String c2Name;
}

public class ClassC3 extends ClassC {
    public String c3Name;
}


and the corresponding mapping:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.springsteel.eden.simple" default-access="field">
    <class  name="Discriminator"
            table="DISCRIMINATOR">
        <id name="id"
            column="DISCRIMINATOR_ID"
            type="long">
        </id>
    </class>

    <class name="ClassA"
           table="CLASS_A">
        <id name="id" column="ID" type="long">
            <generator class="native"/>
        </id>
        <discriminator column="DISCRIMINATOR_ID" insert="false" formula="1"/> <!-- les us assume that correct formula will be used -->
        <property name="aName" column="A_NAME"/>
    </class>

    <joined-subclass name="ClassB"
                     table="CLASS_B" extends="ClassA">
        <key column="ID"/>
        <property name="bName" column="B_NAME"/>
        <many-to-one column="DISCRIMINATOR_ID"
                     name="discr"
                     cascade="all"
                     class="Discriminator"/>
    </joined-subclass>

    <joined-subclass name="ClassC" extends="ClassB"
                     table="CLASS_C">
        <key column="ID"/>
        <property name="cName" column="C_NAME"/>
    </joined-subclass>

    <subclass name="ClassC1" extends="ClassC" discriminator-value="100">
        <property name="c1Name" column="C1_NAME"/>
    </subclass>

    <subclass name="ClassC2" extends="ClassC" discriminator-value="200">
        <property name="c2Name" column="C2_NAME"/>
    </subclass>

    <subclass name="ClassC3" extends="ClassC" discriminator-value="300">       
        <property name="c3Name" column="C3_NAME"/>
    </subclass>

</hibernate-mapping>


After this execution of the following code fails (les us assume that session is a valid hibernate session):
Code:
List<Discriminator> types = new ArrayList<Discriminator>();
types.add(new Discriminator(100L));
types.add(new Discriminator(200L));
types.add(new Discriminator(300L));

session.save(types.get(0));
session.save(types.get(1));
session.save(types.get(2));
       
ClassC1 c1 = new ClassC1();

c1.discr = types.get(0);
c1.aName = "AAA";
c1.bName = "BBB";
c1.cName = "CCC";
c1.c1Name = "C1C1C1";

session.save(c1);


with the following trace (MySQL 5.0.27 is used):
Code:
Hibernate: insert into DISCRIMINATOR (DISCRIMINATOR_ID) values (?)
Hibernate: insert into DISCRIMINATOR (DISCRIMINATOR_ID) values (?)
Hibernate: insert into DISCRIMINATOR (DISCRIMINATOR_ID) values (?)
Hibernate: insert into CLASS_A (A_NAME) values (?)
Hibernate: insert into CLASS_B (B_NAME, DISCRIMINATOR_ID, ID) values (?, ?, ?)
Hibernate: insert into CLASS_C (C_NAME, C1_NAME, ID) values (?, ?, ?)
Hibernate: insert into CLASS_C (ID) values (?)
2009-04-16 12:09:29,952 WARN org.hibernate.util.JDBCExceptionReporter :: SQL Error: 1062, SQLState: 23000
2009-04-16 12:09:29,952 ERROR org.hibernate.util.JDBCExceptionReporter :: Duplicate entry '1' for key 1
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not insert: [com.springsteel.eden.simple.ClassC1]


At the same time exported schema looks well: there are two tables for A and B, B contains the discriminator id and Cx subhierarchy is persisted into single table CLASS_C.

Could you please explain why Hibernate writes ClassC with same id twice?

Thank you in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 4:20 am 
Newbie

Joined: Wed Apr 15, 2009 10:45 am
Posts: 4
BTW, is there way specify discriminator without formula or other hacks?


Top
 Profile  
 
 Post subject: Re: Combination of inheritance strategies
PostPosted: Thu Feb 25, 2010 9:06 pm 
Newbie

Joined: Thu Feb 25, 2010 9:03 pm
Posts: 1
I'm facing the same issue and trying to do the similar inheritance modelling. Can you please post if you are successful in solving this problem.


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