christian wrote:
http://www.hibernate.org/Documentation/SessionAndTransactionScope
Thank you very much, I ever read this article, and followed the
session-per-application-transaction pattern.
But I got into the following Exception:
Code:
Caused by: net.sf.hibernate.HibernateException: [b]You may not dereference a collection with cascade="all-delete-orphan"[/b]
net.sf.hibernate.impl.SessionImpl.updateUnreachableCollection(SessionImpl.java:2885)
net.sf.hibernate.impl.SessionImpl.flushCollections(SessionImpl.java:2754)
net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2225)
net.sf.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1769)
net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1536)
net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1501)
net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
hibernate.SessionUtil.executeQuery(SessionUtil.java:107)
How this exception occured:
1) The entities relationship is
Order(one) --
OrderItems(many)
2) I query out a list of
Order(s) in the first request/response cycle.
3) From
Order(s) query result, result entities stored in HttpSession, I pick one record and launch secend request/response cycle to its items detail.
4) Then the exception is occured.
Below is my Hibernate session controller source, please note that session is not closed. And it will be much appreciated it if you indicate the problem is:
Code:
public class SessionController implements Filter {
private static ThreadLocal hSessionHolder = new ThreadLocal();
private static SessionFactory sessionFactory;
static {
Configuration cfg;
try {
cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
}
catch (HibernateException e) {
throw new RuntimeException("Fail to configure hibernate", e);
}
}
public static Session getSession() {
Session s = (Session) hSessionHolder.get();
if (s == null || !s.isOpen()) {
s = initSession();
hSessionHolder.set(s);
}
return s;
}
public void init(FilterConfig arg0) throws ServletException {
// empty
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
final String HSESSION = "hibernate.session";
HttpSession httpSession = ((HttpServletRequest)request).getSession();
Session session = (Session) httpSession.getAttribute(HSESSION);
try {
try {
if (session == null) {
session = initSession();
httpSession.setAttribute(HSESSION, session);
}
else {
if (!session.isConnected())
session.reconnect();
}
hSessionHolder.set(session);
chain.doFilter(request, response);
}
finally {
session = (Session) hSessionHolder.get();
hSessionHolder.set(null);
if (session != null) {
//session.clear();
session.disconnect();
}
}
}
catch (HibernateException e) {
e.printStackTrace();
throw new RuntimeException("Fail to close hibernate session", e);
}
}
public void destroy() {
//empty
}
private static Session initSession() {
Session session;
try {
session = sessionFactory.openSession();
return session;
}
catch (HibernateException e) {
e.printStackTrace();
throw new RuntimeException("Fail to open hibernate session.");
}
}
}