2013-12-12 59 views
1

我已經閱讀了許多與我一樣的問題的主題,但它沒有解決,所以我決定寫在這裏。Spring&Hibernate不會保存我在數據庫中的記錄

問題是,當我想在我的項目中編輯或刪除記錄時,Java中實現的每個函數似乎運行正常,但數據庫中沒有任何更改。 例如在我的項目中,我有一個名爲'Oddzial'的模型(英文是部門)。 我可以添加新的Oddzial(部門)到數據庫,我會看到更改。但是,如果我想刪除所有內容,則沒有任何錯誤,但數據庫中也沒有任何更改。應該刪除的記錄仍然在數據庫中。

這裏是我的文件:

型號:

@Entity 
@Table(name="oddzial") 
public class Oddzial implements Serializable { 

private static final long serialVersionUID = 1L; 

@Id 
@GeneratedValue 
private Integer oddzial_id; 

@Size(min=3, max=25, message="test message.") 
private String miasto; 

@Size(min=5, max=50, message="test message.") 
private String ulica; 

public Integer getOddzial_id() { 
    return oddzial_id; 
} 

public void setOddzial_id(Integer oddzial_id) { 
    this.oddzial_id = oddzial_id; 
} 

public Integer getId() { 
    return oddzial_id; 
} 

public void setId(Integer id_oddzial) { 
    this.oddzial_id = id_oddzial; 
} 

public String getMiasto() { 
    return miasto; 
} 

public void setMiasto(String miasto) { 
    this.miasto = miasto; 
} 

public String getUlica() { 
    return ulica; 
} 

public void setUlica(String ulica) { 
    this.ulica = ulica; 
} 

@Override 
public String toString() { 
    return "[" + oddzial_id + "] " + ulica + " " + miasto; 
} 
} 

這裏是Oddzial的DAO:

@Repository 
public class OddzialDAOImpl implements OddzialDAO { 

@Autowired 
private SessionFactory sessionFactory; 

private Session getCurrentSession() { 
    return sessionFactory.getCurrentSession(); 
} 

public void addOddzial(Oddzial oddzial) { 
    getCurrentSession().save(oddzial); 
} 

public void updateOddzial(Oddzial oddzial) { 
    Oddzial oddzialToUpdate = getOddzial(oddzial.getId()); 
    oddzialToUpdate.setMiasto(oddzial.getMiasto()); 
    oddzialToUpdate.setUlica(oddzial.getUlica()); 
    getCurrentSession().update(oddzialToUpdate); 
} 

public Oddzial getOddzial(int id) { 
    Oddzial oddzial = (Oddzial) getCurrentSession().get(Oddzial.class, id); 
    return oddzial; 
} 

public void deleteOddzial(int id) { 
    Oddzial oddzial = getOddzial(id); 
    if (oddzial != null) 
     getCurrentSession().delete(oddzial); 
} 

@SuppressWarnings("unchecked") 
public List<Oddzial> getOddzialy() { 
    return getCurrentSession().createQuery("from Oddzial").list(); 
} 
} 

這裏是OddzialService:

@Service 
@Transactional 
public class OddzialServiceImpl implements OddzialService { 

@Autowired 
private OddzialDAO oddzialDAO; 

@Override 
public void addOddzial(Oddzial oddzial) { 
    oddzialDAO.addOddzial(oddzial);  
} 

@Override 
public void updateOddzial(Oddzial oddzial) { 
    oddzialDAO.updateOddzial(oddzial); 
} 

@Override 
public Oddzial getOddzial(int id) { 
    return oddzialDAO.getOddzial(id); 
} 

@Override 
public void deleteOddzial(int id) { 
    oddzialDAO.deleteOddzial(id); 
} 

@Override 
public List<Oddzial> getOddzialy() { 
    return oddzialDAO.getOddzialy(); 
} 

} 

這裏是控制器:

