Hi all, could anybody tell me why this occured?
I want to save an entity, but that entity was loaded in previous request cycle. I kept that entity in the Visit. And that entity has a one-to-many collection.
I will attach my session controller in the last.
----------------------------------------------------------------------
Code:
Caused by: net.sf.hibernate.HibernateException: Found two representations of same collection
net.sf.hibernate.impl.SessionImpl.updateReachableCollection(SessionImpl.java:2836)
net.sf.hibernate.impl.FlushVisitor.processCollection(FlushVisitor.java:32)
net.sf.hibernate.impl.AbstractVisitor.processValue(AbstractVisitor.java:69)
net.sf.hibernate.impl.AbstractVisitor.processValues(AbstractVisitor.java:36)
net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2556)
net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2422)
net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2224)
net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2203)
net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
hibernate.SessionUtil.executeTxn(SessionUtil.java:130)
-----------------------------------------------------------------------
Below is the source I manage session in a Servlet Filter:
I don't want to use lock() to reattach session to enttiy. I think it is hard to implement. But is it correct to just disconnect() and clear() session after I complete an request-responseCode:
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
session.reconnect();
hSessionHolder.set(session);
chain.doFilter(request, response); //business is inside of filter chain
}
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() {
// bird.destory();
}
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.");
}
}
}
[/code]