2014-09-04 110 views
0

我是新來的Hibernate框架。我正在使用休眠4.3.6。我的模型類是事務正在休眠4.3.6

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; 
    } 
} 

以下是我的Hibernate配置

<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> 

我嘗試使用下面的代碼

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(); 

存儲在休眠的紀錄,但我總是得到跟隨錯誤。請建議我做錯了什麼?

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) 

回答

1

你忘了開始交易:

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

謝謝柴坦尼亞。添加這條線解決了我的問題 – 2014-09-05 14:37:59

+0

@UdayKiran,如果它有效,那麼你可以接受這個答案。 – Chaitanya 2014-09-05 15:31:54

0

你忘了開始交易的休眠。試試這個:

Session session = SessionFactory.openSession(); 
Transaction trans = session.beginTransaction(); 

順便說一句,我勸你不要應該寫在標籤會話的工廠一樣,所有的Hibernate配置文件。編寫hibernate profile包括:dialect,show_sql甚至hbm2dll在一個單獨的文件中:例如hibernate.properties。它做出了專業的眼光!祝你好運