Hibernate version:
3.1.2
Mapping documents:
Annotations
@Table(name="EMPLOYEE")
@Entity
@PrimaryKeyJoinColumn(name="EMPLOYEE_ID")
public class Employee extends BusinessRole
{
private List<PracticeLocation> locations = null;
/**
* Default constructor
*/
public Employee() {}
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="EMPLOYEE_LOCATION",
joinColumns = { @JoinColumn(name="EMPLOYEE_ID") },
inverseJoinColumns = {@JoinColumn(name="LOCATION_ID")}
)
@OrderBy("name")
public List<PracticeLocation> getLocations() { return locations; }
public void setLocations(List<PracticeLocation> val) { locations = val; }
public void addLocation(PracticeLocation location)
{
if (locations == null)
locations = new ArrayList<PracticeLocation>();
for (PracticeLocation loc : locations) {
if (loc.getId().longValue() == location.getId().longValue())
return;
}
locations.add(location);
}
} // end class Employee
@Entity
@Table(name="BUSINESS_ROLE")
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class BusinessRole
{
/**
/**
* Person associated with this business role
*/
private Person person = null;
@ManyToOne(fetch=FetchType.EAGER)
@Cascade( {CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
@JoinColumn(name="PERSON_ID", nullable=false)
public Person getPerson() {
if (person == null)
person = new Person();
return person;
}
public void setPerson(Person val) { person = val; }
} // end class BusinessRole
@Entity
@Table(name="PERSON")
public class Person
{
/**
* Phone numbers for this person
*/
private List<Phone> phoneNumbers = null;
/**
* Address for this person
*/
private List<PersonAddress> addresses = null;
@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name="PERSON_ID")
@Cascade( {CascadeType.ALL})
@OrderBy("preferred")
public List<Phone> getPhoneNumbers() { return phoneNumbers; }
public void setPhoneNumbers(List<Phone> val) { phoneNumbers = val; }
public void addPhoneNumber(Phone phone)
{
if (phoneNumbers == null)
phoneNumbers = new ArrayList<Phone>();
phoneNumbers.add(phone);
}
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="PERSON_ADDRESSES",
joinColumns = { @JoinColumn(name="PERSON_ID") },
inverseJoinColumns = {@JoinColumn(name="ADDRESS_ID")}
)
@Cascade( {CascadeType.SAVE_UPDATE})
@OrderBy("preferred")
public List<PersonAddress> getAddresses() { return addresses; }
public void setAddresses(List<PersonAddress> val) { addresses = val; }
public void addAddress(PersonAddress address)
{
if (addresses == null)
addresses = new ArrayList<PersonAddress>();
addresses.add(address);
}
} // end class Person
Code between sessionFactory.openSession() and session.close():
Employee employee = employeeDAO.findById(pEmployeeId);
Full stack trace of any exception that occurs:
N/A
Name and version of the database you are using:
MySQL 5.0.18
The generated SQL (show_sql=true):
2006-09-27 07:47:21,828 INFO [STDOUT] Hibernate: select employee0_.EMPLOYEE_ID as BUSINESS1_32_6_, employee0_1_.VERSION as VERSION32_6_, employee0_1_.DESCRIPTION as DESCRIPT3_32_6_, employee0_1_.PERSON_ID as PERSON5_32_6_, employee0_1_.NAME as NAME32_6_, employee0_.STATUS as STATUS33_6_, employee0_.PRACTICE_ID as PRACTICE3_33_6_, employee0_.EXTERNAL_SYSTEM_ID as EXTERNAL4_33_6_, employee0_.PRIM_LOCATION_FK as PRIM5_33_6_, employee0_.businessRoleName as business6_33_6_, employee0_.EMPLOYMENT_STATUS as EMPLOYMENT7_33_6_, person1_.PERSON_ID as PERSON1_63_0_, person1_.VERSION as VERSION63_0_, person1_.SUFFIX as SUFFIX63_0_, person1_.USER_ID as USER24_63_0_, person1_.PROF_CREDENTIALS as PROF4_63_0_, person1_.ALIAS as ALIAS63_0_, person1_.DOB as DOB63_0_, person1_.LAST_NAME as LAST6_63_0_, person1_.FIRST_NAME as FIRST7_63_0_, person1_.EMAIL as EMAIL63_0_, person1_.MIDDLE_NAME as MIDDLE9_63_0_, person1_.TITLE as TITLE63_0_, person1_.GENDER as GENDER63_0_, person1_.PREFERRED_PHONE_NUMBER as PREFERRED12_63_0_, person1_.NICK_NAME as NICK14_63_0_, person1_.SSN as SSN63_0_, person1_.PRIMARY_LANGUAGE as PRIMARY16_63_0_, person1_.BLOOD_TYPE as BLOOD17_63_0_, person1_.RACE_ID as RACE18_63_0_, person1_.ETHNICITY_ID as ETHNICITY19_63_0_, person1_.CITIZENSHIP_ID as CITIZEN20_63_0_, person1_.RELIGION_ID as RELIGION21_63_0_, person1_.MARITAL_STATUS_ID as MARITAL22_63_0_, person1_.PHOTO_FK as PHOTO23_63_0_, user2_.USER_ID as USER1_97_1_, user2_.VERSION as VERSION97_1_, user2_.PASSWORD as PASSWORD97_1_, user2_.USERNAME as USERNAME97_1_, user2_.ALLOW_LOGIN as ALLOW5_97_1_, user2_.PWD_MUST_RESET as PWD6_97_1_, user2_.PWD_LAST_RESET_DATE as PWD7_97_1_, attrs3_.PERSON_PERSON_ID as PERSON1_8_, attrs3_.NAME as NAME8_, attrs3_.VALUE as VALUE8_, attrs3_.VALUE_TYPE as VALUE4_8_, phonenumbe4_.PERSON_PERSON_ID as PERSON1_9_, phonenumbe4_.PHONE_NUMBER as PHONE2_9_, phonenumbe4_.NUMBER_TYPE_ID as NUMBER3_9_, phonenumbe4_.PREFERRED as PREFERRED9_, addresses5_.PERSON_ID as PERSON1_10_, personaddr6_.ADDRESS_ID as ADDRESS2_10_, personaddr6_.ADDRESS_ID as ADDRESS1_66_2_, personaddr6_.COUNTRY as COUNTRY66_2_, personaddr6_.TIME_ZONE as TIME3_66_2_, personaddr6_.POSTAL_CODE as POSTAL4_66_2_, personaddr6_.CITY as CITY66_2_, personaddr6_.ADDRESS1 as ADDRESS6_66_2_, personaddr6_.STATE_PROVINCE as STATE7_66_2_, personaddr6_.ADDRESS2 as ADDRESS8_66_2_, personaddr6_.LATITUDE as LATITUDE66_2_, personaddr6_.LONGITUDE as LONGITUDE66_2_, personaddr6_.VERSION as VERSION66_2_, personaddr6_.START_DATE as START12_66_2_, personaddr6_.END_DATE as END13_66_2_, personaddr6_.PREFERRED as PREFERRED66_2_, personaddr6_.ADDRESS_TYPE_ID as ADDRESS15_66_2_, attrs7_.EMPLOYEE_EMPLOYEE_ID as EMPLOYEE1_11_, attrs7_.NAME as NAME11_, attrs7_.VALUE as VALUE11_, attrs7_.VALUE_TYPE as VALUE4_11_, locations8_.EMPLOYEE_ID as EMPLOYEE1_12_, practicelo9_.LOCATION_ID as LOCATION2_12_, practicelo9_.LOCATION_ID as LOCATION1_74_3_, practicelo9_.COUNTRY as COUNTRY74_3_, practicelo9_.TIME_ZONE as TIME3_74_3_, practicelo9_.POSTAL_CODE as POSTAL4_74_3_, practicelo9_.CITY as CITY74_3_, practicelo9_.ADDRESS1 as ADDRESS6_74_3_, practicelo9_.STATE_PROVINCE as STATE7_74_3_, practicelo9_.ADDRESS2 as ADDRESS8_74_3_, practicelo9_.LATITUDE as LATITUDE74_3_, practicelo9_.LONGITUDE as LONGITUDE74_3_, practicelo9_.NAME as NAME74_3_, practicelo9_.SERVICES as SERVICES74_3_, practicelo9_.VERSION as VERSION74_3_, practicelo9_.DESCRIPTION as DESCRIP14_74_3_, practicelo9_.STATUS as STATUS74_3_, practicelo9_.EXTERNAL_SYSTEM_ID as EXTERNAL16_74_3_, practicelo9_.EMAIL as EMAIL74_3_, practicelo9_.PARENT_LOCATION_ID as PARENT18_74_3_, practicelo9_.WEB_SITE as WEB19_74_3_, practicelo9_.MAIN_PHONE as MAIN20_74_3_, practicelo9_.ALT_PHONE as ALT21_74_3_, practicelo9_.EMERGENCY_PHONE as EMERGENCY22_74_3_, practicelo9_.FAX as FAX74_3_, practicelo9_.PRACTICE_ID as PRACTICE24_74_3_, practicelo9_.PREFERENCES_ID as PREFERE25_74_3_, practicelo9_.OFFICE_HOURS_ID as OFFICE26_74_3_, attrs10_.PRACTICE_LOCATION_LOCATION_ID as PRACTICE1_13_, attrs10_.NAME as NAME13_, attrs10_.VALUE as VALUE13_, attrs10_.VALUE_TYPE as VALUE4_13_, phonenumbe11_.PRACTICE_LOCATION_LOCATION_ID as PRACTICE1_14_, phonenumbe11_.PHONE_NUMBER as PHONE2_14_, phonenumbe11_.NUMBER_TYPE_ID as NUMBER3_14_, phonenumbe11_.PREFERRED as PREFERRED14_, from EMPLOYEE employee0_ inner join BUSINESS_ROLE employee0_1_ on employee0_.EMPLOYEE_ID=employee0_1_.BUSINESS_ROLE_ID inner join PERSON person1_ on employee0_1_.PERSON_ID=person1_.PERSON_ID left outer join USER user2_ on person1_.USER_ID=user2_.USER_ID left outer join PERSON_attrs attrs3_ on person1_.PERSON_ID=attrs3_.PERSON_PERSON_ID left outer join PERSON_phoneNumbers phonenumbe4_ on person1_.PERSON_ID=phonenumbe4_.PERSON_PERSON_ID left outer join PERSON_ADDRESSES addresses5_ on person1_.PERSON_ID=addresses5_.PERSON_ID left outer join PERSON_ADDRESS personaddr6_ on addresses5_.ADDRESS_ID=personaddr6_.ADDRESS_ID left outer join EMPLOYEE_attrs attrs7_ on employee0_.EMPLOYEE_ID=attrs7_.EMPLOYEE_EMPLOYEE_ID left outer join EMPLOYEE_LOCATION locations8_ on employee0_.EMPLOYEE_ID=locations8_.EMPLOYEE_ID left outer join PRACTICE_LOCATION practicelo9_ on locations8_.LOCATION_ID=practicelo9_.LOCATION_ID left outer join PRACTICE_LOCATION_attrs attrs10_ on practicelo9_.LOCATION_ID=attrs10_.PRACTICE_LOCATION_LOCATION_ID left outer join PRACTICE_LOCATION_phoneNumbers phonenumbe11_ on practicelo9_.LOCATION_ID=phonenumbe11_.PRACTICE_LOCATION_LOCATION_ID left outer join SYSTEM_PREFERENCES systempref12_ on practicelo9_.PREFERENCES_ID=systempref12_.PREFERENCE_ID left outer join PRACTICE_LOCATION_OFFICE_HOURS practicelo13_ on practicelo9_.OFFICE_HOURS_ID=practicelo13_.OFFICE_HOURS_ID where employee0_.EMPLOYEE_ID=?
Debug level Hibernate log excerpt:
Problems with Session and transaction handling?
I have an employee/person that "has" 4 phone numbers, 1 address, and 1 "location". When I do the query, I am getting 4 locations and 4 addresses back. As a test, when I change the locations to load lazily and then force the load of the locations after loading the employee, the correct number of locations is returned.
Any thoughts? TIA for any help.
|