[英]spring mvc + hibernate: createSQLQuery is not valid without active transaction
我的代碼的舊版本:
Session session = sessionFactory.openSession();
Query query = session.createQuery("From User");
List<Users> users = query.list();
session.close();
我配置了hibernate.cfg.xml文件:
<property name =“ hibernate.current_session_context_class”>線程</ property>
context.xml
<tx:annotation驅動的transaction-manager =“ transactionManager” />
當前代碼:
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("From User");
List<Users> list = query.list();
tx.commit();
我必須添加交易代碼,否則會出現錯誤
org.hibernate.HibernateException:如果沒有活動事務,createQuery無效
我缺少什么配置?
ApplicationContext.xml
<?xml version='1.0' encoding='UTF-8' ?>
......
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<context:annotation-config />
<context:component-scan base-package="*" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
public class IndexController {
@Autowired
UserService users;
@RequestMapping("/index")
public String index(ModelMap model ) {
User user= users.test();
model.put("user", user);
return "index";
}
如建議的@ eg04lt3r,您不需要在hibernate.cfg.xml
這一行
<property name="hibernate.current_session_context_class">thread</property>
您需要正確配置spring上下文。
對於會話工廠,如果在hibernate.cfg.xml
和hibernate.properties
具有映射和屬性
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" />
用於交易管理
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
在適用於會話的課程中
public class Service {
@Autowired
private SessionFactory sessionFactory;
@Transactional
public List<User> list() {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from User");
return (List<User>) query.list();
}
}
當然,您無法通過這種方式吸引用戶
new Service().list()
您需要從spring上下文獲得Service
。 Spring將設置sessionFactory
的Service
,並返回一個代理Service
。 當您調用list()
,代理打開會話並創建事務。
更新資料
我重現您的問題。 通過此類,一切正常
public interface TextService {
public void save();
public List<SingleText> test();
}
@Service
public class TextServiceImpl implements TextService {
@Autowired
private SessionFactory sessionFactory;
@Override
@Transactional
public void save() {
SingleText text = new SingleText();
text.setTestText("S.Grey");
Session session = sessionFactory.getCurrentSession();
session.save(text);
}
@Override
@Transactional
public List<SingleText> test() {
Session session = sessionFactory.getCurrentSession();
return session.createCriteria(SingleText.class).list();
}
}
沒有@Transactional
注釋,我得到了
org.hibernate.HibernateException: No Session found for current thread
或(取決於Spring版本)
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
因此,您只需要@Transaction
批注。 而且你不需要這個
<property name="hibernate.current_session_context_class">thread</property>
如果不使用JTA,只需刪除此屬性。 Spring默認執行管理事務。
並在spring配置中定義事務管理器:
<bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name = "sessionFactory" ref = "sessionFactory" />
</bean>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.