删除记录时的约束违例异常

时间:2015-01-19 11:28:00

标签: spring hibernate jpa

我正在开发一个Spring MVC应用程序。我有两个模型类,联系人和位置。

@Entity
@Table(name="Contact")
public class ContactModel {

    @Id
    @Column(name="contactid")
    @GeneratedValue
    private int contactId;

    @Column(name="contactname")
    private String contactName;

    @Column(name="contactemail")
    private String email;

    @Column(name="contactphone")
    private String phone;

    @ManyToOne
    @JoinColumn(name="locationid")
    private LocationModel locationModel;
}

和位置模型:

@Entity
@Table(name="Location")
public class LocationModel {

    @Id
    @Column(name="locationid")
    @GeneratedValue 
    private int locationId;

    @Column(name="locationname")
    private String locationName;

    @Column(name="locationdesc")
    private String locationDescription;

    @OneToMany
    @Cascade({CascadeType.REMOVE})
    @JoinColumn(name="locationid")
    private List<ContactModel> contactList;
}

某个地点可能有多个联系人。当我尝试删除位置时,使用:

Session session = sessionFactory.getCurrentSession();
            LocationModel locationModel = (LocationModel)session.get(LocationModel.class, locationId);
            session.delete(locationModel);

以下异常加注:

    16:46:03,076 ERROR [stderr] (http--127.0.0.1-9090-1) org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

16:46:03,081 ERROR [stderr] (http--127.0.0.1-9090-1)    at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:161)

16:46:03,083 ERROR [stderr] (http--127.0.0.1-9090-1)    at org.springframework.orm.hibernate4.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:683)

16:46:03,085 ERROR [stderr] (http--127.0.0.1-9090-1)    at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:565)

16:46:03,087 ERROR [stderr] (http--127.0.0.1-9090-1)    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)

16:46:03,090 ERROR [stderr] (http--127.0.0.1-9090-1)    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)

16:46:03,093 ERROR [stderr] (http--127.0.0.1-9090-1)    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:478)

16:46:03,097 ERROR [stderr] (http--127.0.0.1-9090-1)    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272)

16:46:03,098 ERROR [stderr] (http--127.0.0.1-9090-1)    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)

16:46:03,100 ERROR [stderr] (http--127.0.0.1-9090-1)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)

16:46:03,102 ERROR [stderr] (http--127.0.0.1-9090-1)    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:646)

如果位置被删除,我不想删除联系人。相反,在相关联系中,位置ID应为0。

我在位置模型中有以下import语句:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Cascade;

如您所见,我从JPA导入了一些语句,而从Hibernate导入了一些语句。这是允许的吗?

1 个答案:

答案 0 :(得分:1)

在删除位置之前,您需要从LocationModel取消引用ContactModel。这与hibernate无关;它会(并且它确实)也会使用普通的SQL失败。

因此,省略级联REMOVE,将LocationModal的contactModel设置为nulland,然后删除LocationModel。