-->
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.  [ 9 posts ] 
Author Message
 Post subject: [INTERESTING] How to map a multilevel inheritanche hierarchy
PostPosted: Sat Oct 06, 2007 9:47 am 
Newbie

Joined: Sat Oct 06, 2007 9:44 am
Posts: 3
Hi!
I would like to know how to map a multilevel inheritance hieararchy.
I have:

1) Superclass A
2) Class B,C extends A
3) Class D,E extends B

In another mapping, I have a many-to-one relationship with class B.
How can I map this?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 06, 2007 6:12 pm 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
Where is the problem?

Pavol


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 06, 2007 7:46 pm 
Newbie

Joined: Sat Oct 06, 2007 9:44 am
Posts: 3
Hi Pavol.
There is no problem if I use a table per subclass strategy, with joined-subclass. But there is a legacy mapping for the first level of hierarchy using subclass with discriminator.
Now, if the discriminator for Class B is "CLASSB" and the discriminators for class D and E are "CLASSD" and "CLASSE", can I do polymorphic queries? Can I query for Class B objects and get both Class D and Class E objects?
This is the problem. I can't change the original mapping.

Mario


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 06, 2007 7:55 pm 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
HI!

Yes, I think the polymorphic query will work, unless you will specify in entity polymorphicType explicit.

Pavol


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 07, 2007 5:40 am 
Newbie

Joined: Sat Oct 06, 2007 9:44 am
Posts: 3
Thank you, I will try!


Top
 Profile  
 
 Post subject: Multi level inheritance table per class hieararchy
PostPosted: Thu Sep 25, 2008 3:21 am 
Beginner
Beginner

Joined: Wed Dec 06, 2006 6:24 am
Posts: 24
Location: Bangalore
Hi,

I have similar problem with multi level inheritance:

I have table like:

Code:
Table: Test_Comments
comment_id varchar2(15),
entity_id varchar2(15),
entity_type_cd varchar2(32),
field_cd varchar2(32),
field_value varchar2(2000).


Here we moved all comments to a separate table for all remaining entities.
entit_id is the corresponsind entity for which this comment belongs to and the entity_type_cd is the discriminator for distinguishing the type of entity and field_cd is the discriminator within the entity to distinguish the field.

so the class hierarchy is :

Code:
Class TestComments{
String commendId;
String fieldValue;

}

(entity_type_cd is the discriminator value=XYZ)
Class XyzTestComment extends TestComments{
Xyz entityId;
}
(field_cd is the discriminator value=FIELD1)
Class XyzField1TestComment extends XyzTestComment{
}
(field_cd is the discriminator value=FIELD2)
Class XyzField2TestComment extends XyzTestComment{
}
(entity_type_cd is the discriminator value=XXX)
Class XxxTestComment extends TestComments{
Xxx entityId;
}
(field_cd is the discriminator value=FIELD)
Class XxxFieldTestComment extends XxxTestComment{
}


Mapping file i'm trying to write is:
Code:

<hibernate-mapping>
    <class name="com.model.TestComments" table="testComments" schema="GL_PA_NEW_TEST">
        <id name="commentId" type="string">
            <column name="commentId" length="15" />

            <generator class="sequence">
               <param name="sequence">GL_PA_SEQ</param>
            </generator>
        </id>
       
        <discriminator force="true" type="string">
           <column name="entity_type_cd" length="20" />
        </discriminator>
       
   
        <subclass name="com.model.XyzTestComment" discriminator-value="XYZ">

how can I subclass it further based on field_cd, i am unable to write <discriminator > here
Code:
           <many-to-one name="entityId" class="com.model.Xyz">
         <column name="entityId" length="15" not-null="true" />
      </many-to-one>
       
        </subclass>
................... same problem with XXX discriminator value.
       
    </class>
</hibernate-mapping>



Please suggest me how to map this relation.. please tell me what is polymorphic query where can i get more information on that.

_________________
Vinay N


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 28, 2008 1:15 pm 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
This can be a bitch :) However, if you need to have the deeper specialization's utilize two columns rather than one, you may want to look at using a formula for the discriminator rather than a single column. This will allow you to create a SQL fragment that you can use to determine when the subclass is used.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2008 8:40 am 
Beginner
Beginner

