我正在尝试使用Spring 4.0.5使用@Transactional注释,并使用mysql db来使用hibernate。
=====以下是代码=====
模型类是:
package com.vipin.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="USER_INFO")
public class UserInfo {
@Id
@Column(name="ssnID")
private int ssnID;
private String firstName;
private String lastName;
public int getSsnID() {
return ssnID;
}
public void setSsnID(int ssnID) {
this.ssnID = ssnID;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
声明数据库操作的dao接口。
DBOperationsDao.java
package com.vipin.dao;
import com.vipin.model.UserInfo;
public interface DBOperationsDao {
public UserInfo getUserInfo(int ssnId);
public void updateUserInfo(int ssnId);
public void deleteUser(int ssnId);
public void addUser(UserInfo userInfo);
}
Dao接口实现(简单一个)
DBOperationsDaoImpl.java
package com.vipin.dao;
import com.vipin.model.UserInfo;
import org.hibernate.SessionFactory;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.springframework.transaction.annotation.Transactional;
public class DBOperationsDaoImpl implements DBOperationsDao {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource(name="sessionFactory")
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
System.out.println("Inside setSessionFactory() method, value of this.sessionFactory is " + this.sessionFactory);
}
@Override
public UserInfo getUserInfo(int ssnId) {
// TODO Auto-generated method stub
return null;
}
@Override
public void updateUserInfo(int ssnId) {
// TODO Auto-generated method stub
}
@Override
public void deleteUser(int ssnId) {
// TODO Auto-generated method stub
}
@Override
@Transactional
public void addUser(UserInfo userInfo) {
Session session = this.sessionFactory.openSession();
System.out.println("Inside addUser() method.. value of session is " + session);
//session.persist(userInfo);
session.save(userInfo);
}
}
我正在努力实现使用@Transactional注释来执行事务管理,使用Spring 4.0.6和Hibernate。
以下是Spring.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<tx:annotation-driven proxy-target-class="true" transaction- manager="transactionManager"/>
<context:component-scan base-package="com.vipin.model"/>
<context:component-scan base-package="com.vipin.dao"/>
<!-- Here we are using apache commons dbcp2 implementation of DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/SPRINGDB" />
<property name="username" value="mysql"/>
<property name="password" value="mysql"/>
</bean>
<bean id="dao" class="com.vipin.dao.DBOperationsDaoImpl"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.vipin.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<context:annotation-config/>
</beans>
主要应用是:
MainApp.java
package com.vipin.app;
import com.vipin.dao.DBOperationsDao;
import com.vipin.dao.DBOperationsDaoImpl;
import com.vipin.model.UserInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
UserInfo userAdd = new UserInfo();
userAdd.setSsnID(1);;
userAdd.setFirstName("Vipin");
userAdd.setLastName("koul");
System.out.println("Inside main...");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:/META-INF/spring.xml");
DBOperationsDao dao = context.getBean("dao", DBOperationsDaoImpl.class);
dao.addUser(userAdd);
}
}
当我运行此程序时,没有行添加到DB,也没有错误/异常。
我在谷歌和这个网站上查看过各种帖子,但没有得到任何线索。任何帮助都会很棒