Арт*_*нко 5 java spring hibernate
我试图基于GenericDao制作web服务.我有Person Entity,我试图为GenericDao扩展的Entity制作dao.但是@Transactional注释不会打开事务:TransactionSynchronizationManager.isActualTransactionActive() = false
我找不到我想念的地方@Transactional.我试图@Transactional几乎随处可见.感觉就像注释不是从GenericDao继承的..
春天-config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://127.0.0.1:5432/test" />
<property name="username" value="postgres" />
<property name="password" value="postgress" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="annotatedClasses">
<list>
<value>org.entity.nci.person.Person</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>
<property name="packagesToScan" value="com.byteslounge.spring.tx.model" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</bean>
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
</beans>
Run Code Online (Sandbox Code Playgroud)
GenericDao.java
package org.dao.nci.person;
import java.io.Serializable;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebService;
import org.springframework.transaction.annotation.Transactional;
public interface GenericDao<E,K> {
@WebMethod(exclude = true)
public String add(List<E> entities) ;
@WebMethod(exclude = true)
public String saveOrUpdate(E entity) ;
@WebMethod(exclude = true)
public String update(E entity, String whereClause) ;
@WebMethod(exclude = true)
public String remove(E entity);
@WebMethod(exclude = true)
public E find(K key);
@WebMethod(exclude = true)
public List<E> get(String whereClause) ;
}
Run Code Online (Sandbox Code Playgroud)
GenericDaoImpl.java
package org.dao.nci.person;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import javax.jws.WebService;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@SuppressWarnings("unchecked")
@Repository
public abstract class GenericDaoImpl<E, K extends Serializable>
implements GenericDao<E, K> {
@Autowired
private SessionFactory sessionFactory;
protected Class<? extends E> daoType;
public GenericDaoImpl() {
Type t = getClass().getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) t;
daoType = (Class) pt.getActualTypeArguments()[0];
}
protected Session currentSession() {
return sessionFactory.getCurrentSession();
}
@Override
@Transactional
public String add(List<E> entities) {
for(Object entity : entities){
currentSession().save(entity);
}
return "test";
}
@Override
@Transactional
public String saveOrUpdate(E entity) {
currentSession().saveOrUpdate(entity);
return "test";
}
@Override
@Transactional
public String update(E entity, String whereClause) {
currentSession().saveOrUpdate(entity);
return "test";
}
@Override
@Transactional
public String remove(E entity) {
currentSession().delete(entity);
return "test";
}
@Override
@Transactional
public E find(K key) {
Session session = currentSession();
Transaction transaction = session.beginTransaction();
transaction.commit();
//session.close();
return (E) session.get(daoType, key);
}
@Override
@Transactional
public List<E> get(String filter) {
List<E> records = currentSession().createCriteria(daoType).list();
return records;
}
}
Run Code Online (Sandbox Code Playgroud)
PersonDao.java
package org.dao.nci.person;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlRootElement;
import org.entity.nci.person.Person;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@WebService
@XmlRootElement(name = "ComplexService")
@Transactional
public interface PersonDao extends GenericDao<Person, String>{
@WebMethod(operationName = "selectPerson")
@WebResult(name="row")
@Override
@Transactional
public List<Person> get(@WebParam(name="filter") String whereClause);
@WebMethod(operationName = "insertPerson")
@Override
@Transactional
public String add(@WebParam(name="row") List<Person> persons);
}
Run Code Online (Sandbox Code Playgroud)
PersonDaoImpl.java
package org.dao.nci.person;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.entity.nci.person.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@WebService(endpointInterface = "example.catalog.ProductCatalogService",
serviceName = "ProductCatalogService")
@Repository
@Transactional
public class PersonDaoImpl extends GenericDaoImpl<Person, String>
implements PersonDao {
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
我试过两个建议:
PersonDaoImpl.java中的覆盖方法如下:
package org.dao.nci.person;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.entity.nci.person.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;
@WebService(endpointInterface = "example.catalog.ProductCatalogService",
serviceName = "ProductCatalogService")
public class PersonDaoImpl extends GenericDaoImpl<Person, String>
implements PersonDao {
@Override
@Transactional
public String add(List<Person> entities){
return super.add(entities);
}
}
Run Code Online (Sandbox Code Playgroud)
我试着设置proxy-target-class ="false"
两者都没有帮助..方法完成没有任何错误但没有提交.TransactionSynchronizationManager.isActualTransactionActive()仍然是假的.
Pac*_*ace -1
来自Spring 文档:
Spring 建议您仅使用 @Transactional 注释来注释具体类(以及具体类的方法),而不是注释接口。您当然可以将 @Transactional 注释放在接口(或接口方法)上,但这仅在您使用基于接口的代理时按照您期望的方式工作。Java 注释不是从接口继承的事实意味着,如果您使用基于类的代理 (proxy-target-class="true") 或基于编织的切面 (mode="aspectj"),则事务设置为不被代理和编织基础设施识别,并且该对象不会被包装在事务代理中,这绝对是糟糕的。
我相信基于类的代理是默认的,所以您要么需要改变它,要么想出一种新的方法来解决您的问题。
| 归档时间: |
|
| 查看次数: |
477 次 |
| 最近记录: |