-->
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.  [ 3 posts ] 
Author Message
 Post subject: table per class hierarchy: 2nd select seems to be redundant
PostPosted: Thu Aug 30, 2007 5:27 am 
Newbie

Joined: Thu Aug 30, 2007 5:07 am
Posts: 5
Hibernate version:
3.2.4.sp1

Name and version of the database you are using:
MySQL 5.0

Hibernate mapping:
Code:
@Entity
@Table(name = "account")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public abstract class BaseAccountPojo
{
   @Id
   @GeneratedValue
   @Column(name = "account_id")
   public Long accountId;
}

@Entity
@DiscriminatorValue("cust")
public class CustAccountPojo extends BaseAccountPojo
{
   @OneToOne
   @PrimaryKeyJoinColumn
   public CreditCardPojo creditCard;

   @OneToMany(mappedBy = "owner")
   public Set<EndCustAccountPojo> endCustAccounts = new HashSet<EndCustAccountPojo>();
}

@Entity
@DiscriminatorValue("endCust")
public class EndCustAccountPojo extends BaseAccountPojo
{
   @OneToOne
   @PrimaryKeyJoinColumn
   public CreditCardPojo creditCard;

   @ManyToOne
   @JoinColumn(name = "owner_id")
   public CustAccountPojo owner;
}

@Entity
@Table(name = "credit_card")
public class CreditCardPojo
{
   @Id
   @GeneratedValue
   @Column(name = "account_id")
   public Long accountId;
}


Code between sessionFactory.openSession() and session.close():
Code:
session.get(EndCustAccountPojo.class, 1L);


The generated SQL:
Code:
Hibernate:
    select
        endcustacc0_.account_id as account2_0_2_,
        endcustacc0_.owner_id as owner3_0_2_,
        custaccoun1_.account_id as account2_0_0_,
        creditcard2_.account_id as account1_1_1_
    from
        account endcustacc0_
    left outer join
        account custaccoun1_
            on endcustacc0_.owner_id=custaccoun1_.account_id
    left outer join
        credit_card creditcard2_
            on custaccoun1_.account_id=creditcard2_.account_id
    where
        endcustacc0_.account_id=?
        and endcustacc0_.type='endCust'
Hibernate:
    select
        creditcard0_.account_id as account1_1_0_
    from
        credit_card creditcard0_
    where
        creditcard0_.account_id=?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 7:24 am 
Newbie

Joined: Thu Aug 30, 2007 5:07 am
Posts: 5
Everything works fine here. Just have turned on the debug level of log4j and noticed that second select is for fetching the relation CustAccountPojo.CreditCardPojo. My bad. Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 03, 2007 3:59 am 
Newbie

Joined: Thu Aug 30, 2007 5:07 am
Posts: 5
And yet it redundant. I noticed that it's not possible to use bidirectional relations together with table per class hierarchy. Result is superfluous selects. That's very very bad. Thanks.


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