Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate deletes a record, but the record doesn't go away?

EDIT: Oops, I guess it would help if I posted the delete code. Sorry.

    // delete from permanent store and local list
    int index = get_record_to_delete();
    if (entityList.getEntities().get(index).getEntityId() != null ) {
        // existing persisted record
        entityService.delete(entityInfo.getEntities().get(index).getEntityId());
    }
    //remove from local list
    entityList.getEntities().remove(index);
    return ownersInfo;



    // entityService called by above code
    public void delete(MyEntity entity)  {
        repository.delete(entity);  // subclass of org.springframework.data.jpa.repository.JpaRepository
    }

I'm using Spring Data/JPA V 1.4.2.RELEASE and hibernate 3.6.10.Final.

I have a situation where I'm deleting an entity, and delete appears to work, but when I refresh my web page, the entity reappears. So, it doesn't produce an error, but the record doesn't get deleted.

This entity has child entities contained within it. I suspect that some kind of entity dependence problem is keeping the entity from being truly "deleted", but I don't know which entity dependency it might be, and Hibernate's not telling me. It doesn't matter if the contained entities get deleted or not, but this entity needs to get deleted. Not sure why that's not happening. Here's the entity I'm trying to delete:

public class MyEntity implements java.io.Serializable {
    @Id
    @GeneratedValue
    @Column(name = "MYID", unique = true, nullable = false)
    private Long myId;

    @Column(name = "MY_NAME", nullable = false, length = 50)
    private String myName;

    @ManyToOne
    @JoinColumn(name = "ADDRESS_ID", nullable = false)
    private AddressEntity myAddress;

    @ManyToOne
    @JoinColumn(name = "OTHER_ADDRESS_ID", nullable = true)
    private AddressEntity myOtherAddress;

    @ManyToOne
    @JoinColumn(name = "TYPE_CODE", nullable = false)
    private MyType myType;

    @ManyToOne
    @JoinColumn(name = "ADDRESS_CODE", nullable = false)
    private AddressCode addressCode;

    @Column(name = "OTHER", nullable = false, precision = 3, scale = 0)
    private Integer myOther;

    @ManyToOne
    @JoinColumn(name = "PARENT_ID", nullable = false)
    private MyParent parent;

    @Column(name = "PHONE", nullable = false, precision = 10, scale = 0)
    private Long myPhone;

    @Column(name = "SOCIAL", nullable = false, length = 9)
    private String mySocial;
}

Anyone see anything that might suggest why this entity won't delete?

like image 615
user3120173 Avatar asked Mar 07 '14 21:03

user3120173


1 Answers

This turned out to be a case of a Hibernate @OneToMany annotation canceling the delete.

Because the parent object had a @OneToMany(cascade=CascadeType.ALL) annotation on it, like this:

Class MyParent {

    @OneToMany(cascade=CascadeType.ALL)
    Set<MyObject> myObjects;

}

Hibernate apparently walks the entire object graph and for some reason, because this parent wasn't being deleted, it canceled the child delete.

The thing to do to detect this is turn your logging all the way up to TRACE and look for the following message from Hibernate:

un-scheduling entity deletion

Those are the magic words that let you know that Hibernate is canceling your delete. This was not particularly well documented, so I hope this helps someone else.

like image 187
user3120173 Avatar answered Sep 21 '22 10:09

user3120173