-->
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.  [ 10 posts ] 
Author Message
 Post subject: Inheritance Patterns supported in Hibernate
PostPosted: Mon Sep 06, 2004 1:55 pm 
Newbie

Joined: Mon Sep 06, 2004 1:39 pm
Posts: 18
Location: Utah, USA
Hibernate version: 2.1.latest

Mapping documents: none yet

There are a few Inheritance patterns I am familiar with for O/R mapping. Nearly all of the examples that I can find in hibernate documentation relate to Single Table Inheritance, where all fields are contained in a single table and sub-classes or inherited classes are treated as components in the mapping document.

The pattern I am interested in using is called Class Table Inheritance, which allows for greater normalization in the data model (See Explanation below).

The final alternative that I am familiar with is Concrete Table Inheritance which requires less normalization in the data model, but more than single table inheritance (since the latter leaves possibly many empty fields per record).

Can anyone provide documentation that would enable me to implement a persistence layer in Hibernate that conforms to the Class Table Inheritance pattern? What I'm asking for is the ability to map two tables to one finished object that is comprised of two classes one being abstract. I have read in the Hibernate documentation that mapping two tables to one class is poor form. If this is the case then what level of database normalization is considered good form? Thanks in advance.

[Explanation]
In my case I would like to normalize the data model to the point of having a type of Abstract table. For example:
Accounting Transactions always have a date, description, and approved status. Specific Accounting Transactions (like Orders, Receipts, Purchase Orders, etc.) have additional information related to shipping, amount tendered and expected delivery date. Due to the large number of transactions I would lilke to have a Transaction Table that holds general information about every transaction and specific transaction tables that hold information specific to that type of transaction.

My application would have an abstract Transaction class that would be a generalization of all Transactions, with a specific class for each transaction type.
[/Explanation]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 06, 2004 1:58 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://www.hibernate.org/hib_docs/refer ... tance.html

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 06, 2004 2:16 pm 
Newbie

Joined: Mon Sep 06, 2004 1:39 pm
Posts: 18
Location: Utah, USA
It would seem then that I want table-per-subclass. In my case the Transaction would be an Interface and each specific transaction would be a class. I would use a joined-subclass element for each actual transaction that I included in my application.

If I understand correctly, Hibernate will determine which of the Joined-subclasses to load automatically, without using a descriminator?

Thanks for the post...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 06, 2004 4:32 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
dwatrous wrote:
If I understand correctly, Hibernate will determine which of the Joined-subclasses to load automatically, without using a descriminator?

True, it will query each table of the hierarchy

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Need more precise help !
PostPosted: Thu Jan 06, 2005 2:17 pm 
Newbie

Joined: Thu Jan 06, 2005 1:46 pm
Posts: 8
Hi,
to continue with this topic :

I already use this form of mapping, but I need help for this situation :

I have one more table to describe the nature of the transaction (just to be consistent with the example). This is useful for grouping Transcations. For now I can navigate from Groups of transactions to all the specific Transactions of this Group. I need grouping for displaying transactions and is really useful for performance (lazy fetching).

But I have the following problem :
When I create a specific transaction(eg. Purchase), I need to know the Group Reference if I want hibernate to save my object with the right FK.

But is implicit, when you create a Purchase it can be only in a Purchase group. I'm facing a cruss-cutting concern. To create a Purchase, I need to know in which group it will be saved. And to knowing the group I need to query the database. This is mean i can't create Purchase until i'm working with persistent or detached objects.

The question is : Do you have an idea to resolve this cruss-cutting concern !

P.S. : The user can't create new group (Business Rule)


Thanks,

Louis-Michel

<hibernate-mapping package="com.agiledss.rps.core.model">

<class name="RpsObj"
table="dwa_obj">

<!-- Common id property. -->
<id name="id"
column="dwa_obj_id"
type="integer"
unsaved-value="null">

<generator class="hilo">
<param name = "table">dwa_unq_key</param>
<param name = "column">nxt_hgh</param>
</generator>
</id>

<!-- A versioned entity. -->
<version name="version"
column="dwa_obj_ver"
access="net.sf.hibernate.property.DirectPropertyAccessor"/>

<!-- Simple property. -->
<property name="name" column="dwa_obj_nam" type="string" not-null="true"/>
<property name="description"
........

<!-- Bidirectional, required as ObjectType is dwa_obj_typ_id NOT NULL. This is also
a read-only property that will never be updated. -->
<many-to-one
name="objectTypeRef"
class="RpsEntObjType"
update="false"
cascade="none"
column="dwa_obj_typ_id"/>

<joined-subclass name="RpsObjSystemValue" table="dwa_sys_val">
<key column="dwa_sys_val_id"/>

<!-- Simple property. -->
<property name="type" column="dwa_sys_val_typ_id" not-null="true" type="com.agiledss.rps.core.enum.RpsEnmSystemValueType"/>
<property name="value" column="dwa_sys_val_val" type="string"/>
</joined-subclass>

</hibernate-mapping>

<hibernate-mapping
package="com.agiledss.rps.core.model">

<class name="RpsEntObjType" table="dwa_obj_typ" lazy="true">
<id
name="id"
column="dwa_obj_typ_id"
type="integer">
<generator class="assigned"/>
</id>

<property
name="name"
column="dwa_obj_typ_nam"
type="string"/>

<set
name="rpsObjects"
inverse="true"
lazy="true"
cascade="none">

<key column="dwa_obj_typ_id"/>
<one-to-many class="RpsObj"/>
</set>
</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 06, 2005 3:21 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If I understand your pb, you can do the following
lazy="true" the Group class and do session.load(Group.class, purchaseGrouopId); the object will not be loaded for real (a proxy will be returned)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 06, 2005 3:50 pm 
Newbie

Joined: Thu Jan 06, 2005 1:46 pm
Posts: 8
But, this mean you tie the Business Model to the Persistence Layer and I want to advoid that. Do You think is possible ?

Thanks,
Louis-Michel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 06, 2005 3:56 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
mathurin.lm wrote:
But, this mean you tie the Business Model to the Persistence Layer

I don't think so, why?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 06, 2005 4:17 pm 
Newbie

Joined: Thu Jan 06, 2005 1:46 pm
Posts: 8
Imagine, I want to create a new Purchase. In pure java code I would expect to do :
Purchase purchase = new Purchase("Something");

SessionService sessionService = getSessionService();

sessionService.beginTransaction();
sessionService.makePersistent(purchase);
sessionService.commitTransaction();

In this case hibernate don't know the FK to the TransactionType Table.


In this case my object in completively independant the persistence layer, but for now I doing this


SessionService sessionService = getSessionService();

sessionService.beginTransaction();

Purchase purchase = new Purchase("Something",
sessionService.load(Group.class, purchase.getPurchaseGroupId);

sessionService.makePersistent(purchase);
sessionService.commitTransaction();

Louis-Michel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 07, 2005 7:32 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
OK,
So try this one
TransactionType tx = new TransactionType();
tx.setId(MY_GROUP_TX_ID);
then purchase.setTxType(tx); //manually or hidden in a setter

But beware not to cascade the save-update to tx type from purchase

_________________
Emmanuel


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