-->
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.  [ 2 posts ] 
Author Message
 Post subject: @DiscriminatorFormula with InheritanceType.JOINED
PostPosted: Thu Dec 03, 2009 2:22 pm 
Newbie

Joined: Thu Dec 03, 2009 2:00 pm
Posts: 1
Using annotations version 3.4.0GA & hibernate version 3.3.2GA.

Per the hibernate documentation I can use a table per subclass with a discriminator (http://docs.jboss.org/hibernate/stable/ ... criminator). I also need to specify a discriminator formula.

Is this supported via annotations? The annotation documentation refers to using the @DiscriminatorFormula with single table inheritance, not joined. Is this the only supported use via annotations? I haven't tried configuring this via xml yet so not sure if it works or not. I'd prefer to use annotations but I guess could use XML for this one thing if need be.

My reasoning for using a discriminator with a joined inheritance is performance (please correct if my understanding is off). I have a wide hierarchy (one base class, many subclasses) so using a single table doesn't make sense the vast majority of columns wouldn't be used for any given record. However, by default the join inheritance type joins against every single subclass table. This is unecessary as I can define which sub table would contain the data via a discriminator and hence only have to join with that one sub table.

My root entity looks like the following:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorFormula("(a formula...)")
@DiscriminatorValue(value = "base")
@Table(name = "config")
public class BaseConfig{

A sub entity is defined as follows:
@Entity
@Table(name = "sub_config")
@DiscriminatorValue(value = "subconfig")
public class SubConfig

When I run this the formula annotation appears to be ignored and the default case statement is generated to determine which class to use. Just want to know if I'm doing something wrong, or this use case just isn't supported via annotations.

Thanks,
Bob


Top
 Profile  
 
 Post subject: Re: @DiscriminatorFormula with InheritanceType.JOINED
PostPosted: Tue Feb 02, 2010 8:00 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi jacorob,

I think you cannot use @DiscriminatorFormular and @DiscriminatorValue in conjunction. It does not make real sense either. The SQL snippet you put into the @DiscriminatorFormular is used to produce the value for discrimination by referring to other fields or do some calculations. For example, suppose you'd have a field type in your BaseConfig class where you store the actual type of an object. Then you could use something like that:
Code:
@DiscriminatorFormula("CASE WHEN type IS NULL THEN 'fully.qualified.type.name.of.BaseConfig' ELSE type END")
Note, that Hibernate maps the type field (of actual type Class) as its fully qualified name string. Furthermore, you may want to try leaving out the @DiscriminatorValue annotation. It might shadow the formula annotation.

On your mapping strategy:
I don't know if content-based discrimination works with JOINED inheritance. I think I've seen bug reports on that issue. But there might be an alternative. You can use
Code:
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
in your BaseConfig class. For each of the sub-classes you will then need
Code:
@SecondaryTable(name = "sub-class table name", pkJoinColumns = { @PrimaryKeyJoinColumn(name = "sub-class table id name", referencedColumnName = "super-class table id name") })

This will basically do the expected structure.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


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