Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
I'm running my application in Weblogic. So as you can see I'm trying to load BOM with all his association using fetching mode. We don't want to use get(), load() b'cause of the many generated select and it is a question of performance, so we changed all association to Lazy=true. The problem is that in the BOM object we have a fileContent (which is declared as lazy=true), that we don't want to load when getting the BOM by the given ID, we want only to load the association. When running my code we get the generated SQL statement, and we see that fileContent is loaded and it take about 15 to load the result set.
Do you have any idea how can I load all the associations without properties declared as lazy=true.
Thank you in advance
public BOM getBOMById(long bomId, boolean lock) throws InfrastructureException, BOMNotFoundException
{
BOM bom = null;
try
{
startTransaction();
Session session = HibernateUtil.getSession();
// if (lock)
// {
// bom = (BOM) session.get(BOM.class, new Long(bomId), LockMode.UPGRADE);
// }
// else
// {
// bom = (BOM) session.get(BOM.class, new Long(bomId));
// if(bom != null)
// bom.getDescription();
// }
if(true){
Calendar enterDate = Calendar.getInstance();
BOM criteriaBom = new BOM();
Example example = Example.create(criteriaBom);
example.excludeProperty("fileContent");
System.out.println("example query example.toSqlString() = " + example.toString());
Criteria criteria = session.createCriteria(BOM.class);
bom = (BOM)criteria
.setFetchMode("laptopFiles", FetchMode.JOIN)
.setFetchMode("laptopFiles.files", FetchMode.JOIN)
.setFetchMode("laptopFiles.exeFiles", FetchMode.JOIN)
.setFetchMode("laptopFiles.formFiles", FetchMode.JOIN)
.setFetchMode("laptopFiles.exeFiles.files",FtchMode.JOIN)
.setFetchMode("laptopFiles.formFiles.files",FetchMode.JOIN)
.setFetchMode("pdaFiles", FetchMode.JOIN)
.setFetchMode("pdaFiles.files", FetchMode.JOIN)
.setFetchMode("pdaFiles.exeFiles", FetchMode.JOIN)
.setFetchMode("pdaFiles.formFiles", FetchMode.JOIN)
.setFetchMode("pdaFiles.exeFiles.files", FetchMode.JOIN)
.setFetchMode("pdaFiles.formFiles.files",FetchMode.JOIN)
.add( Restrictions.idEq(new Long(bomId))).uniqueResult();
Calendar exitDate = Calendar.getInstance();
System.out.println("enter Time = "+enterDate.getTime()+" exit = "+exitDate.getTime());
}
commitTransaction();
if(bom == null){
throw new BOMNotFoundException("There is no BOM found with the given ID "+bomId);
}
}
catch (HibernateException ex)
{
log.error("getBOMById failed.", ex);
throw new InfrastructureException(ex);
}
return bom;
}
Hibernate version:Hibernate 3.0
Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="com.mot.edc.cia.model.BOM"
table="BOM"
>
<id
name="id"
column="BOM_ID"
type="long"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-BOM.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<many-to-one
name="laptopFiles"
class="com.mot.edc.cia.model.LaptopFiles"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
unique="true"
column="LAPTOPFILES_ID"
not-null="false"
lazy="true"
/>
<many-to-one
name="pdaFiles"
class="com.mot.edc.cia.model.PDAFiles"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
unique="true"
column="PDAFILES_ID"
not-null="false"
lazy="true"
/>
<property
name="description"
type="java.lang.String"
update="true"
insert="true"
column="FILE_DESC"
not-null="true"
lazy="false"
/>
<property
name="fileContent"
type="blob"
update="true"
insert="true"
column="FILE_CONTENT"
not-null="true"
lazy="true"
/>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="com.mot.edc.cia.model.PDAFiles"
table="PDAFILES"
>
<cache usage="read-write"/>
<id
name="id"
column="PDAFILES_ID"
type="long"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-PDAFiles.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<set
name="files"
table="PDA_BOMFILE"
lazy="true"
inverse="false"
cascade="save-update"
sort="unsorted"
>
<cache usage="read-write"/>
<key
column="PDAFILES_ID"
>
</key>
<many-to-many
class="com.mot.edc.cia.model.BOMFile"
column="BOMFILE_ID"
outer-join="auto"
/>
</set>
<set
name="formFiles"
table="PDA_EDCFORM"
lazy="true"
inverse="false"
cascade="save-update"
sort="unsorted"
>
<cache usage="read-write"/>
<key
column="PDAFILES_ID"
>
</key>
<many-to-many
class="com.mot.edc.cia.model.EDCForm"
column="EDCFORM_ID"
outer-join="auto"
/>
</set>
<many-to-one
name="exeFiles"
class="com.mot.edc.cia.model.PostDeploymentExeFiles"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
unique="true"
column="POSTDEPLOYEXE_ID"
not-null="false"
lazy="true"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-PDAFiles.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="com.mot.edc.cia.model.LaptopFiles"
table="LAPTOPFILES"
>
<cache usage="read-write"/>
<id
name="id"
column="LAPTOPFILES_ID"
type="long"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-LaptopFiles.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<set
name="files"
table="LAPTOP_BOMFILE"
lazy="true"
cascade="save-update"
sort="unsorted"
>
<cache usage="read-write"/>
<key
column="LAPTOPFILES_ID"
>
</key>
<many-to-many
class="com.mot.edc.cia.model.BOMFile"
column="BOMFILE_ID"
outer-join="auto"
/>
</set>
<set
name="formFiles"
table="LAPTOP_EDCFORM"
lazy="true"
inverse="false"
cascade="save-update"
sort="unsorted"
>
<cache usage="read-write"/>
<key
column="LAPTOPFILES_ID"
>
</key>
<many-to-many
class="com.mot.edc.cia.model.EDCForm"
column="EDCFORM_ID"
outer-join="auto"
/>
</set>
<many-to-one
name="exeFiles"
class="com.mot.edc.cia.model.PostDeploymentExeFiles"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
unique="true"
column="POSTDEPLOYEXE_ID"
not-null="false"
lazy="true"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-LaptopFiles.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
public static Session getSession()
throws InfrastructureException {
// With CMT, this should return getSessionFactory().getCurrentSession() and do nothing else
// Session s = (Session) threadSession.get();
Session s = null;
try {
if (s == null) {
//log.debug("Opening new Session for this thread.");
//if (getInterceptor() != null) {
// log.debug("Using interceptor: " + getInterceptor().getClass());
// s = getSessionFactory().openSession(getInterceptor());
//} else {
// s = getSessionFactory().openSession();
s = getSessionFactory().getCurrentSession();
//}
//threadSession.set(s);
}
}
catch (HibernateException ex)
{
throw new InfrastructureException(ex);
}
return s;
}
/**
* Closes the Session local to the thread.
*/
public static void closeSession()
throws InfrastructureException {
// Would be written as a no-op in an EJB container with CMT
try {
//Session s = (Session) threadSession.get();
//threadSession.set(null);
//if (s != null && s.isOpen()) {
// log.debug("Closing Session of this thread.");
// s.close();
//}
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
}
Full stack trace of any exception that occurs:
Name and version of the database you are using:
Oracle
The generated SQL (show_sql=true):
select this_.BOM_ID as BOM1_12_, this_.LAPTOPFILES_ID as LAPTOPFI2_3_12_, this_.PDAFILES_ID as PDAFILES3_3_12_, this_.FILE_DESC as
FILE4_3_12_, this_.FILE_CONTENT as FILE5_3_12_, laptopfile2_.LAPTOPFILES_ID as
LAPTOPFI1_0_, laptopfile2_.POSTDEPLOYEXE_ID as POSTDEPL2_13_0_, files3_.LAPTOPFILES_ID as LAPTOPFI1_14_,
bomfile4_.BOMFILE_ID as BOMFILE2_14_, bomfile4_.BOMFILE_ID as BOMFILE1_1_, bomfile4_.PATH as PATH5_1_,
bomfile4_.PERMISSION as PERMISSION5_1_, bomfile4_.NAME as NAME5_1_, bomfile4_.VERSION as VERSION5_1_,
bomfile4_.CHECKSUM as CHECKSUM5_1_, bomfile4_.FILE_SIZE as FILE7_5_1_, formfiles5_.LAPTOPFILES_ID as LAPTOPFI1_15_,
edcform6_.EDCFORM_ID as EDCFORM2_15_, edcform6_.EDCFORM_ID as EDCFORM1_2_, edcform6_.FORM_NAME as FORM2_11_2_,
files7_.EDCFORM_ID as EDCFORM1_16_, bomfile8_.BOMFILE_ID as BOMFILE2_16_, bomfile8_.BOMFILE_ID as BOMFILE1_3_,
bomfile8_.PATH as PATH5_3_, bomfile8_.PERMISSION as PERMISSION5_3_, bomfile8_.NAME as NAME5_3_, bomfile8_.VERSION as
VERSION5_3_, bomfile8_.CHECKSUM as CHECKSUM5_3_, bomfile8_.FILE_SIZE as FILE7_5_3_,
postdeploy9_.POSTDEPLOYEXE_ID as POSTDEPL1_4_, files10_.POSTDEPLOYEXE_ID as POSTDEPL1_17_, bomfile11_.BOMFILE_ID as
BOMFILE2_17_, bomfile11_.BOMFILE_ID as BOMFILE1_5_, bomfile11_.PATH as PATH5_5_, bomfile11_.PERMISSION as PERMISSION5_5_,
bomfile11_.NAME as NAME5_5_, bomfile11_.VERSION as VERSION5_5_, bomfile11_.CHECKSUM as CHECKSUM5_5_,
bomfile11_.FILE_SIZE as FILE7_5_5_, pdafiles12_.PDAFILES_ID as PDAFILES1_6_, pdafiles12_.POSTDEPLOYEXE_ID as POSTDEPL2_16_6_,
files13_.PDAFILES_ID as PDAFILES1_18_, bomfile14_.BOMFILE_ID as BOMFILE2_18_, bomfile14_.BOMFILE_ID as BOMFILE1_7_,
bomfile14_.PATH as PATH5_7_, bomfile14_.PERMISSION as PERMISSION5_7_, bomfile14_.NAME as NAME5_7_,
bomfile14_.VERSION as VERSION5_7_, bomfile14_.CHECKSUM as CHECKSUM5_7_, bomfile14_.FILE_SIZE as FILE7_5_7_,
formfiles15_.PDAFILES_ID as PDAFILES1_19_, edcform16_.EDCFORM_ID as EDCFORM2_19_, edcform16_.EDCFORM_ID as EDCFORM1_8_,
edcform16_.FORM_NAME as FORM2_11_8_, files17_.EDCFORM_ID as EDCFORM1_20_, bomfile18_.BOMFILE_ID as BOMFILE2_20_,
bomfile18_.BOMFILE_ID as BOMFILE1_9_, bomfile18_.PATH as PATH5_9_, bomfile18_.PERMISSION as PERMISSION5_9_,
bomfile18_.NAME as NAME5_9_, bomfile18_.VERSION as VERSION5_9_, bomfile18_.CHECKSUM as CHECKSUM5_9_,
bomfile18_.FILE_SIZE as FILE7_5_9_, postdeploy19_.POSTDEPLOYEXE_ID as POSTDEPL1_10_, files20_.POSTDEPLOYEXE_ID as
POSTDEPL1_21_, bomfile21_.BOMFILE_ID as BOMFILE2_21_, bomfile21_.BOMFILE_ID as BOMFILE1_11_, bomfile21_.PATH as
PATH5_11_, bomfile21_.PERMISSION as PERMISSION5_11_, bomfile21_.NAME as NAME5_11_, bomfile21_.VERSION as
VERSION5_11_, bomfile21_.CHECKSUM as CHECKSUM5_11_, bomfile21_.FILE_SIZE as FILE7_5_11_ from BOM this_,
LAPTOPFILES laptopfile2_, LAPTOP_BOMFILE files3_, BOMFILE bomfile4_, LAPTOP_EDCFORM formfiles5_, EDCFORM edcform6_,
EDCFORM_BOMFILE files7_, BOMFILE bomfile8_, POSTDEPLOYMENTEXEFILES postdeploy9_, POSTDEPLOYEXE_BOMFILE files10_,
BOMFILE bomfile11_, PDAFILES pdafiles12_, PDA_BOMFILE files13_, BOMFILE bomfile14_, PDA_EDCFORM formfiles15_,
EDCFORM edcform16_, EDCFORM_BOMFILE files17_, BOMFILE bomfile18_, POSTDEPLOYMENTEXEFILES postdeploy19_,
POSTDEPLOYEXE_BOMFILE files20_, BOMFILE bomfile21_ where this_.LAPTOPFILES_ID=laptopfile2_.LAPTOPFILES_ID(+) and
laptopfile2_.LAPTOPFILES_ID=files3_.LAPTOPFILES_ID(+) and files3_.BOMFILE_ID=bomfile4_.BOMFILE_ID(+) and
laptopfile2_.LAPTOPFILES_ID=formfiles5_.LAPTOPFILES_ID(+) and formfiles5_.EDCFORM_ID=edcform6_.EDCFORM_ID(+) and
edcform6_.EDCFORM_ID=files7_.EDCFORM_ID(+) and files7_.BOMFILE_ID=bomfile8_.BOMFILE_ID(+) and
laptopfile2_.POSTDEPLOYEXE_ID=postdeploy9_.POSTDEPLOYEXE_ID(+) and postdeploy9_.POSTDEPLOYEXE_ID=files10_.POSTDEPLOYEXE_ID(+) and
files10_.BOMFILE_ID=bomfile11_.BOMFILE_ID(+) and this_.PDAFILES_ID=pdafiles12_.PDAFILES_ID(+) and
pdafiles12_.PDAFILES_ID=files13_.PDAFILES_ID(+) and files13_.BOMFILE_ID=bomfile14_.BOMFILE_ID(+) and
pdafiles12_.PDAFILES_ID=formfiles15_.PDAFILES_ID(+) and formfiles15_.EDCFORM_ID=edcform16_.EDCFORM_ID(+) and
edcform16_.EDCFORM_ID=files17_.EDCFORM_ID(+) and files17_.BOMFILE_ID=bomfile18_.BOMFILE_ID(+) and
pdafiles12_.POSTDEPLOYEXE_ID=postdeploy19_.POSTDEPLOYEXE_ID(+) and postdeploy19_.POSTDEPLOYEXE_ID=files20_.POSTDEPLOYEXE_ID(+) and
files20_.BOMFILE_ID=bomfile21_.BOMFILE_ID(+) and (1=1) and this_.BOM_ID = 1739
Debug level Hibernate log excerpt: