I am using Spring's HibernateDAOSupport, which I think closes the session after every call.
----------------BaseDAOHibernate.java-------------
public class BaseDAOHibernate extends HibernateDaoSupport implements DAO {
protected final Log log = LogFactory.getLog(getClass());
/**
* @see com.testing.dao.DAO#saveObject(java.lang.Object)
*/
public void saveObject(Object o) {
getHibernateTemplate().saveOrUpdate(o);
}
/**
* @see com.testing.dao.DAO#getObject(java.lang.Class, java.io.Serializable)
*/
public Object getObject(Class clazz, Serializable id) {
Object o = getHibernateTemplate().get(clazz, id);
if (o == null) {
throw new ObjectRetrievalFailureException(clazz, id);
}
return o;
}
/**
* @see com.testing.dao.DAO#getObjects(java.lang.Class)
*/
public List getObjects(Class clazz) {
return getHibernateTemplate().loadAll(clazz);
}
/**
* @see com.testing.dao.DAO#removeObject(java.lang.Class, java.io.Serializable)
*/
public void removeObject(Class clazz, Serializable id) {
getHibernateTemplate().delete(getObject(clazz, id));
}
}
--------------------------------------------------------------------------
------------------UserDAOHibernate.java------------------------------
public class UserDAOHibernate extends BaseDAOHibernate implements UserDAO {
/**
* @see com.testing.dao.UserDAO#getUser(java.lang.String)
*/
public User getUser(Integer userId) {
User user = (User) getHibernateTemplate().get(User.class, userId);
if (user == null) {
log.warn("uh oh, user '" + userId + "' not found...");
throw new ObjectRetrievalFailureException(User.class, userId);
}
return user;
}
/**
* @see com.testing.dao.UserDAO#getUser(java.lang.String)
*/
public User getUser(String username) {
List list = getHibernateTemplate().find("from User u where u.username=?", new Object[]{username});
Iterator iter = list.iterator();
if (iter.hasNext())
return (User)iter.next();
else
return null;
}
/**
* @see com.testing.dao.UserDAO#getUsers(com.testing.model.User)
*/
public List getUsers(User user) {
return getHibernateTemplate().find("from User");
}
/**
* @see com.testing.dao.UserDAO#saveUser(com.testing.model.User)
*/
public void saveUser(final User user) {
if (log.isDebugEnabled()) {
log.debug("user's id: " + user.getUsername());
}
getHibernateTemplate().saveOrUpdate(user);
// necessary to throw a DataIntegrityViolation and catch it in UserManager
getHibernateTemplate().flush();
}
/**
* @see com.testing.dao.UserDAO#removeUser(java.lang.Integer)
*/
public void removeUser(Integer userId)
{
User user = getUser(userId);
removeUserCookies(user.getUsername());
getHibernateTemplate().delete(user);
}
/**
* @see com.testing.dao.UserDAO#removeUser(java.lang.String)
*/
public void removeUser(String userName)
{
User user = getUser(userName);
removeUserCookies(userName);
getHibernateTemplate().delete(user);
}
}
-------------------------------------------------------------------
---------------------User.java------------------------------------
package com.testing.model;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
/**
* User class
* @hibernate.class table="app_user"
*/
public class User extends BaseObject implements Serializable {
private static final long serialVersionUID = 3832626162173359411L;
private Integer id;
protected String username;
protected String password;
protected String confirmPassword;
protected String firstName;
protected String lastName;
protected Address address = new Address();
protected String phoneNumber;
protected String email;
protected String website;
protected String passwordHint;
protected Integer version;
protected Boolean superAdmin;
protected Boolean enabled;
protected Set projectRoles = new HashSet();
public User() {
}
public User(String username) {
this.username = username;
}
/**
* @hibernate.id column="uid" generator-class="native" unsaved-value="null"
*
* @return auto generated id of the user
*/
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
/**
* Returns the username.
*
* @return String
*
* @struts.validator type="required"
* @hibernate.property column="username" length="20" not-null="true" unique="true"
*/
public String getUsername() {
return username;
}
/**
* Returns the password.
* @return String
*
* @struts.validator type="required"
* @struts.validator type="twofields" msgkey="errors.twofields"
* @struts.validator-args arg1resource="userForm.password"
* @struts.validator-args arg1resource="userForm.confirmPassword"
* @struts.validator-var name="secondProperty" value="confirmPassword"
* @hibernate.property column="password" not-null="true"
*/
public String getPassword() {
return password;
}
/**
* Returns the confirmedPassword.
* @return String
*
* @struts.validator type="required"
*/
public String getConfirmPassword() {
return confirmPassword;
}
/**
* Returns the firstName.
* @return String
*
* @struts.validator type="required"
* @hibernate.property column="first_name" not-null="true" length="50"
*/
public String getFirstName() {
return firstName;
}
/**
* Returns the lastName.
* @return String
*
* @struts.validator type="required"
* @hibernate.property column="last_name" not-null="true" length="50"
*/
public String getLastName() {
return lastName;
}
public String getFullName() {
return firstName + ' ' + lastName;
}
/**
* Returns the address.
*
* @return Address
*
* @hibernate.component
*/
public Address getAddress() {
return address;
}
/**
* Returns the email. This is an optional field for specifying a
* different e-mail than the username.
*
* @return String
*
* @struts.validator type="required"
* @struts.validator type="email"
* @hibernate.property name="email" not-null="true" unique="true"
*/
public String getEmail() {
return email;
}
/**
* Returns the phoneNumber.
*
* @return String
*
* @struts.validator type="mask" msgkey="errors.phone"
* @struts.validator-var name="mask" value="${phone}"
* @hibernate.property column="phone_number" not-null="false"
*/
public String getPhoneNumber() {
return phoneNumber;
}
/**
* Returns the website.
*
* @return String
*
* @struts.validator type="required"
* @hibernate.property column="website" not-null="false"
*/
public String getWebsite() {
return website;
}
/**
* Returns the passwordHint.
*
* @return String
*
* @struts.validator type="required"
* @hibernate.property column="password_hint" not-null="false"
*/
public String getPasswordHint() {
return passwordHint;
}
/**
* Sets the username.
* @param username The username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* Sets the password.
* @param password The password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Sets the confirmedPassword.
* @param confirmPassword The confirmed password to set
*/
public void setConfirmPassword(String confirmPassword) {
this.confirmPassword = confirmPassword;
}
/**
* Sets the firstName.
*
* @param firstName The firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* Sets the lastName.
* @param lastName The lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* Sets the address.
* @param address The address to set
*/
public void setAddress(Address address) {
this.address = address;
}
/**
* Sets the email.
* @param email The email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* Sets the phoneNumber.
* @param phoneNumber The phoneNumber to set
*/
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
/**
* Sets the website.
* @param website The website to set
*/
public void setWebsite(String website) {
this.website = website;
}
/**
* @param passwordHint The password hint to set
*/
public void setPasswordHint(String passwordHint) {
this.passwordHint = passwordHint;
}
/**
* @return Returns the updated version.
* @hibernate.version
*/
public Integer getVersion() {
return version;
}
/**
* The updated version to set.
* @param version
*/
public void setVersion(Integer version) {
this.version = version;
}
/**
* @return Returns the enabled.
* @hibernate.property column="enabled"
*/
public Boolean getEnabled() {
// isEnabled doesnt' work for copying properties to Struts ActionForms
return enabled;
}
/**
* @param enabled The enabled to set.
*/
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
final User user = (User) o;
if (username != null ? !username.equals(user.getUsername()) : user.getUsername() != null) return false;
return true;
}
public int hashCode() {
return (username != null ? username.hashCode() : 0);
}
/**
* Generated using Commonclipse (
http://commonclipse.sf.net)
*/
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
// .append("roles", this.roles)
.append("firstName", this.firstName).append("lastName",
this.lastName)
.append("passwordHint", this.passwordHint).append("username",
this.username).append("fullName", this.getFullName())
.append("email", this.email).append("phoneNumber",
this.phoneNumber).append("password", this.password)
.append("address", this.address).append("confirmPassword",
this.confirmPassword).append("website", this.website)
.append("version", this.getVersion())
.append("enabled", this.getEnabled()).toString();
}
/**
* @return Returns the superAdmin property.
* @hibernate.property column="superadmin"
*/
public Boolean getSuperAdmin()
{
return superAdmin;
}
public void setSuperAdmin(Boolean superAdmin)
{
this.superAdmin = superAdmin;
}
/**
* Returns the user's roles for different projects
*
* @return Set
*
* @hibernate.set table="user_project_role" cascade="save-update" lazy="false" outer-join="true" fetch="join"
* @hibernate.collection-key column="user_id"
* @hibernate.collection-one-to-many class="com.testing.model.ProjectRole"
*/
public Set getProjectRoles()
{
return projectRoles;
}
public void setProjectRoles(Set projectRoles)
{
this.projectRoles = projectRoles;
}
public void addProjectRole(ProjectRole pRole)
{
this.getProjectRoles().add(pRole);
}
public void removeProjectRole(ProjectRole pRole)
{
this.getProjectRoles().remove(pRole);
}
}
----------------------------------------------------------------------
Thanks