@Controller 
@RequestMapping(value="/oddzial") 
public class OddzialController { 

@Autowired 
private OddzialService oddzialService; 

@RequestMapping(value="/add", method=RequestMethod.GET) 
public ModelAndView addOddzialPage() { 
    ModelAndView modelAndView = new ModelAndView("add-oddzial-form"); 
    modelAndView.addObject("oddzial", new Oddzial()); 
    return modelAndView; 
} 

@RequestMapping(value="/add", method=RequestMethod.POST) 
public String addingOddzial(@ModelAttribute @Valid Oddzial oddzial, BindingResult result) { 
    if(result.hasErrors()) { 
     return "add-oddzial-form"; 
    } 
    oddzialService.addOddzial(oddzial); 
    String message = "Oddział został pomyślnie dodany do bazy."; 

    return "list-of-oddzials"; 
} 

@RequestMapping(value="/list") 
public ModelAndView listOfOddzials() { 
    ModelAndView modelAndView = new ModelAndView("list-of-oddzials"); 

    List<Oddzial> oddzials = oddzialService.getOddzialy(); 
    modelAndView.addObject("oddzials", oddzials); 
    return modelAndView; 
} 

@RequestMapping(value="/edit/{id}", method=RequestMethod.GET) 
public ModelAndView editOddzialPage(@PathVariable Integer id) { 
    ModelAndView modelAndView = new ModelAndView("edit-oddzial-form"); 
    Oddzial oddzial = oddzialService.getOddzial(id); 
    modelAndView.addObject("oddzial",oddzial); 
    return modelAndView; 
} 

@RequestMapping(value="/edit/{id}", method=RequestMethod.POST) 
public ModelAndView edditingOddzial(@ModelAttribute Oddzial oddzial, @PathVariable Integer id) { 

    ModelAndView modelAndView = new ModelAndView("home"); 

    oddzialService.updateOddzial(oddzial); 

    String message = "Dane zostały zmodyfikowane."; 
    modelAndView.addObject("message", message); 

    return modelAndView; 
} 

@RequestMapping(value="/delete/{id}", method=RequestMethod.GET) 
public ModelAndView deleteOddzial(@PathVariable Integer id) { 
    ModelAndView modelAndView = new ModelAndView("home"); 
    oddzialService.deleteOddzial(id); 
    String message = "Oddział został usunięty."; 
    modelAndView.addObject("message", message); 
    return modelAndView; 
} 

} 

我的Spring配置文件的內容:

<?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:tx="http://www.springframework.org/schema/tx" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:flow="http://www.springframework.org/schema/webflow-config" 
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/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
"> 

<!-- Root Context: defines shared resources visible to all other web components --> 

<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost/tutorial" /> 
    <property name="username" value="root" /> 
    <property name="password" value="" /> 
</bean> 

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="packagesToScan" value="pl.carrental" /> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> 
      <prop key="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="sessionFactory" /> 
</bean> 

<bean 
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 

<bean id="passwordEncoder" 
    class="org.springframework.security.authentication.encoding.ShaPasswordEncoder"> 
    <constructor-arg value="256" /> 
</bean> 

<bean id="saltSource" 
    class="org.springframework.security.authentication.dao.ReflectionSaltSource"> 
    <property name="userPropertyToUse" value="username" /> 
</bean> 

<!-- <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping"> 
    <property name="flowRegistry" ref="flowRegistry"></property> </bean> <bean 
    class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter"> <property 
    name="flowExecutor" ref="flowExecutor"></property> </bean> <flow:flow-executor 
    id="flowExecutor" flow-registry="flowRegistry" /> <flow:flow-registry id="flowRegistry" 
    base-path="/WEB-INF/flows"> <flow:flow-location-pattern value="/**/*-flow.xml" 
    /> </flow:flow-registry> --> 

<bean id="multipartResolver" 
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> 

<bean 
    class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> 

<tx:annotation-driven transaction-manager="transactionManager" /> 

<context:component-scan 
    base-package="pl.carrental" /> 

web.xml文件的內容:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
version="3.0"> 

<!-- The definition of the Root Spring Container shared by all Servlets 
    and Filters --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
      /WEB-INF/spring/root-context.xml 
      /WEB-INF/spring/spring-security.xml 
    </param-value> 
</context-param> 

<!-- Creates the Spring Container shared by all Servlets and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<!-- Processes application requests --> 
<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<filter> 
    <filter-name>localizationFilter</filter-name> 
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>localizationFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

<filter> 
    <filter-name>OpenSessionInViewFilter</filter-name> 
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>OpenSessionInViewFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

預先感謝幫助

+0

因此,您可以添加記錄,但不能修改或刪除它們?我注意到在Hibernate屬性中有'show_sql'設置爲'true'。你可以看到當你試圖刪除/修改記錄時SQL Hibernate產生了什麼? –

+0

在數據庫中存在一個具有該id的對象,如果您在getCurrentSession()。delete()上設置斷點,它會被命中嗎? –

回答

0

解決。該行:

<tx:annotation-driven transaction-manager="transactionManager" /> 

必須在servlet-context.xml中。不在像我的情況下在root-config.xml中。現在交易已經完成,我可以刪除和更新記錄。

相關問題