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