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 đề?
Cảm ơn bạn đã hoạt động. –
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? –