2013-12-11 3 views
0

У меня есть две таблицы, и на одном из них я хочу выполнить каскад при операции удаления. Я попытался с помощью аннотаций как на переменной и на метод получения, а также, но переменная одна дает мне эту ошибку: Произошла ошибкаjava jpa ManyToOne каскад удалить не работает как ожидалось

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).

И, когда я пытаюсь использовать много к одному аннотации на методе геттера, он ничего не делает.

Вот мой родительский объект класс:

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; 
    } 






} 

И я удалил много методов из родителей класс здесь, чтобы иметь ясность. Любые предложения относительно того, что я делаю неправильно?

ответ

1

Вам придется управлять этой проблемой вручную, так как на самом деле не имеет смысла каскадировать операции удаления из отношения ManyToOne (в противном случае ваша БД не будет согласованной, так как там могут остаться некоторые IPUser объектов без UserAccount).

См. this answer для отрывок из спецификации JPA + аналогичная проблема.

0

Хорошо, вот как я заработал.

Надеюсь, все будет хорошо с этикеткой, отвечающей на вопросы, задаваемые самим вопросом. Я добавил ссылку на класс IPUser в классе UserAccount, и теперь он работает плавно. Вот фрагмент, который я добавил:

 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; 
     } 

Пожалуйста, не стесняйтесь обновлять мой ответ или задавать любые вопросы относительно этого.

Смежные вопросы