Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Confusion over inheritance
PostPosted: Sun Feb 14, 2010 8:06 pm 
Newbie

Joined: Tue Feb 02, 2010 8:44 pm
Posts: 6
Hi team,
I am new to Hibernate so please execuse my ignorance. I read so many articles about inheritance but couldn't find a clear answer to the following question.

For using SINGLE_TABLE inheritancer strategy I have to have a special field (DTYPE) in my table to store the class type, is this correct? This sounds like changing a DB design to serve an ORM requirements?

If this is so, say I have one table called MEMO and two classes; MemoHeader which is extended by another class MemoDetails which extends MemoHeader. All my Memo information are saved using instances of MemoDetails. I guess using the SINGLE_TABLE strategy will insert a value - say "details" - in the discriminator column, so:
Q 1- If I want to get a List<MemoHeader> object from my MEMO table will I be able to do that when all the records were saved using MemoDetails entity class, or Hibernate will understand how to get back those records typed "details" but wraps them up as MemoHeader objects?

Q 2- Will it still work the other way around? I mean, save MemoHeader as "header" and get it back as a List<MemoDetails>?

Thanks for any clarification.

Daniel,


Top
 Profile  
 
 Post subject: Re: Confusion over inheritance
PostPosted: Mon Feb 15, 2010 6:28 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 988
Quote:
This sounds like changing a DB design to serve an ORM requirements?


In a typical pure relational schema,
you probably would have a boolean or small-int field telling whether the record is a "header" or a "detail".
So in fact also on pure relational schemes, certain fields have the function of a discriminator.
I you don't want discriminator fields, then choose another inheritance strategy.

Q1: session.createQuery("from MemoHeader").list()
will return you also all "details" but they will be returned as concrete MemoDetails instances.
(you can cast them to MemoHeader, but anyway "details" will be returned as MemoDetails instances)

Q2: session.createQuery("from MemoDetails").list();
will return you zero objects

Code:
select  memo0_.*   from   Memo memo0_    where      memo0_.DTYPE='Details'


N.B.: In this case (subclass) the discriminator column is implicitly included in the where condition,
indipendently from forcing discriminator or not (@org.hibernate.annotations.ForceDiscriminator)


Top
 Profile  
 
 Post subject: Re: Confusion over inheritance
PostPosted: Fri Feb 19, 2010 1:02 am 
Newbie

Joined: Mon Feb 15, 2010 2:39 am
Posts: 4
I have two fields of an entity class which I don't want to be unique but to instead be used as composite fields for a key which must itself be unique. For example I have two fields (name and version) which can be the same for other records but together they must be unique. What is the best way to do that using Hibernate (with annotations)? I am using Hibernate Validator for other fields but I am not sure of a way to use that to validate that two fields together compose a unique key. I am using a generic entity class which has an id generic type which can be swapped out for a composite key class but I have yet to get that to work very well. Any tips would be appreciated, thanks in advance

_________________
r4 gold ds


Top
 Profile  
 
 Post subject: Re: Confusion over inheritance
PostPosted: Fri Feb 19, 2010 4:30 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 988
This is the solution I would suggest


Code:
@Entity
@Table(uniqueConstraints = { @UniqueConstraint(columnNames={"name","version"})})
public class MyClass
{
    @Id @GeneratedValue(strategy = GenerationType.TABLE)
    @Column
    private long id;

    @Column
    @NaturalId (mutable = true)
    protected String name;

    @Column
    @NaturalId (mutable = true)
    protected String version;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 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.