Hibernate version: 3.0.5
Mapping documents:
Code:
<hibernate-mapping package="be.rmi.intranet.rh" default-lazy="true">
<class name="be.rmi.intranet.rh.Person" table="RH_PERSON">
<cache usage="read-write"/>
<id name="id">
<column name="PERSON_ID" not-null="true" unique="true" sql-type="NUMBER"/>
<generator class="sequence">
<param name="sequence">RH_GENERIC_SEQ</param>
</generator>
</id>
<many-to-one name="organisation" column="ORG_ID"/>
<property name="firstname" column="FIRST_NAME" type="string" />
<property name="lastname" column="LAST_NAME" type="string" />
<many-to-one name="home" column="HOME_ADDRESS" unique="true" not-null="true" cascade="persist,merge,save-update"/>
<!-- snip, lots of other properties -->
<joined-subclass name="be.rmi.intranet.rh.Employee"
table="RH_EMPLOYEE">
<key column="PERSON_ID" />
<many-to-one name="section" column="SECT_ID" />
<property name="scientific" column="EMP_SCIENTIFIC"
type="yes_no" />
<property name="email" column="EMP_EMAIL" type="string" />
<property name="login" column="EMP_LOGIN" type="string" />
<many-to-one name="officeAddress" column="EMP_OFFICE_ADDRESS"
unique="true" cascade="persist,merge,save-update"/>
<!-- snip, lots of other properties here too -->
</joined-subclass>
</class>
</hibernate-mapping>
Code between reattach of user session and detach (working with session per user pattern in web environment):Code:
Query query = session.createQuery(
"select Employee from be.rmi.intranet.rh.Employee Employee order by Employee.lastname, Employee.firstname desc");
return query.list();
Name and version of the database you are using:Oracle 9
The generated SQL (show_sql=true):Code:
Hibernate: select employee0_.PERSON_ID as PERSON1_, employee0_1_.ORG_ID as ORG2_3_, employee0_1_.FIRST_NAME as FIRST3_3_, employee0_1_.LAST_NAME as LAST4_3_, employee0_1_.FORMULATION as FORMULAT5_3_, employee0_1_.TITLE as TITLE3_, employee0_1_.BIRTHDATE as BIRTHDATE3_, employee0_1_.BIRTHPLACE as BIRTHPLACE3_, employee0_1_.GENDER as GENDER3_, employee0_1_.LANGUAGE as LANGUAGE3_, employee0_1_.HOME_ADDRESS as HOME11_3_, employee0_1_.NATIONALITY as NATIONA12_3_, employee0_1_.PICTURE as PICTURE3_, employee0_1_.NOTE as NOTE3_, employee0_.SECT_ID as SECT2_4_, employee0_.EMP_SCIENTIFIC as EMP3_4_, employee0_.EMP_EMAIL as EMP4_4_, employee0_.EMP_LOGIN as EMP5_4_, employee0_.EMP_OFFICE_ADDRESS as EMP6_4_, employee0_.EMP_OFFICELOCATION as EMP7_4_, employee0_.EMP_OFFICEKEYNUMBER as EMP8_4_, employee0_.EMP_IN_DATE as EMP9_4_, employee0_.EMP_OUT_DATE as EMP10_4_, employee0_.EMP_STATUTAIRE_NOMINATION_DATE as EMP11_4_, employee0_.EMP_WORKING_PERCENT as EMP12_4_, employee0_.EMP_SALARY_SCALE as EMP13_4_, employee0_.EMP_SALARY_IN_DATE as EMP14_4_, employee0_.EMP_FIRSTJOB_DATE as EMP15_4_, employee0_.EMP_RANK_BEFORE_REFORM as EMP16_4_, employee0_.EMP_NEW_RANK as EMP17_4_, employee0_.EMP_MEDICAL_CENTER as EMP18_4_, employee0_.EMP_MEDICAL_NUMBER as EMP19_4_, employee0_.EMP_SICKDAY_YEARSPENT as EMP20_4_, employee0_.EMP_SICKDAY_YEARLEFT as EMP21_4_, employee0_.EMP_SICKDAY_INBANK as EMP22_4_, employee0_.EMP_SICKDAY_FORAGE as EMP23_4_, employee0_.EMP_CIRCONSTANCE_DAY as EMP24_4_, employee0_.EMP_ILLNESS_CONTACT1 as EMP25_4_, employee0_.EMP_ILLNESS_CONTACT2 as EMP26_4_, employee0_.EMP_NAT_REG_NUMBER as EMP27_4_, employee0_.EMP_ID_CARD as EMP28_4_, employee0_.EMP_BANKACCOUNTNO as EMP29_4_, employee0_.EMP_BADGE_NO as EMP30_4_, employee0_.EMP_IBG_NO as EMP31_4_, employee0_.EMP_BILINGUAL_PREMIUM as EMP32_4_, employee0_.EMP_BILINGUAL_SCALE as EMP33_4_, employee0_.EMP_ADMIN_MATRICULE as EMP34_4_, employee0_.EMP_DRIVE_AUTORISATION as EMP35_4_, employee0_.EMP_RMI_ID as EMP36_4_, employee0_.EMP_TRAP as EMP37_4_, employee0_.EMP_STATUS_1 as EMP38_4_, employee0_.EMP_STATUS_2 as EMP39_4_, employee0_.EMP_ADMIN_STATE as EMP40_4_, employee0_.EMP_MARITAL_STATUS as EMP41_4_, employee0_.SPP_ID as SPP42_4_, employee0_.PDATA_ID as PDATA43_4_, employee0_.TRANSPORT_ID as TRANSPORT44_4_, employee0_.NOK_ID as NOK45_4_, employee0_.EMP_WORKED_ON_PATR as EMP46_4_, employee0_.EMP_PHONE_ACTIV_CODE as EMP47_4_, employee0_.EMP_TIMECLOCK_CODE as EMP48_4_ from RH_EMPLOYEE employee0_ inner join RH_PERSON employee0_1_ on employee0_.PERSON_ID=employee0_1_.PERSON_ID order by employee0_1_.LAST_NAME, employee0_1_.FIRST_NAME desc
Problem:Am trying to use 2nd level cache to prevent querying all those data again and again from database. If would be expecting Hibernate to do this:
Code:
select employee0_.PERSON_ID as PERSON1_ from RH_EMPLOYEE employee0_ inner join RH_PERSON employee0_1_ on employee0_.PERSON_ID=employee0_1_.PERSON_ID order by employee0_1_.LAST_NAME, employee0_1_.FIRST_NAME desc
This would return only Id of objects and then there would be a chance for second level cache to provide the corresponding 'Employee' Object. Instead, as you see, hibernate is hungry and takes all datas of the object at onec. And there are lot of those datas.
I've read that query caching is not a good idea, as the whole result would be cached. That's not what i want, i just want the List of ids to be fetched from Database, but hydratation process to take into account object with those ids are already in 2nd level cache and there is no need for getting those datas from server.