2012-08-17 23 views
8

Tôi đang tạo ứng dụng JSF và sử dụng một số công cụ ngủ đông trong đó. Tất cả tôi muốn làm là để lưu các đối tượng vào cơ sở dữ liệu nhưng tôi cứ bị ngoại lệ này:org.hibernate.HibernateException: lưu không hợp lệ nếu không có giao dịch hoạt động

org.hibernate.HibernateException: save is not valid without active transaction 

Lúc đầu, tôi đã nhận được ngoại lệ này:

org.hibernate.HibernateException: No CurrentSessionContext configured! 

Sau đó, tôi phát hiện ra rằng tôi cần phải thêm điều này vào cấu hình ngủ đông của tôi:

<property name="hibernate.current_session_context_class">thread</property> 

Điều này giải quyết được vấn đề này nhưng hiện tại ở trên xuất hiện. Tôi đang tiết kiệm tổ chức vào cơ sở dữ liệu như thế này:

public void create(T entity) { 
    getSessionFactory().getCurrentSession().save(entity); 
} 

tập tin hibernate.cfg.xml của tôi trông như thế này:

<hibernate-configuration> 
    <session-factory> 
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/online_tests_management</property> 
     <property name="hibernate.connection.username">root</property> 
     <property name="hibernate.connection.password">root</property> 
     <property name="hibernate.show_sql">true</property> 
     <property name="hibernate.hbm2ddl.auto">update</property> 
     <property name="hibernate.current_session_context_class">thread</property> 

     <mapping class="com.groupgti.onlinetests.management.db.Service"/> 
    </session-factory> 
</hibernate-configuration> 

Tôi đang sử dụng:

  • Hibernate-4.1.4 .Final
  • JDK 1.6
  • Tomcat 6
  • JSF 2.0
  • PrimeFaces 3.3.1
  • MySql

Không ai biết ở đâu có thể là vấn đề?

Trả lời

20

Bạn phải gọi

public void create(T entity) { 
    Session session=getSessionFactory().getCurrentSession(); 
    Transaction trans=session.beginTransaction(); 
    session.save(entity); 
    trans.commit(); 
} 
+0

Cảm ơn bạn đã hoạt động. –

+0

Tôi đang sử dụng Spring HibernateTransactionManager thông qua Spring AOP để cấu hình các giao dịch. Tôi có thể thấy trong nhật ký giao dịch bắt đầu đúng nhưng lưu của tôi vẫn không thành công vì tôi không sử dụng mã ở trên. Tại sao vậy? –

3

Hãy thử thay đổi phương pháp của bạn để thực hiện như sau:

public void create(T entity) { 
    getSessionFactory().getCurrentSession().beginTransaction(); 
    getSessionFactory().getCurrentSession().save(entity); 
    getSessionFactory().getCurrentSession().endTransaction(); 
} 

nên giải quyết vấn đề của bạn.

3

Làm điều đó như thế này:

public void create(T entity) { 
     org.hibernate.Session ss= getSessionFactory().getCurrentSession(); 
     Transaction tx=ss.beginTransaction(); 
     ss.save(entity); 
     tx.commit();  
    } 

Làm xử lý một phần cho mình là ngoại lệ.

2

Tôi nghĩ rằng bạn sẽ tìm thấy một cái gì đó như thế này là mạnh mẽ hơn và thích hợp:

Session session = factory.openSession(); 
Transaction tx = null; 
try { 
    tx = session.beginTransaction(); 

    // Do some work like: 
    //session.load(...); 
    //session.persist(...); 
    //session.save(...); 

    tx.commit(); // Flush happens automatically 
} 
catch (RuntimeException e) { 
    tx.rollback(); 
    throw e; // or display error message 
} 
finally { 
    session.close(); 
} 

Bạn có thể không "gần gũi" giao dịch, bạn có thể đóng phiên giao dịch, như bạn có thể nhìn thấy. Đọc this here, nó có thể hữu ích cho bạn.

+0

Chăm sóc, "openSession" nên tránh. Phương pháp thích hợp là "getCurrentSession". –

Các vấn đề liên quan