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=?