Spring 3 + Hibernate:java.sql.BatchUpdateException:无法添加或更新子行(ManyToMany)

时间:2011-06-22 10:47:36

标签: java mysql hibernate spring

我在编辑后存储帐户项目时遇到问题。 “帐户”项包含一组角色(@ManyToMany)。如果我加载帐户项并尝试编辑它一切正常,直到我尝试添加新角色。角色都带有复选框。最初检查的那些也在编辑表单中检查。如果我现在尝试检查其他一些并尝试保存更改的帐户,我从Hibernate得到一个例外,说:

  

java.sql.BatchUpdateException:无法添加或更新子行:外键约束失败(fi_mc-toolaccount_role,CONSTRAINT FK4AC9CB881F90989D FOREIGN KEY(Account_id)参考Accountid))

在角色实体中我没有定义与Account的任何关系。它刚刚在账户实体上定义。这可能是原因吗?

帐户实体摘录:

@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
@JoinTable(name = "Account_Role", joinColumns = { 
    @JoinColumn(name = "Account_id") }, 
    inverseJoinColumns = { @JoinColumn(name = "Role_id") })
private Set<Role> roles = new HashSet<Role>(0);

帐户服务摘录:

@Transactional
public void merge(Account account) {
    accountDAO.merge(account);
}

帐户提取DAO:

public void merge(Account account) {
    sessionFactory.getCurrentSession().merge(account);
}

帐户

的edit.jsp摘录
    <tr>
        <td style="width:75px">
            <label for="roles"><spring:message code="labels.account.form.roles" text="Roles" /></label>
        </td>
        <td>
            <form:checkboxes path="roles" items="${roleList}" itemLabel="name" itemValue="idAsString" delimiter="<br/>"/> 
        </td>

</tr>

帐户控制器的摘录

/**
 * edit() - Save edited item
 * @param id of changed object
 * @param item which has been changed
 * @return  path to view
 */
@RequestMapping(value="/edit/{id}", method=RequestMethod.POST)
public String edit(@PathVariable("id") Long id, @ModelAttribute("item") Account item, BindingResult bindingResult, Model model) {
    accountService.merge(item);

    return "redirect:/account/list";
}

/**
 * edit() - Edit an item
 * @param id of item to change
 * @param model to store item
 * @return  path to view
 */
@RequestMapping(value="/edit/{id}", method=RequestMethod.GET)
public String editForm(@PathVariable("id") Long id, Model model) {
    Account item = accountService.load(id);

    for(Role r : item.getRoles()){
        System.out.println("ROLE ITEM: ID="+r.getId()+" | NAME="+r.getName()+" | HASH="+r);
    }

    for(Role r : roleService.list()){
        System.out.println("ROLE SERVICE: ID="+r.getId()+" | NAME="+r.getName()+" | HASH="+r);
    }

    model.addAttribute("item", item);
    model.addAttribute("roleList", roleService.list());

    return "account/edit";
}

从spring-servlet.xml中提取:

<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />

<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans -->
<context:component-scan base-package="fi.java.elearning.*" />

<!-- Configures the annotation-driven Spring MVC Controller programming model -->
<mvc:annotation-driven /> 

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />

<!-- Include Tiles for View Rendering -->
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass">
        <value>
            org.springframework.web.servlet.view.tiles2.TilesView
        </value>
    </property>
</bean>

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/configurations/tiles/tiles.xml</value>
        </list>
    </property>
</bean>

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


<!-- multi language support -->

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="de" />
</bean>

<bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="language" />
</bean>

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
    <property name="interceptors">
       <list>
        <ref bean="localeChangeInterceptor" />
       </list>
    </property>
</bean>

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="languages/messages" />
</bean>

<!-- Import Hibernate Context -->
<import resource="configurations/hibernate/hibernate-context.xml" />

<!-- Import Spring Security -->
<import resource="configurations/spring/spring-security.xml" />

从web.xml中提取:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
  <error-code>404</error-code>
  <location>/WEB-INF/views/contents/exceptions/404.jsp</location>
</error-page>

从hibernate.context.xml中提取:

<context:property-placeholder location="/WEB-INF/configurations/hibernate/database.properties" />

<!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
<!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html -->                           
<!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html -->
<!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
             p:dataSource-ref="dataSource"
             p:configLocation="${hibernate.config}"
             p:packagesToScan="fi.java.elearning"/>

<!-- Declare a datasource that has pooling capabilities-->   
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close"
            p:driverClass="${app.jdbc.driverClassName}"
            p:jdbcUrl="${app.jdbc.url}"
            p:user="${app.jdbc.username}"
            p:password="${app.jdbc.password}"
            p:acquireIncrement="5"
            p:idleConnectionTestPeriod="60"
            p:maxPoolSize="100"
            p:maxStatements="50"
            p:minPoolSize="10" />

<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
            p:sessionFactory-ref="sessionFactory" />

<!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />   

0 个答案:

没有答案