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 saving to a join table in many to many relationship
PostPosted: Thu Aug 12, 2004 12:55 am 
Beginner
Beginner

Joined: Mon Dec 08, 2003 12:15 am
Posts: 47
I have a many to many relationship solved with a join table.

My business requirements are such that there exists a many to many relationship between user and technology. This is resolved with the table user_technology.

So whenever I want to associate a user to a technology what I essentially need to do is an insert into the user_technology table. My code for that follows (with spring framework):

public void saveTechnologyForUser(final UserVO user, final TechnologyVO tech) {

HibernateTemplate template = getHibernateTemplate();
template.execute(
new HibernateCallback(){

public Object doInHibernate(Session session) throws HibernateException {

user.addTechnology(tech);
session.update(user);
log.info("Saved user with email " + user.getEmail());
return null;
}
}
);
}

What ends up happening is that hibernate does 4 queries. It updates the user table, it updates the technology table, it deletes from the join table (user_technology) based on the user_id and then it inserts into the user_technology table the user id and technology id. This seems like way too much work for what amounts to just an insert into the join table. Is there a way to do this so that you can get hibernate to just do an insert into the join table without the other queries. The other queries are un-necessary for my needs. On top of that, I need to keep the user's data in the join table and therefore I can't have hibernate delete it and then reinsert based on the user id. This is because a user may be associated to more than one technology

My mapping docs are below. The Technology object knows nothing about Users. I wanted Users to manage the relationship.

<?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>

Any help with this is greatly appreciated.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 12, 2004 8:24 am 
Beginner
Beginner

Joined: Mon Aug 02, 2004 1:08 pm
Posts: 42
Post your JavaBeans code.


Top
 Profile  
 
 Post subject: Re: Help saving to a join table in many to many relationship
PostPosted: Thu Aug 12, 2004 9:21 am 
Beginner
Beginner

Joined: Mon Dec 08, 2003 12:15 am
Posts: 47
/*
* Created on Jul 6, 2004
*
* To change the template for this generated file go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
package com.securance.vo;

import java.util.HashSet;
import java.util.Set;

/**
* @author SC
*
* @hibernate.class
* table="auditapp_user"
*
* @hibernate.query
* name="findByEmailAndPassword"
* query="from SecuranceUserVO user where user.email = :email and user.password = :password"
*
*/
public class SecuranceUserVO extends BaseVO {

private Set techs = new HashSet();
private Integer userId;
private String businessEntity;
private String firstName;
private String lastName;
private String phoneNumber;
private String password;
private char subscribed;
private boolean authenticated;
private String email;


/**
* @hibernate.property
* column="business_entity"
*/
public String getBusinessEntity() {
return businessEntity;
}

/**
* @hibernate.property
* column="first_name"
*/
public String getFirstName() {
return firstName;
}

/**
* @hibernate.property
* column="last_name"
*/
public String getLastName() {
return lastName;
}

/**
* @hibernate.property
* column="password"
*/
public String getPassword() {
return password;
}

/**
* @hibernate.property
* column="phone_number"
*/
public String getPhoneNumber() {
return phoneNumber;
}

/**
* @hibernate.property
* column="subscribed"
*/
public char getSubscribed() {
return subscribed;
}

/**
* @hibernate.set
* table="user_technology"
* inverse="false"
* lazy="true"
* cascade="all"
* @hibernate.collection-key
* column="user_id"
* @hibernate.collection-many-to-many
* class="com.securance.vo.TechnologyVO"
* column="tech_id"
*
*/
public Set getTechs() {
return techs;
}

/**
* @hibernate.id
* generator-class="native"
* column="user_id"
*/
public Integer getUserId() {
return userId;
}

/**
* @param string
*/
public void setBusinessEntity(String string) {
businessEntity = string;
}

/**
* @param string
*/
public void setFirstName(String string) {
firstName = string;
}

/**
* @param string
*/
public void setLastName(String string) {
lastName = string;
}

/**
* @param string
*/
public void setPassword(String string) {
password = string;
}

/**
* @param string
*/
public void setPhoneNumber(String string) {
phoneNumber = string;
}

/**
* @param c
*/
public void setSubscribed(char c) {
subscribed = c;
}

/**
* @param set
*/
public void setTechs(Set set) {
techs = set;
}

/**
* @param integer
*/
public void setUserId(Integer integer) {
userId = integer;
}

/**
* @return
*/
public boolean isAuthenticated() {
return authenticated;
}

/**
* @param b
*/
public void setAuthenticated(boolean b) {
authenticated = b;
}

/**
* @hibernate.property
* column="email"
*/
public String getEmail() {
return email;
}

/**
* @param string
*/
public void setEmail(String string) {
email = string;
}

//utility method to add a Technology to a use
public void addTechnology(TechnologyVO tech){
if(tech == null){
throw new IllegalArgumentException("Null TechnologyVO");
}
tech.getUsers().add(this);
techs.add(tech);
}

}


package com.securance.vo;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
* Created by IntelliJ IDEA.
* User: jalvarado
* Date: Jan 13, 2004
* Time: 2:17:43 AM
* To change this template use Options | File Templates.
*
* @hibernate.class table="technology"
*/
public class TechnologyVO implements Serializable {

private Integer technologyId;
private String technologyName;
private String description;
private ApTypeVO apType;
private Set steps = new HashSet();
private Set users = new HashSet();

/**
* @hibernate.id
* generator-class="native"
* column="tech_id"
*/
public Integer getTechnologyId() {
return technologyId;
}

public void setTechnologyId(Integer techId) {
this.technologyId = techId;
}
/**
* @hibernate.property
* column="technology"
*/
public String getTechnologyName() {
return technologyName;
}

public void setTechnologyName(String technologyName) {
this.technologyName = technologyName;
}
/**
* @hibernate.property
* column="description"
*/
public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

/**
* @hibernate.many-to-one
* column="ap_type_id"
* outer-join="false"
*/
public ApTypeVO getApType() {
return apType;
}

public void setApType(ApTypeVO apType) {
this.apType = apType;
}

/**
* @hibernate.set
* table="ap_step_technology"
* inverse="true"
* lazy="true"
* @hibernate.collection-key
* column="tech_id"
* @hibernate.collection-many-to-many
* class="com.securance.vo.ApStepVO"
* column="ap_step_id"
*
*/
public Set getSteps() {
return steps;
}

/**
* @param set
*/
public void setSteps(Set set) {
steps = set;
}

/**
* @hibernate.set
* table="user_technology"
* inverse="true"
* lazy="true"
* @hibernate.collection-key
* column="tech_id"
* @hibernate.collection-many-to-many
* class="com.securance.vo.SecuranceUserVO"
* column="user_id"
*
*/
public Set getUsers() {
return users;
}

/**
* @param set
*/
public void setUsers(Set set) {
users = set;
}

}


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.