java jpa ManyToOne级联删除不按预期工作

时间:2013-12-11 13:23:06

标签: java spring hibernate jpa jpa-2.0

我有两个表,其中一个表我想在删除操作上执行级联。我已经尝试在变量和getter方法上使用注释,但变量1给了我这个错误: 发生错误

org.hibernate.exception.DataException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:102)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at no.nhst.user.dao.hibernate.IPUserDAOImpl.saveOrUpdate(IPUserDAOImpl.java:78)
    at no.nhst.user.service.IPUserServiceImpl.saveOrUpdateIPUser(IPUserServiceImpl.java:103)
    at no.nhst.user.web.admin.IPCustomerController.submitIpUsers(IPCustomerController.java:97)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:91)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.sql.BatchUpdateException: Data truncation: Out of range value adjusted for column 'user_account' at row 1
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2028)
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1451)
    at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
    at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    ... 79 more
Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Out of range value adjusted for column 'user_account' at row 1
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3607)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1980)
    ... 84 more

这是不可能的,因为它所指的列具有BIGINT(10)大小。

而且,当我尝试在getter方法上使用多对一注释时,它根本没有做任何事情。

这是我的父实体类:

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import no.nhst.user.model.resolver.AuthoritiesResolver;
import no.nhst.user.model.resolver.DefaultAuthoritiesResolver;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table(name = "ac_useraccount", uniqueConstraints = { @UniqueConstraint(columnNames = { "domain", "username" }) })
public class UserAccount extends BaseEntity implements UserDetails {

    private static final long serialVersionUID = 1L;

    private static final Log logger = LogFactory.getLog(UserAccount.class);

    private String domain;
    private String username;
    private String password;
    private String email;
    private UserProfile userProfile;

    private Date created;
    private Date lastlogin;
    private Date expires;
    private Date credentialsExpires;

    private boolean enabled = false;
    private boolean locked = false;

    private Set<Role> roles;
    private Set<Group> groups;
    private Set<UserApplication> applications;
    private Set<Tag> tags = new HashSet<Tag>();  // optional "tags" to categorize a user in some way (not required)

    private int maxSessions;

    private AuthoritiesResolver authoritiesResolver;

    UserAccount() {
        this.applications = new HashSet<UserApplication>();
        this.authoritiesResolver = new DefaultAuthoritiesResolver();
    }

    UserAccount(String domain, String username) {
        this(domain, username, null, null, false);
    }

    UserAccount(String domain, String username, String password, String email, boolean enabled) {
        this();
        this.domain = domain;
        this.username = username;
        this.password = password;
        this.email = email;
        this.enabled = enabled;
    }





    public void setDomain(String domain) {
        this.domain = domain;
    }



    public void setUserProfile(UserProfile userProfile) {
        this.userProfile = userProfile;
        if (null != userProfile)
            userProfile.setUserAccount(this);
    }

    /** See http://docs.jboss.org/hibernate/core/3.5/reference/en-US/html/objectstate.html#objectstate-transitive */
    // TODO: See AVIS-589 (update: that Jira issue is resolved)
    @OneToOne(orphanRemoval=false,fetch = FetchType.LAZY)
    @Cascade({CascadeType.ALL})
    public UserProfile getUserProfile() {
        return userProfile;
    }





    // TODO: AVIS-607, set to FetchType.EAGER temporarily
    @OneToMany(mappedBy="userAccount", fetch=FetchType.LAZY)
    @Cascade({CascadeType.ALL})
    public Set<UserApplication> getApplications() {
        return this.applications;
    }

    public void setApplications(Set<UserApplication> applications) {
        this.applications = applications;
    }

    @OneToMany(mappedBy = "userAccount")
    @Cascade({CascadeType.ALL})
    public Set<Tag> getTags() {
        return tags;
    }

    public void setTags(Set<Tag> tags) {
        this.tags = tags;
    }




