I have spent the last two hours going through the forum and I have yet to find a solution to my problem.
I have a many to many relation between auditapp_user and technology tables which is resolved by a table user_technology.
I retrieve a user in an earlier session and then what I need to do is associate a technology to it based on a technology id. This is the code I am using:
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);
}
}
The utility method in user is this:
public void addTechnology(TechnologyVO tech){
if(tech == null){
throw new IllegalArgumentException("Null TechnologyVO");
}
tech.getUsers().add(this);
techs.add(tech);
}
This does not work. I have the user side marked inverse=false like the docs suggest, but still it does not work.
I get a LazyInitializationException whenever I try this code.
I have a feeling this has to do with the fact that I am working with a previously loaded user in a separate session.
Any help would be greatly appreciated.
Thanks
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.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>
<?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>
|