I wrote a simple DAO stateless session bean following several article's recommendation. The bean deploys successfully in JBoss 3.2.1. So I called the insert method from a client, and no error msg displayed. But I checked my database, and nothing was inserted.
So I took converted my DAO code and changed it to the non-EJB version (where you explicitly begin and commit a transaction) and it worked! I'm wondering if I'm missing a declaration in hibernate.cfg.xml or something??
Here is the
EJB version of my code:
<ejb code>
Code:
Context ctx = new InitialContext();
String jndiName = "java:/hibernate/HibernateFactory";
SessionFactory factory = (SessionFactory)ctx.lookup(jndiName);
net.sf.hibernate.Session session = factory.openSession();
Cat princess = new Cat();
princess.setId(2);
princess.setName("Princess Deuce");
princess.setSex('F');
princess.setWeight(12.4f);
session.save(princess);
session.flush(); // tried w/ and w/out this line, no effect
session.close();
</ejb code>Plain vanilla version of code:
<vanilla code>Code:
net.sf.hibernate.SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();
net.sf.hibernate.Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Cat princess = new Cat();
princess.setId(1);
princess.setName("Princess Deuce");
princess.setSex('F');
princess.setWeight(12.4f);
session.save(princess);
transaction.commit();
</vanilla code>EJB hibernate.cfg.xmlCode:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:/TeradataDS</property>
<property name="dialect">net.sf.hibernate.dialect.TeradataDialect</property>
<property name="show_sql">true</property>
<property name="net.sf.hibernate.transaction.JTATransactionFactory">net.sf.hibernate.transaction.JBossTransactionManagerLookup</property>
<!-- Mapping files -->
<mapping resource="Cat.hbm.xml"/>
<mapping resource="Employee.hbm.xml"/>
<mapping resource="Department.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Vanilla hibernate.cfg.xmlCode:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">false</property>
<property name="dialect">net.sf.hibernate.dialect.TeradataDialect</property>
<!-- Mapping files -->
<mapping resource="Cat.hbm.xml"/>
<mapping resource="Department.hbm.xml"/>
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>