cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brent Johnson <bljohn...@gmail.com>
Subject Using Hibernate via Java, not Flow
Date Wed, 13 Apr 2005 20:37:24 GMT
This is probably a simple question. I've read the Wiki about using Hibernate 
and most of that uses Hibernate directly in Flow. I'd rather not do that. 
I've created some "Wrapper" type classes that are intended to be used from 
Flow, and they handle the hibernate sessions. All they really do is return 
the POJO objects for display purposes in a JXTemplate. When a save is done, 
the flow populates a POJO object based on data from the request, and a 
save() function is called in the wrapper which handles the saving.

I'm using the regular HibernateUtil.java (using ThreadLocal) that comes with 
Hibernate (specifically version 3.0). That being said, is there any harm in 
calling a HibernateUtil.openSession() multiple times in my Java files from 
flow? Here's an example, in my flow I have the following code (modified for 
simplicity):

var types = Packages.my.package.wrappers.MyWrapper.getTypes();
var states = Packages.my.package.wrappers.UtilWrapper.getStates();

The getTPPTypes() call does a HibernateUtil.currentSession() to retrieve the 
session, starts a new transaction, and just does a simple Hibernate query, 
and returns a java.util.List. The getStates() call does basically the same 
thing, just returning a List of objects representing US states. Since the 
HibernateUtil is ThreadLocal, it should only create ONE session right? 
(since I'm not closing at the end of each call in the wrapper)

At the end of my flow, should I just make a call to something like 
Packages.my.package.wrappers.UtilWrapper.closeSession() or something?

The problem I'm having is this, using Cocoon 2.1.7 w/ flow and both 
Hibernate2 and 3, I've been getting these Hibernate exceptions:
org.hibernate.NonUniqueObjectException: a different object with the same 
identifier value was already associated with the session
OR
org.hibernate.HibernateException: Illegal attempt to associate a collection 
with two open sessions
OR
org.hibernate.HibernateException: illegally attempted to associate a proxy 
with two open Sessions

Although, to be honest, I forgot to make my calls at the end of the cocoon 
flow to close the session, so maybe thats why.

I just wanted some input from other Cocoon developers using Hibernate. Are 
most of you accessing Hibernate directly from Flow? Or doing something 
similar to what I am? If this is an idiotic way to do it, please feel free 
to tell me. I'm all for constructive criticism. I just want to make sure I'm 
doing this right before I get too far into this project.

Thanks,

- Brent

Mime
View raw message