Quote:
Hi all,
I’m trying to use Hibernate inside a BMP Entity bean and using CMT to manage the transactions to the database.
The code below tries to persist data inside EntityBean::ejbStore, but it doesn’t seem to write to the database unless Session::flush() is called.
According to the documentation, hibernate should be flushing and committing data to the database when a JTA/CMT transaction commits.
What am I doing wrong?
Note:- I've tried XA database/drivers too.
Thanks,
Gavin
Code:
[Hibernate Config]
<hibernate-configuration>
<session-factory name="boo_hoo">
<property name="hibernate.connection.datasource">java:jdbc/mySql</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<!--
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<property name="current_session_context_class">jta</property>
-->
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<!-- org.hibernate.transaction.WebSphereTransactionManagerLookup -->
<property name="show_sql">true</property>
<property name="debug">true</property>
<mapping resource="Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
Code:
[Person.hbm.xml]
<hibernate-mapping>
<class name=" PersonBean" table="person" dynamic-update="true" >
<id name="id" column="id"></id>
<property name="firstname" column="firstname" />
<property name="surname" column="surname" />
</class>
</hibernate-mapping>
Code:
public abstract class HibernateEntityBean implements EntityBean{
protected EntityContext myEntityCtx;
protected static SessionFactory sessionFactory;
static{
try{
Configuration config = new Configuration();
Configuration inputConfig = config.configure("Hibernate.cfg.xml");
sessionFactory = inputConfig.buildSessionFactory();
}
catch(Exception e){
System.out.println(e);
}
}
public void ejbLoad() {
Serializable pk = (Serializable) getEntityContext().getPrimaryKey();
Session session = getSession();
session.load(this,pk);
}
public void ejbStore() {
Session session = getSession();
session.update(this);
//session.flush(); Do I need this??!!
}
protected Session getSession(){
Session session = sessionFactory.getCurrentSession();
//session.setFlushMode(FlushMode.COMMIT);
return session;
}
...
Code:
[PersonBean]
public class PersonBean extends HibernateEntityBean{
private String firstname;
private String surname;
private Integer id;
public Integer ejbCreate(Integer key, String name1, String name2) throws javax.ejb.CreateException {
id = key;
firstname = name1;
surname = name2;
Session ses = getSession();
ses.save(this);
return key;
}
public Collection ejbFindByRange(Integer min,Integer max){
Session ses = getSession();
List result = ses.createQuery("select p.id from PersonBean p where p.id <= :max and p.id >= :min")
.setInteger("max",max.intValue())
.setInteger("min",min.intValue())
.list();
return result;
}
public Integer ejbFindByPrimaryKey(Integer key){
return key;
}
/* Getters and Setters here */
}
Quote:
Calling code inside a stateless session bean. I've tried CMT and BMT.
Note:- records 100-102 exist
Code:
Collection col = ph.findByRange(new Integer(100),new Integer(102));
Iterator it = col.iterator();
while(it.hasNext()){
Person p = (Person) it.next();
p.setSurname(p.getSurname()+"a");
}