Reputation: 33
I am new to hibernate framework. I am using hibernate 4.3.6. My model class is
public class UserDetails{
@Id
private int userId;
private String Name;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
following is my hibernate configuration
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/GHAC</property>
<property name="connection.username">root</property>
<property name="connection.password"/>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping class="org.ghac.uday.UserDetails"/>
</session-factory>
</hibernate-configuration>
I am trying to store a record in hibernate using the following code
UserDetails user = new UserDetails();
user.setUserId(1);
user.setName("Uday Kiran");
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
Transaction tx = session.getTransaction();
session.save(user);
tx.commit();
session.close();
But I am always getting the following error. Please suggest what am I doing wrong ?
Sep 04, 2014 9:32:11 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
Sep 04, 2014 9:32:11 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started
at
org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:172)
at org.ghac.uday.example.HibernateTest.main(HibernateTest.java:28)
Upvotes: 0
Views: 433
Reputation: 277
you forgot to begin Transaction for hibernate. Try this:
Session session = SessionFactory.openSession();
Transaction trans = session.beginTransaction();
By the way, I advise you shouldn't write all hibernate profile in tag session-factory like that. Write hibernate profile include: dialect, show_sql or even hbm2dll in a separate file: hibernate.properties for example. It make a professional vision! Good luck
Upvotes: 0
Reputation: 21445
You forgot to begin the transaction:
Transaction tx = session.getTransaction();
tx.begin(); // Add this line
Upvotes: 1