问题描述
这是配置:
< context:annotation-config />
< context:component-scan base-package =com.cinebot/>
< mvc:annotation-driven />
< bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =packagesToScan>
< list>
<值> com.cinebot.db.entity< /值>
< / list>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.HSQLDialect< / prop>
< prop key =hibernate.show_sql> true< / prop>
< /道具>
< / property>
< / bean>
< bean id =transactionManagerclass =org.springframework.orm.hibernate4.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
< tx:annotation-driven />
这是错误的代码:
@Transactional
public static< T> T get(Class< T>类,序列化id)抛出异常{
if(id == null)return null;
T obj =(T)HibernateUtil.getSessionFactory()。getCurrentSession()。get(classe,id);
return obj;
}
这是一个例外:
org.hibernate.HibernateException:如果没有活动事务,则get无效
这是一个实例:
package com.cinebot.db.entity;
//生成3-lug-2012 10.31.04 by Hibernate Tools 3.4.0.CR1
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
$ b $ **
*由hbm2java生成的Dettagli
* /
@Entity
@Table(name =dettagli,catalog =cinebot )
public class Dettagli实现java.io.Serializable {
private static final long serialVersionUID = 1L;
私人字符串nome;
私有字符串valore;
$ b $ public Dettagli(){
}
public Dettagli(String nome){
this.nome = nome;
}
public Dettagli(String nome,string valore){
this.nome = nome;
this.valore = valore;
$ b $ @ @
@Column(name =nome,unique = true,nullable = false,length = 32)
public String getNome(){
返回this.nome;
}
public void setNome(String nome){
this.nome = nome;
@Column(name =valore,length = 65535)
public String getValore(){
return this.valore;
}
public void setValore(String valore){
this.valore = valore;
}
}
我不明白为什么不是足够的@Transactional注释来自动打开事务。我错过了什么?
谢谢
可以重新检查以下几点(尽管我不会推荐使用相同的)。
- 您需要在控制器中注入/ autowire spring DAO。使用@Repository注释DAO
- 确保您的spring扫描并为由context:component-scan完成的DAO类创建bean。这里你的dao类应该在给定的包/子包内。
我建议在Controller和DAO之间使用服务层。将服务方法注释为@transactional,它在DAO中调用方法。请记住,您不应创建任何bean的新实例,以调用其中的方法,而是注入/ autowire Service-> Controller和DAO-> Service。
i have a trouble opening Hibernate transaction.
This is the configuration:
<context:annotation-config />
<context:component-scan base-package="com.cinebot" />
<mvc:annotation-driven />
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.cinebot.db.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />
And this is the faulty code:
@Transactional
public static <T> T get(Class<T> classe, Serializable id) throws Exception {
if(id==null) return null;
T obj = (T) HibernateUtil.getSessionFactory().getCurrentSession().get(classe, id);
return obj;
}
This is the exception:
org.hibernate.HibernateException: get is not valid without active transaction
This is an example entitiy:
package com.cinebot.db.entity;
// Generated 3-lug-2012 10.31.04 by Hibernate Tools 3.4.0.CR1
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Dettagli generated by hbm2java
*/
@Entity
@Table(name = "dettagli", catalog = "cinebot")
public class Dettagli implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String nome;
private String valore;
public Dettagli() {
}
public Dettagli(String nome) {
this.nome = nome;
}
public Dettagli(String nome, String valore) {
this.nome = nome;
this.valore = valore;
}
@Id
@Column(name = "nome", unique = true, nullable = false, length = 32)
public String getNome() {
return this.nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Column(name = "valore", length = 65535)
public String getValore() {
return this.valore;
}
public void setValore(String valore) {
this.valore = valore;
}
}
I can not understand why is not enought the @Transactional annotation to auto-open the transaction. What i missed?
Thanks
Based on the comments You can recheck below points(though I would not recommend using same)
- You need to inject/autowire spring DAO into your controller. Annotate the DAO with @Repository
- Make sure your spring scans and create bean for your DAO class which is done by context:component-scan. Here your dao class should be inside the given package/subpackage.
I would recommend to use service layer between your Controller and DAO. Annotate the service method as @transactional which calls method in DAO. Remember you should not create new instance of any bean in order to call methods in it but inject/autowire Service->Controller and DAO->Service.
这篇关于没有活动交易,获取无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!