我使用spring4 + hibernate4创建了一个简单的项目。
我实现了CRUD函数,但我发现更新操作不能使用注释@Transactional
。
如果添加session.flush()
,则可以更新数据。
但我的代码中有@Transactional
,为什么我必须使用flush()
?
弹簧common.xml:
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/cang7"></property>
<property name="username" value="root"></property>
<property name="password" value="phpwind.net"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.study.user.domain</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
弹簧mvc.xml
<context:component-scan base-package="com.study.user">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<mvc:annotation-driven />
<mvc:resources location="/js/" mapping="/js/**"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
我的UserDAO:
@Repository
public class UserDAO implements IUserDAO {
private static final Logger logger = Logger.getLogger(UserDAO.class);
@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public User getUser(int id) {
String hql = "from User u where u.userId=?";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setInteger(0, id);
return (User) query.uniqueResult();
}
@SuppressWarnings("unchecked")
@Override
public List<User> getAllUser() {
String hql = "from User";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
return query.list();
}
@Override
public void addUser(User user) {
sessionFactory.getCurrentSession().save(user);
}
@Override
public boolean deleteUser(User user) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean updateUser(User user) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(user);
return false;
}
}
我的服务:
@Service("userService")
public class UserService implements IUserService {
@Resource
private IUserDAO userDAO;
@Override
@Transactional
public void updateUser(User user) {
userDAO.updateUser(user);
}
}
我的控制员:
@Controller
@RequestMapping("/user")
public class UserController {
@Resource(name = "userService")
private IUserService userService;
@RequestMapping("/getAllUser")
public String getAllUser(HttpServletRequest request) {
request.setAttribute("userList", userService.getAllUser());
return "/index";
}
@RequestMapping("/getUser")
public String getUser(int id, HttpServletRequest request) {
request.setAttribute("user", userService.getUser(id));
return "/editUser";
}
@RequestMapping("/toAddUser")
public String toAddUser() {
return "/addUser";
}
@RequestMapping("addUser")
public String addUser(User user, HttpServletRequest request) {
userService.addUser(user);
return "redirect:/user/getAllUser";
}
@RequestMapping("updateUser")
public String updateUser(User user, HttpServletRequest request) {
userService.updateUser(user);
return "redirect:/user/getAllUser";
}
}
答案 0 :(得分:0)
我已经解决了这个问题。
原因是UserService加载了两次。 我应该添加我的spring-mvc.xml
<context:component-scan base-package="com.study.user" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>