简体   繁体   中英

transaction is getting started in hibernate 4.3.6

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)

You forgot to begin the transaction:

Transaction tx = session.getTransaction();
tx.begin(); // Add this line

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM