Hi, I'm newbie at Hibernate but I got into a project that already was working with JBoss 3.2.3 and Hibernate 2.16.
The application was designed to use Hibernate with lazy="false" (default for Hibernate 2.1.6), but the problem is that it loads lots of data that we don't need for all sitations. For example it loads, all roles and it assosiated objects of a User, when we only need (in some case) just the User itself!
So we tried to use the option lazy="true", but... When I am in a big transaction => Hibernate close the session!! and it throws one of those LazyInitializeException...
My code is something like this:
Session session = ThreadLocalSession.currentSession();
// Obtengo el usuario por login
SecurityServices secServices = new SecurityServicesBusinessDelegate();
User user = secServices.getUser(userLogin);
if (user == null) { // No se encontró el usuario
return ret;
}
// Obtengo los registros de estado de un incidente no terminados
List status = getOpenIncidentStatusByUser(user.getId());
// Obtengo los incidentes en revision (por parte de un administrador) ingresados por el usuario
//TODO: Estudiar si es necesario
List inRevision = getIncidentStatusInRevision(user);
if (inRevision != null && inRevision.size() > 0) {
status.addAll(inRevision);
}
Date currentDateTime = DateUtilities.getCurrentDateTime();
Date currentDate = DateUtilities.getCurrentDate();
// Recorro los estados armando los registros de resumen
// correspondientes
Iterator itStatus = status.iterator();
while (itStatus.hasNext()) {
IncidentStatus incStatus = (IncidentStatus) itStatus.next();
long incidentCode = incStatus.getIncident();
long subincidentCode = incStatus.getSubincident();
// Obtengo el ultimo registro de incidente asociado a este estado
IncidentFixedData fixedData = getIncidentFixedData(incidentCode, subincidentCode, false);
// Obtengo el último registro de traza del incidente
IncidentTrace trace = getIncidentTrace(incidentCode, subincidentCode);
IncidentTrace firstTrace = trace;
if (trace.getAction() > 1) {
firstTrace = getFirstIncidentTrace(incidentCode, subincidentCode);
}
// Obtengo los datos para el resumen
long incidentId = fixedData.getId();
String description = fixedData.getDescription();
HERE IT HANGS=> String statusDsc = incStatus.getStatus().getDescription();
String priority = fixedData.getPriority().getDescription();
Date predictedDate = incStatus.getPredictedDate();
Date commitedDate = incStatus.getDateOfCommitment();
Date deadline = incStatus.getDeadline();
String type = fixedData.getFormType().getName();
String recomendation = trace.getRecomendation();
FormTypeOperation formTypeOper = trace.getFormTypeOperation();
int color = IncidentSumary.COLOR_GREEN;
if (DateUtilities.compare(predictedDate, currentDateTime) >= 0) {
color = IncidentSumary.COLOR_RED;
} else {
int daysNotification = fixedData.getSubMotive().getDaysNotification();
Date temp = DateUtilities.addDays(predictedDate, -daysNotification);
if (DateUtilities.compare(temp, DateUtilities.getCurrentDateTime()) >= 0) {
color = IncidentSumary.COLOR_YELLOW;
}
}
// Creo y agrego el registro de resumen a la lista
long priorityId = fixedData.getPriority().getId();
int lastAction = trace.getAction();
String motive = fixedData.getSubMotive().getGroupMotiveContactType().getMotive();
String submotive = fixedData.getSubMotive().getDescription();
String group = fixedData.getSubMotive().getGroupMotiveContactType().getGroup().getDescription();
String assignedTo = trace.getStatus().getAssignedTo().getName();
String supervisor = trace.getStatus().getSupervisor().getName();
ret.add(new IncidentSumary(incidentId, description, statusDsc, priority, predictedDate, commitedDate, type, recomendation, formTypeOper, incidentCode, subincidentCode, priorityId, color, lastAction, motive, submotive, group, firstTrace.getDateTime(), assignedTo, supervisor));
Collections.sort(ret);
}
And My XMLs:
IncidentStatus.hbm.xml:
..
<class name="uy.com.isbel.crmop.incidents.vo.IncidentStatus" table="FRM_INCIDENTES_ESTADOS" lazy="true">
<id type="long" name="id" column="ID" unsaved-value="-1">
<generator class="sequence">
<param name="sequence">CRMOP_INCESTADOS_SQ</param>
</generator>
</id>
<property type="long" name="idFormType" column="IDFORMULARIO" />
<property type="long" name="incident" column="CODINCIDENTE" />
<property type="long" name="subincident" column="CODSUBINCIDENTE" />
..
<many-to-one name="assignedTo" cascade="none">
<column name="ASIGNADOA"/>
</many-to-one>
<many-to-one name="supervisor" cascade="none">
<column name="IDSUPERVISOR"/>
</many-to-one>
<many-to-one name="status" cascade="none">
<column name="IDESTADO"/>
</many-to-one>
<many-to-one name="priority" cascade="none">
<column name="IDPRIORIDAD"/>
</many-to-one>
..
</class>
..
IncidentFixedData.hbm.xml:
..
<class name="uy.com.isbel.crmop.incidents.vo.IncidentFixedData" table="FRM_INCIDENTES_DATOS_FIJOS" lazy="true">
<id type="long" name="id" column="ID" unsaved-value="-1">
<generator class="sequence">
<param name="sequence">CRMOP_INCDFIJOS_SQ</param>
</generator>
</id>
<property type="long" name="incident" column="CODINCIDENTE" />
<property type="long" name="subincident" column="CODSUBINCIDENTE" />
...
<many-to-one name="channel" cascade="none">
<column name="IDCANAL"/>
</many-to-one>
<many-to-one name="formType" cascade="none">
<column name="IDFORMULARIO"/>
</many-to-one>
<many-to-one name="user" cascade="none">
<column name="IDUSUARIO"/>
</many-to-one>
<many-to-one name="client" cascade="none">
<column name="IDCLIENTE" />
</many-to-one>
<many-to-one name="contact" cascade="none">
<column name="IDCONTACTO" />
</many-to-one>
<many-to-one name="subMotive" cascade="none">
<column name="IDSUBCAT" />
</many-to-one>
<many-to-one name="priority" cascade="none">
<column name="PRIORIDAD"/>
</many-to-one>
...
</class>
..
User.hbm.xml
..
<class name="uy.com.isbel.crmop.security.vo.User" table="Usuarios" lazy="true">
<id name="id" type="long" column="IdUsuario" unsaved-value="-1">
<generator class="sequence">
<param name="sequence">USUARIOS_SQ</param>
</generator>
</id>
<property name="login" column="Nombre" type="string"/>
<property name="email" column="Email" type="string"/>
<property name="name" column="DESCRIPCION" type="string"/>
...
</class>
..
Status.hxm.xml
<class name="uy.com.isbel.crmop.incidents.vo.Status" table="ESTADOS" lazy="true">
<id type="long" name="id" column="IDENTIFICADOR" unsaved-value="-1">
<generator class="sequence">
<param name="sequence">CRMOP_ESTADOS_SQ</param>
</generator>
</id>
<property type="string" name="description" column="DESCRIPCION" />
</class>
Any help, will be appretiated.
Thank you ver much.
Marcelo
_________________ Computer Engineering Marcelo Giorgi.
|