我正在尝试将spring与hibernate集成并尝试在dao层中使用会话工厂 我的测试类看起来像这样:
package com.transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.transactionManagement.java.Employee;
import com.transactionService.service.TransactionService;
public class TransactionManagementTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Bean.xml");
Employee emp = (Employee)applicationContext.getBean("employee");
emp.setFirstName("vivek3");
emp.setLastName("krishna2");
emp.setAmount(2000);
TransactionService ts=(TransactionService) applicationContext.getBean("transactionService");
ts.insertEmployee(emp);
}
}
我创建了服务和dao接口 道: 包com.Transaction.dao;
import com.transactionManagement.java.Employee;
public interface Transaction {
public void insertEmployee(Employee emp);
public Employee getEmployeebyId(Integer employeeIdentifier);
public int updateSalaryfoeEmployee(Integer employeeIdentifier,Integer Salary);
}
服务:
package com.transactionService.service;
import com.transactionManagement.java.Employee;
public interface TransactionService {
public void insertEmployee(Employee emp);
public Employee getEmployeebyId(Integer employeeIdentifier);
public int updateSalaryfoeEmployee(Integer employeeIdentifier,Integer Salary);
}
相应的dao&服务impl:
package com.Transaction.dao.impl;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.Transaction.dao.Transaction;
import com.transactionManagement.java.Employee;
@Component("transactionDao")
public class TransactionDaoImpl implements Transaction {
@Autowired
private Employee employee;
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Autowired
private SessionFactory sessionFactory;
public void insertEmployee(Employee emp) {
sessionFactory.openSession().save(emp);
}
public Employee getEmployeebyId(Integer employeeIdentifier) {
// TODO Auto-generated method stub
return (Employee) sessionFactory.openSession().get(Employee.class, employeeIdentifier);
}
public int updateSalaryfoeEmployee(Integer employeeIdentifier,
Integer Salary) {
// TODO Auto-generated method stub
Query q= (Query) sessionFactory.openSession().createQuery("update Employee set Salary=:salary where id=:id")
.setParameter("salary", Salary)
.setParameter("id", employeeIdentifier);
return q.executeUpdate();
}
}
服务impl:
package com.transactionService.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.Transaction.dao.Transaction;
import com.transactionManagement.java.Employee;
import com.transactionService.service.TransactionService;
@Component("transactionService")
public class TransactionServiceImpl implements TransactionService{
@Autowired
private Transaction transactionDao;
public void insertEmployee(Employee emp) {
transactionDao.insertEmployee(emp);
}
public Employee getEmployeebyId(Integer employeeIdentifier) {
return transactionDao.getEmployeebyId(employeeIdentifier);
}
public int updateSalaryfoeEmployee(Integer employeeIdentifier,
Integer Salary) {
return transactionDao.updateSalaryfoeEmployee(employeeIdentifier, Salary);
}
}
对应的Beans.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<import resource="DataSource.xml"/>
<context:component-scan base-package="com.Transaction.dao"></context:component-scan>
<context:component-scan base-package="com.Transaction.dao.impl"></context:component-scan>
<context:component-scan base-package="com.transactionService.service"></context:component-scan>
<context:component-scan base-package="com.transactionService.service.impl"></context:component-scan>
<context:component-scan base-package="com.transactionManagement.java" />
<bean id="employee" class="com.transactionManagement.java.Employee"></bean>
<bean id="transactionDao" class="com.Transaction.dao.impl.TransactionDaoImpl">
<property name="employee" ref="employee"></property>
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="springTransactionTest" class="com.transaction.SpringTransactionManageTest">
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource"><ref bean="dataSource"/>
</property>
<property name="packagesToScan" value="com.transactionManagement.java" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
数据源文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="url" value="jdbc:jtds:sqlserver://PRAMOD-LAPTOP;databaseName=TestMart1;" />
<property name="username" value="sa" />
<property name="password" value="1234" />
</bean>
</beans>
输出跟踪:
Mar 05, 2017 1:20:37 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@25f38edc: startup date [Sun Mar 05 13:20:37 IST 2017]; root of context hierarchy
Mar 05, 2017 1:20:37 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Bean.xml]
Mar 05, 2017 1:20:38 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [DataSource.xml]
Mar 05, 2017 1:20:38 PM org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
INFO: Overriding bean definition for bean 'transactionDao': replacing [Generic bean: class [com.Transaction.dao.impl.TransactionDaoImpl]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [F:\Bhaskar\SpringTransactionManagement\target\classes\com\Transaction\dao\impl\TransactionDaoImpl.class]] with [Generic bean: class [com.Transaction.dao.impl.TransactionDaoImpl]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [Bean.xml]]
Mar 05, 2017 1:20:38 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@490ab905: defining beans [dataSource,transactionDao,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,transactionService,employee,springTransactionTest,sessionFactory,transactionManager,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Mar 05, 2017 1:20:38 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: net.sourceforge.jtds.jdbc.Driver
Mar 05, 2017 1:20:39 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Mar 05, 2017 1:20:39 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.18.Final}
Mar 05, 2017 1:20:39 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 05, 2017 1:20:39 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Mar 05, 2017 1:20:40 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
Mar 05, 2017 1:20:40 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Mar 05, 2017 1:20:40 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Mar 05, 2017 1:20:40 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: insert into Employee (amount, first_name, last_name) values (?, ?, ?)
问题:我没有使用@transactional但仍然插入正在进行。任何人都可以解释这个
此致 巴斯卡尔