Hi,
I was practising the JPA using hibernate. Below is the EJB "CopyOfItemDAOBean" (stateless, transaction-scoped) which invokes "updateItemState" method and passes detached object with modified cost value. But database was not showing the modified value. I created bean ItemDAOBean with TransactionManagementType.BEAN and manually flushed the EntityManager and the cost value got updated. I checked the logs and for "TransactionManagementType.CONTAINER" the flushing of persistence context was not done. As per the JPA specification when the transaction gets ended the corresponding EntityManager and Persistence context will be flushed and closed. I am not sure why this was not happening in this case. Can any one help me on this ?
Code:
@Stateless(mappedName = "itemDAOBean2")
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class CopyOfItemDAOBean implements ItemDAO {
/*
* @PersistenceContext( type=PersistenceContextType.EXTENDED, properties =
* @PersistenceProperty( name="org.hibernate.flushMode", value="MANUAL") )
*/
@PersistenceContext
private EntityManager em;
/*
* @Resource(name="java:comp/UserTransaction") UserTransaction utx;
*/
@Override
public Item addItem(Item i) {
System.out.println("addItem method called !!!");
em.persist(i);
return i;
}
@Override
public void updateItemState(Set<Item> iList) {
for (Item i : iList) {
em.merge(i);
}
}
}
Code:
@Stateless(mappedName="itemDAOBean")
@TransactionManagement(TransactionManagementType.BEAN)
public class ItemDAOBean implements ItemDAO {
@PersistenceContext
private EntityManager em;
@Resource(name="java:comp/UserTransaction")
UserTransaction utx;
@Override
public Item addItem(Item i) {
System.out.println( "addItem method called !!!");
try {
utx.begin();
em.persist(i);
em.flush();
utx.commit();
} catch (NotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RollbackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (HeuristicMixedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (HeuristicRollbackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
@Override
public void updateItemState(Set<Item> iList)
try {
utx.begin();
for(Item i : iList){
em.merge(i);
}
em.flush();
utx.commit();
} catch (NotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RollbackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (HeuristicMixedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (HeuristicRollbackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="em" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>myDataSource</jta-data-source>
<properties>
<!-- property name="hibernate.ejb.cfgfile" value="META-INF/hibernate.cfg.xml"/ -->
<property name="hibernate.transaction.factory_class"
value="org.hibernate.transaction.JTATransactionFactory" />
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.WeblogicTransactionManagerLookup" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>