-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Help with many to many association
PostPosted: Mon Jul 26, 2004 4:25 pm 
Beginner
Beginner

Joined: Mon Dec 08, 2003 12:15 am
Posts: 47
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>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 26, 2004 4:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
you must associate the user with the session, before doing anything that would initialize its lazy collection


Top
 Profile  
 
 Post subject: Re:Help with many to many association
PostPosted: Tue Jul 27, 2004 5:27 am 
Beginner
Beginner

Joined: Mon Dec 08, 2003 12:15 am
Posts: 47
Okay I associated the user with the session like this:

Query q = session.createQuery("from SecuranceUserVO user where user.userId = " + user.getUserId());
SecuranceUserVO s = (SecuranceUserVO)q.list().get(0);

then I call my utility method:
s.addTechnology(tech);

and then I save, commit, close session.

The problem with doing it this way is that no sql is being generated for the join table whatsoever. Hibernate tries to update the technology table instead of the join table. On top of that, that update doesn't even work because I get an exception java.sql.BatchUpdateException: General error, message from server: "Lock wait timeout exceeded; Try restarting transaction"

What I am trying to do is here is so simple yet Hibernate seems to be making it so hard. I just want to do an insert into a join table of a many to many relationship.

Can someone point me to some example code of how to do this.

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.