-->
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: Selectively avoid JOIN for particular inheritance scenario
PostPosted: Tue Dec 01, 2009 5:14 pm 
Beginner
Beginner

Joined: Thu May 28, 2009 10:25 am
Posts: 21
Consider a concrete parent class (Employee) and an inherited class (ContractEmployee) that roughly mirrors my specific issue (Ideally, Employee should be abstract, ..but using Employee as a concrete parent class mirrors my situation more closely).
The annotations follow (using a mixed inheritance strategy)

Code:
@Entity
@Table(name = "EMPLOYEE")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class Employee {
   @Id
   @Column(name = "ID")
   @GeneratedValue
   private long id;

   @Column(name = "NAME")
   private String name;

   @Column(name = "ADDRESS")
   private String address;
...
}

Code:

@Entity
@DiscriminatorValue("CE")
@SecondaryTable(name = "CONTRACT_EMPLOYEE",
      pkJoinColumns = @PrimaryKeyJoinColumn(name = "CONTRACT_EMPLOYEE_ID")
)
public class ContractEmployee extends Employee{
   
   @Column(table = "CONTRACT_EMPLOYEE",
         name = "CE_HOURLYRATE",
         nullable = false
   )
   private int hourlyRate;
...
}




In the DB, this is what I see.
Code:
mysql> select * from employee;
+----------+----+------------------------+----------+
| DTYPE    | ID | ADDRESS                | NAME     |
+----------+----+------------------------+----------+
| Employee |  1 | 11 wide open road      | emp joe  |
| CE       |  2 | 1 some street          | ctr jack |
+----------+----+------------------------+----------+

mysql> select * from contract_employee;
+---------------+----------------------+
| CE_HOURLYRATE | CONTRACT_EMPLOYEE_ID |
+---------------+----------------------+
|           100 |                    2 |
+---------------+----------------------+
1 row in set (0.00 sec)


Id 1 = Employee
Id 2 = Contractor

Question:
Consider the following driver code and the results:
Code:
//1
      ContractEmployee cemp =  EmployeeDAO.retrieveContractEmployee(2); //HQL: from ContractEmployee where id = :id
      System.out.println(cemp);
      System.out.println("********************");
//2
      Employee emp =  EmployeeDAO.retrieveEmployee(2); //HQL: from Employee where id = :id
      System.out.println(emp);
      System.out.println("********************");
//3
      Employee emp1 =  EmployeeDAO.retrieveEmployee(1);//HQL: from Employee where id = :id
      System.out.println(emp1);


Code:
Hibernate: select contractem0_.ID as ID1_, contractem0_.ADDRESS as ADDRESS1_, contractem0_.NAME as NAME1_, contractem0_1_.CE_HOURLYRATE as CE1_2_ from EMPLOYEE contractem0_ left outer join CONTRACT_EMPLOYEE contractem0_1_ on contractem0_.ID=contractem0_1_.CONTRACT_EMPLOYEE_ID where contractem0_.DTYPE='CE' and contractem0_.ID=?
com.xyz.data.ContractEmployee@1329642[name=ctr jack,hourlyRate=100,address=1 some street,id=2]
********************
Hibernate: select employee0_.ID as ID1_, employee0_.ADDRESS as ADDRESS1_, employee0_.NAME as NAME1_, employee0_1_.CE_HOURLYRATE as CE1_2_, employee0_2_.FE_HOURLYRATE as FE1_3_, employee0_.DTYPE as DTYPE1_ from EMPLOYEE employee0_ left outer join CONTRACT_EMPLOYEE employee0_1_ on employee0_.ID=employee0_1_.CONTRACT_EMPLOYEE_ID left outer join FULLTIME_EMPLOYEE employee0_2_ on employee0_.ID=employee0_2_.FULLTIME_EMPLOYEE_ID where employee0_.ID=?
com.xyz.data.ContractEmployee@15575e0[name=ctr jack,hourlyRate=100,address=1 some street,id=2]
********************
Hibernate: select employee0_.ID as ID1_, employee0_.ADDRESS as ADDRESS1_, employee0_.NAME as NAME1_, employee0_1_.CE_HOURLYRATE as CE1_2_, employee0_2_.FE_HOURLYRATE as FE1_3_, employee0_.DTYPE as DTYPE1_ from EMPLOYEE employee0_ left outer join CONTRACT_EMPLOYEE employee0_1_ on employee0_.ID=employee0_1_.CONTRACT_EMPLOYEE_ID left outer join FULLTIME_EMPLOYEE employee0_2_ on employee0_.ID=employee0_2_.FULLTIME_EMPLOYEE_ID where employee0_.ID=?
com.xyz.data.Employee@81b1fb[name=emp joe,address=11 wide open road road,id=1]




Cases 1 and 2 are as expected. Is there any way to execute case 3 without invoking the JOINs on the FULLTIME_EMPLOYEE + CONTRACT_EMPLOYEE tables, assuming I already know that there are no children for a particular ID (from the DTYPE column)?



Thanks


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.