Reputation: 2722
I am writing my first example for Inserting Record using Hibernate Framework .
To start with I am trying to following instructions given at URL
You can see my code at GIT Hibernate Basic
When calling below piece of code
public Long saveEmployee(Employee emp){
Session session = new Configuration().configure().buildSessionFactory().openSession();
session.beginTransaction();
Long id = (Long) session.save(emp);
session.getTransaction().commit();
session.close();
return id;
}
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root123</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- validate | update | create | create-drop -->
<!-- validate: validate the schema, makes no changes to the database.
update: update the schema.
create: creates the schema, destroying previous data.
create-drop: drop the schema at the end of the session. -->
<property name="hbm2ddl.auto">validate</property>
<!-- added as suggested in below answer-->
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
It throws org.hibernate.TransactionException: Unable to locate JTA UserTransaction
Details :
Is it neccessary to deploy it in Application Server ?
Whether we have to explicitly specify the JTAUsertransaction in Hibernate 4.1 version?
=============== Update ==============
With
hibernate.transaction.factory_class = org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
Solved this error and I can execute the program .
But I still wanted to know the reason why its not working when using org.hibernate.transaction.JTATransactionFactory
Upvotes: 2
Views: 3619
Reputation: 2722
When used
hibernate.transaction.factory_class = org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
Solved this error and I can execute the program.
I have deliberately posted it as an answer so it would be helpful for someone.
Upvotes: 2
Reputation: 2943
You would be missing the JTA configuration in your hibernate config file
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
Upvotes: 0