-->
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: NonUniqueObjectException in Struts Action
PostPosted: Tue Oct 26, 2004 12:01 pm 
Newbie

Joined: Tue Oct 26, 2004 11:42 am
Posts: 3
I write Struts action with some utils class from http://caveatemptor.hibernate.org/
HibernateUtil and DAO pattern
I use session-per-request session with Hibernate filter as follow

<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>jq.com.vn.bean.util.HibernateFilter</filter-class>
</filter>

filter method

public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {

// There is actually no explicit "opening" of a Session, the
// first call to HibernateUtil.beginTransaction() in control
// logic (e.g. use case controller/event handler) will get
// a fresh Session.
try {
chain.doFilter(request, response);

// Commit any pending database transaction.
HibernateUtil.commitTransaction();

} finally {

// No matter what happens, close the Session.
HibernateUtil.closeSession();

}
}



Hibernate version:2.1.6

Mapping documents:
<class name="Contents" table="T_CONTENTS" proxy="Contents">
<id name="id" column="CONTENTS_ID" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
<property name="title" not-null="true" column="F_TITLE"/>
<property name="brief" column="F_BRIEF"/>
<property name="content" column="F_CONTENT"/>
<property name="createDate"
column="F_CREATED"
type="java.util.Date"
not-null="true"/>
<property name="status" column="F_STATUS"/>
<many-to-one
name="category"
column="CATEGORY_ID"
class="Category"
not-null="true"
/>
<set name="images"
lazy="true"
table="T_IMAGES"
order-by="F_TITLE asc">
<key column="CONTENTS_ID"/>
<composite-element class="Image">
<property name="title" column="F_TITLE"/>
<property name="description" column="F_DESCRIPTION"/>
<property name="path" column="F_PATH"/>
<property name="thumbnail" column="F_THUMBNAIL"/>
<property name="createDate"
column="F_CREATED"
type="java.util.Date"
not-null="true"/>
</composite-element>
</set>
</class>


DAO method
public void makePersistent(Contents contents)
throws InfrastructureException {

try {
contents.setCreateDate(new Date());
HibernateUtil.getSession().saveOrUpdate(contents);
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
}
Struts Action
private ActionForward updateContents(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
ContentsForm contentsForm=(ContentsForm) form;
ContentsDAO contentsDAO=new ContentsDAO();
Contents contents=new Contents().copyFrom(contentsForm);
contentsDAO.makePersistent(contents);
return mapping.findForward(Constant.SUCCESS);
}

when i new instant of object, it's work
but if I delete or update, thrown exception:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session


Anyone give the solution?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 26, 2004 12:20 pm 
Senior
Senior

Joined: Fri Jun 18, 2004 10:17 am
Posts: 140
not sure if this helps you out ... from hibernate API for the exception you are getting ...

This exception is thrown when an operation would break session-scoped identity. This occurs if the user tries to associate two different instances of the same Java class with a particular identifier, in the scope of a single Session.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 26, 2004 12:31 pm 
Newbie

Joined: Tue Oct 26, 2004 11:42 am
Posts: 3
Thank for reply soon
I know that is common problem, the reason I known, but the solution for Struts action not work yet


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.