Joined: Wed Dec 06, 2006 6:24 am
Posts: 24
Location: Bangalore
Thanks Ryan for your reply, I have removed multiple discriminators and concatenated them at the column level to solve this problem. However still i've a doubt how to use nested <subclass> will that be used to solve multilevel inheritance problem? If you have any resources to make use of nested <subclass> please share with me.

Thanks in advance.

_________________
Vinay N


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2008 6:12 am 
Beginner
Beginner

Joined: Wed Dec 06, 2006 6:24 am
Posts: 24
Location: Bangalore
Hi,

I have few doubts on inheritance.

Code:

ClassA {

String id;
List<ClassB> childs;
ClassAComment classAcomment;

}

ClassB{
String id;
ClassBComment classBcomment;
}

I would like to store the field values which are having >500 size in TestComments.


TestComment:
Code:
COMMENT_ID          PK
PARENT_ENTITY_ID not null
ENTITY_ID              not null
ENTITY_FIELD_CD   not null
fieldValue


at the database level i don't have any foriegn key constraint on parentId and entityId, but i am trying to enforce them at hibernate mapping level like:

Code:

ClassA mapping:

<class name="ClassA" table="TableA">
      <id name="id" type="string">
         <column name="TABLEA_ID" length="15" />

         <generator class="sequence">
            <param name="sequence">TEST_SEQ</param>
         </generator>
      </id>
      
      <one-to-one name="classAcomment"
         class="ClassAComment"
         cascade="all" property-ref="entityId" />
         
       <bag name="childs" inverse="true" cascade="all">
            <key>
                <column name="TABLEA_ID" length="15" not-null="true" />
            </key>

            <one-to-many class="ClassB" />
        </bag>
      
</class>

ClassB mapping:

<class name="ClassB" table="TableB">
      <id name="id" type="string">
         <column name="TABLEB_ID" length="15" />

         <generator class="sequence">
            <param name="sequence">TEST_SEQ</param>
         </generator>
      </id>
      
      <one-to-one name="classBcomment"
         class="ClassABomment"
         cascade="all" property-ref="entityId" />
         
       <many-to-one name="classA" class="ClassA" fetch="select">
            <column name="TABLEA_ID" length="15" not-null="true" />
        </many-to-one>
      
</class>

TestComments mapping:
      
<class name="TestComment"
      table="O_TEST_COMMENT">
      <id name="commentId " type="string">
         <column name="COMMENT_ID" length="15" />

         <generator class="sequence">
            <param name="sequence">TEST_SEQ</param>
         </generator>
      </id>

      <discriminator force="true" type="string">
         <column name="ENTITY_FIELD_CD" length="32" not-null="true" />
      </discriminator>

<subclass name="ClassAComment"
         discriminator-value="CLASSA_COMMENT">

   
         <many-to-one name="parentEntityId"
            class="ClassA" lazy="false">
            <column name="PARENT_ENTITY_ID" length="15"
               not-null="true" />               
         </many-to-one>
         
         <many-to-one name="entityId"
            class="ClassA" lazy="false">
            <column name="ENTITY_ID" length="15"
               not-null="true" />               
         </many-to-one>         
         
      </subclass>

      <subclass
         name="ClassBComment"
         discriminator-value="CLASSB_COMMENT">
         
         <many-to-one name="parentEntityId"
            class="ClassA" lazy="false">
            <column name="PARENT_ENTITY_ID" length="15"
               not-null="true" />
         </many-to-one>
         
         <many-to-one name="entityId"
            class="ClassB" lazy="false">
            <column name="ENTITY_ID" length="15"
               not-null="true" />               
         </many-to-one>
      </subclass>

.......
</class>


I am duplicating one selected ClassA object along with its child by setting Id to null and tried merge on ClassA, the queries generated in the following order:

first insert on ClassA
insert on ClassAComment
insert on ClassBComment (here i am getting exception)

Hibernate is trying to insert child(ClassBComment) first before Parent(ClassB), is there any way to tell hibernate to insert parent first.

caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.test.domain.ClassBComment.entityId
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:334)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:693)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
... 120 more

_________________
Vinay N


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