Vishal Suri
Vishal Suri

Reputation: 455

Constraint violation exception while deleting record

I am working on a Spring MVC app. I have two model classes, contact and location.

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

and location model:

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

There can be multiple contacts for a location. When I try to delete location, using:

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

Following exception raises:

    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)

I do not want to delete contact if location gets deleted. Rather, in related contact, location id should become 0.

I have following import statements in location model:

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;

As you can see, I have imported some statements from JPA and some from Hibernate. Is this permitted?

Upvotes: 0

Views: 3409

Answers (1)

Stijn Geukens
Stijn Geukens

Reputation: 15628

You need to dereference ContactModel from LocationModel before deleting location. This has nothing to do with hibernate; it would (and it does) fail with plain SQL as well.

So, omit cascade REMOVE, set contactModel of LocationModal to nulland then delete LocationModel.

Upvotes: 1

Related Questions