I am trying to follow the recommended way of manipulating relationships in a pojo.
My situation is as follows. I am dealing with a many to many relationship. I am trying the following code and keep getting a net.sf.hibernate.LazyInitializationException.
log.info("Entered saveTechnologyForUser(Integer techId)");
public void saveTechnologyForUser(SecuranceUserVO user, Integer techId) throws DaoException {
log.info("Entered saveTechnologyForUser(Integer techId)");
try{
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
TechnologyVO tech = new TechnologyVO();
tech.setTechnologyId(techId);
user.addTechnology(tech);
//update it
session.saveOrUpdate(user);
tx.commit();
log.info("Committed transaction and saved user");
HibernateUtil.closeSession();
}
catch(HibernateException hex){
log.error("Caught HibernateException in findUserByEmailAndPassword..", hex);
throw new DaoException(hex);
}
catch(Exception ex){
log.error("Caught Exception in saveTechnologyForUser(SecuranceUser user, Integer techId)", ex);
throw new DaoException(ex);
}
}
my convenience method to add a tech is:
public void addTechnology(TechnologyVO tech){
if(tech == null){
throw new IllegalArgumentException("Null TechnologyVO");
}
tech.getUsers().add(this);
techs.add(tech);
}
This gives me this exception:
ERROR [http8080-Processor1] (LazyInitializationException.java:25) - Failed to lazily initialize a collection - no Session
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no Session
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:213)
at net.sf.hibernate.collection.PersistentCollection.write(PersistentCollection.java:82)
at net.sf.hibernate.collection.Set.add(Set.java:155)
at com.securance.vo.SecuranceUserVO.addTechnology(SecuranceUserVO.java:201)
at com.securance.dao.MySqlSecuranceUserDAO.saveTechnologyForUser(MySqlSecuranceUserDAO.java:109)
at com.securance.audit.service.AuditToolServiceImpl.saveTechnologyForUser(AuditToolServiceImpl.java:336)
at com.securance.web.actions.auditapp.GenerateApplicationAuditStepsAction.execute(GenerateApplicationAuditStepsAction.java:100)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:551)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2422)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:163)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:199)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:700)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)
Can anyone pinpoint what I am doing wrong???
mapping docs:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="com.securance.vo.TechnologyVO"
table="technology"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="technologyId"
column="tech_id"
type="java.lang.Integer"
>
<generator class="native">
</generator>
</id>
<property
name="technologyName"
type="java.lang.String"
update="true"
insert="true"
column="technology"
/>
<property
name="description"
type="java.lang.String"
update="true"
insert="true"
column="description"
/>
<many-to-one
name="apType"
class="com.securance.vo.ApTypeVO"
cascade="none"
outer-join="false"
update="true"
insert="true"
column="ap_type_id"
/>
<set
name="steps"
table="ap_step_technology"
lazy="true"
inverse="true"
cascade="none"
sort="unsorted"
>
<key
column="tech_id"
/>
<many-to-many
class="com.securance.vo.ApStepVO"
column="ap_step_id"
outer-join="auto"
/>
</set>
<set
name="users"
table="user_technology"
lazy="true"
inverse="true"
cascade="none"
sort="unsorted"
>
<key
column="tech_id"
/>
<many-to-many
class="com.securance.vo.SecuranceUserVO"
column="user_id"
outer-join="auto"
/>
</set>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-TechnologyVO.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="com.securance.vo.SecuranceUserVO"
table="auditapp_user"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="userId"
column="user_id"
type="java.lang.Integer"
>
<generator class="native">
</generator>
</id>
<property
name="businessEntity"
type="java.lang.String"
update="true"
insert="true"
column="business_entity"
/>
<property
name="firstName"
type="java.lang.String"
update="true"
insert="true"
column="first_name"
/>
<property
name="lastName"
type="java.lang.String"
update="true"
insert="true"
column="last_name"
/>
<property
name="password"
type="java.lang.String"
update="true"
insert="true"
column="password"
/>
<property
name="phoneNumber"
type="java.lang.String"
update="true"
insert="true"
column="phone_number"
/>
<property
name="subscribed"
type="char"
update="true"
insert="true"
column="subscribed"
/>
<set
name="techs"
table="user_technology"
lazy="true"
inverse="false"
cascade="all"
sort="unsorted"
>
<key
column="user_id"
/>
<many-to-many
class="com.securance.vo.TechnologyVO"
column="tech_id"
outer-join="auto"
/>
</set>
<property
name="email"
type="java.lang.String"
update="true"
insert="true"
column="email"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-SecuranceUserVO.xml
containing the additional properties and place it in your merge dir.
-->
</class>
<query name="findByEmailAndPassword"><![CDATA[
from SecuranceUserVO user where user.email = :email and user.password = :password
]]></query>
</hibernate-mapping>
|