我一直在關注本教程: http://www.scribd.com/doc/25244173/Java-Struts-Spring-Hibernate-Tutorial 設置(前面描述)與教程文件工作正常,但是當我更改 - 刪除/更新操作不會發生。沒有錯誤或怪癖,它完全忽略了我!至於檢索數據 - 一切都很完美..休眠和彈簧加載操作做trich,更新/刪除操作不要
幾乎所有來自tut的文件都是相同的,有這些差異;本教程使用一個服務文件:
Services.java
package services;
import org.springframework.transaction.annotation.Transactional;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import data.*;
import java.util.List;
// This class is the business services tier in the application.
// @Transactional is needed so that a Hibernate transaction is set up,
// otherwise updates won't have an affect
@Transactional
public class Services {
// So Spring can inject the session factory
SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory value) {
sessionFactory = value;
}
// Shortcut for sessionFactory.getCurrentSession()
public Session sess() {
return sessionFactory.getCurrentSession();
}
public Event getEventById(long id) {
return (Event) sess().load(Event.class, id);
}
public Person getPersonById(long id) {
return (Person) sess().load(Person.class, id);
}
public void deleteEventById(long id) {
sess().delete(getEventById(id));
}
public void deletePersonById(long id) {
sess().delete(getPersonById(id));
}
public void createEvent(String name) {
Event theEvent = new Event();
theEvent.setName(name);
sess().save(theEvent);
}
public void createPerson(String name) {
Person p = new Person();
p.setName(name);
sess().save(p);
}
@SuppressWarnings("unchecked")
public List getEvents() {
return sess().createQuery("from Event").list();
}
@SuppressWarnings("unchecked")
public List getPeople() {
return sess().createQuery("from Person").list();
}
public void removePersonFromEvent(int personId, int eventId) {
getEventById(eventId).getPeople().remove(getPersonById(personId));
}
public void addPersonToEvent(int personId, int eventId) {
getEventById(eventId).getPeople().add(getPersonById(personId));
}
}
,我曾嘗試使用父控制器和靜態調用的HibernateUtil的文件分開: HibernateUtil.java
package com.epa.util;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class HibernateUtil {
// So Spring can inject the session factory
static SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory value) {
sessionFactory = value;
}
// Shortcut for sessionFactory.getCurrentSession()
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
BaseController.java
package com.epa.controller.base;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
import com.epa.controller.EventController;
import com.epa.controller.PersonController;
import com.epa.util.HibernateUtil;
@Transactional
public class BaseController {
protected SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
// Shortcut for sessionFactory.getCurrentSession()
public Session sess() {
return sessionFactory.getCurrentSession();
}
private PersonController personController = null;
private EventController eventController = null;
public PersonController getPersonController() {
if (this.personController == null) {
this.personController = new PersonController();
}
return personController;
}
public EventController getEventController() {
if (this.eventController == null) {
this.eventController = new EventController();
}
return eventController;
}
}
EventController.java
package com.epa.controller;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.epa.controller.base.BaseController;
import com.epa.model.Event;
@Transactional
public class EventController extends BaseController {
public Event getEventById(long id) {
return (Event) sess().load(Event.class, id);
}
public void deleteEventById(long id) {
sess().delete(getEventById(id));
}
public void createEvent(String name) {
Event theEvent = new Event();
theEvent.setName(name);
sess().save(theEvent);
}
@SuppressWarnings("unchecked")
public List getEvents() {
return sess().createQuery("from Event").list();
}
}
和春天的的applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- The singleton hibernate session factory -->
<bean id="sessionFactory" scope="singleton"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<!-- Spring's hibernate transaction manager, in charge of making hibernate sessions/txns -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- So classes/functions with @Transactional get a hibernate txn -->
<tx:annotation-driven />
<!-- Inject my business services class to the actions
<bean id="services" class="com.epa.services.Services" scope="singleton">
<property name="sessionFactory" ref="sessionFactory" />
</bean>-->
<!-- Inject my business services class to the actions -->
<bean id="hibernateUtil" class="com.epa.util.HibernateUtil" scope="singleton">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="baseController" class="com.epa.controller.base.BaseController" scope="singleton" />
</beans>