-->
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.  [ 1 post ] 
Author Message
 Post subject: select for each subclass for table per class hierarchy
PostPosted: Tue Jun 16, 2009 4:15 pm 
Newbie

Joined: Tue Mar 11, 2008 4:49 am
Posts: 12
Hi
I am using table per class hierarchy inheritance and having problems with polymorphic queries.
I have 2 level inheritance structure:
class Transaction
class TransactionA extends Transaction
class TransactionB extends Transaction

class TransactionD extends Transaction
class Transaction D1 extends TransactionD
class Transaction D2 extends TransactionD
class Transaction D3 extends TransactionD

Code:
@Entity
@Table(name = "transaction")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
        name = "type",
        discriminatorType = DiscriminatorType.STRING       
) public abstract class Transaction

@Entity
@DiscriminatorValue("A")
public class TransactionA extends Transaction

@Entity
@DiscriminatorValue("B")
public class TransactionB extends Transaction

public abstract class TransactionD extends Transaction

@Entity
@DiscriminatorValue("D1")
public class TransactionD1 extends TransactionD

@Entity
@DiscriminatorValue("D2")
public class TransactionD2 extends TransactionD

@Entity
@DiscriminatorValue("D3")
public class TransactionD3 extends TransactionD



TransactionD3, TransactionD2 and TransactionD1 classes don't define any mappings since all required fields are mapped in TransactionD class.
Everything works ok but when i execute criteria or hql queries like
Code:
TransactionD transactionD = (TransactionD) session.createQuery(" from "+TransactionD.class.getName() +" t where t.id =?").setLong(0, 10000324L).uniqueResult();


Code:
TransactionD transactionD = (TransactionD) sessionFactory.getCurrentSession()
            .createCriteria(TransactionD .class).add(Restrictions.eq("id", 10000324L)).uniqueResult();
/

I can see that Hibernate executes one SQL for each of TransactionD subclasses:
Code:
select
        transactio0_.t_id as tfx2_155_,
           ...
    from
        transaction_d transactio0_
    where
        transactio0_.type='D1'
        and transactio0_.t_id=?


Code:
select
        transactio0_.t_id as tfx2_155_,
           ...
    from
        transaction_d transactio0_
    where
        transactio0_.type='D2'
        and transactio0_.t_id=?


Code:
select
        transactio0_.t_id as tfx2_155_,
           ...
    from
        transaction_d transactio0_
    where
        transactio0_.type='D3'
        and transactio0_.t_id=?


I am wondering why hibernate can not execute just one sql providing set of discriminator values of subclasses of TransactionD in where condition:
Code:
select
        transactio0_.t_id as tfx2_155_,
           ...
    from
        transaction_d transactio0_
    where
        transactio0_.type in ('D1,'D2','D3')
        and transactio0_.t_id=?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.