b]Hibernate version: [/b] 2.1.8
Name and version of the database you are using: MySQL 4
JBoss version: 4.0.1sp1
Hello,
I've switched over from using the HibernateUtil to the new HibernateContext to obtain and use Hibernate Sessions.
I have the following usage scenerio:
Web (ServletContextListener) --> Command (EJB) --> DAO (Contains Hibernate Code (HibernateContext))
Problem
I can retrieve Hibernate Objects using the HibernateContext Utility but I cannot save (persist any). When I switch back to using the HibernateUtil method (as outlined in the Hibernate in Action book) it works, and all is persisted! Following is my configuration and code:
Config files:
hibernate-service.xml (deployed as a .har file)
Code:
<server>
<mbean code="org.jboss.hibernate.jmx.Hibernate"
name="jboss.har:service=Hibernate">
<attribute name="DatasourceName">
java:/SwiftyDS
</attribute>
<attribute name="Dialect">
net.sf.hibernate.dialect.MySQLDialect
</attribute>
<attribute name="SessionFactoryName">
java:/hibernate/SessionFactory
</attribute>
<attribute name="CacheProviderClass">
net.sf.hibernate.cache.HashtableCacheProvider
</attribute>
</mbean>
</server>
DAO files: Code:
public void updateStates(List states)
{
Session session = HibernateContext.getSession(IConstants.HIBERNATE_SESSION_FACOTRY);
List databaseStates = this.getAllStates();
for(Iterator it = states.iterator(); it.hasNext(); )
{
State state = (State) it.next();
try
{
if(!databaseStates.contains(state))
session.saveOrUpdate(state);
}
catch (HibernateException hex)
{
throw new InfrastructureException(this.getClass(), "Trouble while executing updateStates.", hex);
}
}
}
/**
* Gets all states
*
* @return a list of all states
*/
public List getAllStates()
{
Session session = HibernateContext.getSession(IConstants.HIBERNATE_SESSION_FACOTRY);
List states = new ArrayList();
try
{
states = session.createCriteria(State.class).list();
}
catch(HibernateException hex)
{
throw new InfrastructureException(this.getClass(), "Trouble while trying to acquire all states", hex);
}
return states;
}
Now, I can call the getAllStates method and it works fine, however, the State objects are not getting persisted to the database.
Funny thing is though when I make a call (from the web listener) directly with NO EJB involvement, with the following code, everything works fine (all data is entered into the DB):
Code:
try
{
HibernateUtil.beginTransaction();
StateDAO stateDAO = new StateDAO();
//UPDATE STATES
stateDAO.updateStates(digester.getStates());
//COMMIT (SAVE)
HibernateUtil.commitTransaction();
}
finally
{
HibernateUtil.closeSession();
}
Would anyone have a idea as to why the HibernateContext is not saving the objects' state?