- This topic has 6 replies, 3 voices, and was last updated 19 years, 6 months ago by Cliff Nguyen.
-
AuthorPosts
-
FrankMemberHello,
I get the following error when I try and update a record.
a different object with the same identifier value was already associated with the session: 8a8a94a903612f3c0103612f67260001, of class: com.stemc.AuthorMy code looks like
Session hibernateSession = null;
Transaction myTransaction = null;
Author myAuthor = null;if (request.getParameter(“Submit”) != null) {
try {
hibernateSession = HibernateSessionFactory.currentSession();
myTransaction = hibernateSession.beginTransaction();
myAuthor = new Author();
myAuthor.setAuthorId(request.getParameter(“authorID”));
myAuthor.setFirstName(request.getParameter(“first”));
myAuthor.setLastName(request.getParameter(“last”));
myAuthor.setHomePhone(request.getParameter(“homePhone”));
hibernateSession.update(myAuthor);
myTransaction.commit();
hibernateSession.flush();
response.sendRedirect(“index.jsp”);
return;
} catch (Exception e) {
out.println(e.getMessage().toString());
}
}try {
hibernateSession = HibernateSessionFactory.currentSession();
myTransaction = hibernateSession.beginTransaction();
myAuthor = new Author();
myAuthor = (Author)hibernateSession.load(Author.class, request.getParameter(“authorID”));
myTransaction.commit();
hibernateSession.flush();
} catch (Exception e2) {
out.println(e2.getMessage().toString());
}What am I doing wrong?
Thanks
Frank
Riyad KallaMemberReplace your hibernateSession.flush lines with .close(), you aren’t closing your sessions.
FrankMemberI did as you suggested, now my app fails with null values.
If I run http://localhost:8080/list_authors.jsp I get a listing.
If I try and refresh the page I get a session closed message.try {
hibernateSession = HibernateSessionFactory.currentSession();
myTransaction = hibernateSession.beginTransaction();
Criteria query = hibernateSession.createCriteria(Author.class);
query.addOrder(Order.asc(“lastName”));
authors = query.list().iterator();
myTransaction.commit();
hibernateSession.close();
} catch (Exception e) {
out.println(e.getMessage().toString());
} finally {
try { hibernateSession.close();}
catch (Exception e2) {;}
}
Riyad KallaMemberOk, try flush then close 😀
Moving to OT > Soft Dev, this is a straight Hibernate question BTW.
FrankMemberSorry if I posted wrong area. I did not mean to emply that ME was at fault.
I still get the session closed.Here is my complete source:
<%@ page language=”java” import=”java.util.*,net.sf.hibernate.*,com.stemc.*” %>
<%@ page import=”net.sf.hibernate.Session” %>
<%@ page import=”net.sf.hibernate.expression.Order” %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
Session hibernateSession = null;
Transaction myTransaction = null;
Iterator authors = null;try {
hibernateSession = HibernateSessionFactory.currentSession();
myTransaction = hibernateSession.beginTransaction();
Criteria query = hibernateSession.createCriteria(Author.class);
query.addOrder(Order.asc(“lastName”));
authors = query.list().iterator();
myTransaction.commit();
hibernateSession.flush();
hibernateSession.close();
} catch (Exception e) {
out.println(e.getMessage().toString());
} finally {
try { hibernateSession.close();}
catch (Exception e2) {;}
}
%><!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<base href=”<%=basePath%>”><title>My JSP ‘index.jsp’ starting page</title>
<meta http-equiv=”pragma” content=”no-cache”>
<meta http-equiv=”cache-control” content=”no-cache”>
<meta http-equiv=”expires” content=”0″>
<meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″>
<meta http-equiv=”description” content=”This is my page”><!–
<link rel=”stylesheet” type=”text/css” href=”styles.css”>
–>
</head><body>
<h1>Author Listing</h1> <br>
<% if (authors == null) { %>
No authors defined!
<% } else { %>
<table width=”100%” border=”0″>
<tr><th>Last Name</th><th>First Name</th></tr>
<% while (authors.hasNext()) {
Author myAuthor = (Author)authors.next(); %>
<tr><td><%=myAuthor.getLastName() %></td>
<td><%=myAuthor.getFirstName() %></td>
<td><a href=”edit_author.jsp?authorID=<%=myAuthor.getAuthorId()%>”>Edit</a></td>
<td><a href=”edit_author.jsp”>Delete</a></td></tr>
<% } } %>
</table>
<hr />
<p align=”right”><a href=”create_author.jsp”>Add Author</a></p>
</body>
</html>
FrankMemberI solved this error.
I was doing:hibernateSession = HibernateSessionFactory.currentSession();
hibernateSession.close();instead of:
hibernateSession = HibernateSessionFactory.currentSession();
HibernateSessionFactory.closeSession();Regards,
Frank
Cliff NguyenParticipanthi everybody,
Instead of using:
session.delete(YourObject);I used, everything goes ok:
session.delete(YourObject);
session.evict(YourObject); -
AuthorPosts