    @Basic
    public int getMaxSessions() {
        return maxSessions;
    }

    public void setMaxSessions(int maxSessions) {
        this.maxSessions = maxSessions;
    }

    @Temporal(TemporalType.TIMESTAMP)
    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    @Temporal(TemporalType.TIMESTAMP)
    public Date getLastlogin() {
        return lastlogin;
    }

    public void setLastlogin(Date lastlogin) {
        this.lastlogin = lastlogin;
    }

    // Transient (dependent on other fields) Spring Security UserDetails methods

    @Transient
    public Set<GrantedAuthority> getAuthorities() {
        return authoritiesResolver.resolveAuthorities(this);
    }

    @Transient
    public boolean isAccountNonExpired() {
        boolean accountNonExpired = true;
        if(this.expires != null) {
            Date now = new Date();
            if(now.after(expires)) {
                accountNonExpired = false;
            }
        }
        return accountNonExpired;
    }

    @Transient
    public boolean isCredentialsNonExpired() {
        boolean credentialsNonExpired = true;
        if(this.credentialsExpires != null) {
            Date now = new Date();
            if(now.after(credentialsExpires)) {
                credentialsNonExpired = false;
            }
        }        
        return credentialsNonExpired;
    }

    @Transient
    public boolean isAccountNonLocked() {
        return !this.locked;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        this.locked = !accountNonLocked;
    }

    public void setAuthoritiesResolver(AuthoritiesResolver authoritiesResolver) {
        this.authoritiesResolver = authoritiesResolver;
    }

    @Transient
    public AuthoritiesResolver getAuthoritiesResolver() {
        return authoritiesResolver;
    }

    @Transient
    public void addRole(Role role) {
        this.getRoles().add(role);
        role.getUserAccounts().add(this);
    }

    @Transient
    public void removeRole(Role role) {
        this.getRoles().remove(role);
        role.getUserAccounts().remove(this);
    }

    @Transient
    public void addGroup(Group group) {
        this.getGroups().add(group);
        group.getUserAccounts().add(this);
    }






}

这是我的子课,我使用了多对一注释:

import org.hibernate.annotations.*;

import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="ac_ua_ip")
public class IPUser extends BaseEntity{

    private static final long serialVersionUID = 1L;

    private String ip;
    private String comment;

    private UserAccount userAccount;



    public IPUser(){

    }

    public IPUser (String ip, String comment, UserAccount userAccount){
        this.ip = ip;
        this.comment = comment;
        this.setUserAccount(userAccount);
    }

    @Column (nullable = false)
    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    @Column
    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    @ManyToOne(cascade = CascadeType.REMOVE)
    public UserAccount getUserAccount() {
        return userAccount;
    }

    public void setUserAccount(UserAccount userAccount) {
        this.userAccount = userAccount;
    }






}

而且,我已经从父类中删除了很多方法,只是为了清晰起见。 关于我做错了什么的建议?

2 个答案:

答案 0 :(得分:1)

您必须手动管理该问题,因为从ManyToOne关系级联删除操作实际上没有意义(否则您的数据库将不一致,因为可能仍有一些IPUser实体没有UserAccount )。

请参阅this answer以获取JPA规范的摘录+类似问题。

答案 1 :(得分:0)

好的,这就是我如何使它发挥作用。

我希望回答自问问题的礼仪一切顺利。 我在UserAccount类中添加了对IPUser类的引用,现在它运行得很顺利。这是我添加的片段:

        private List<IPUser> ipUserList;    
        @OneToMany(mappedBy = "userAccount", fetch = FetchType.LAZY)
        @Cascade({CascadeType.DELETE})
        public List<IPUser> getIpUserList() {
            return ipUserList;
        }

        public void setIpUserList(List<IPUser> ipUserList) {
            this.ipUserList = ipUserList;
        }

请随时更新我的​​回答或就此提出任何问题。

相关问题