R.A.Munna
R.A.Munna

Reputation: 1709

One-To-Many relation in JPA DELETE operation is not working

The entity model and repository is given below.

Channel.java

public class Channel extends BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name = "channel_name")
    private String channelName;

    @Column(name = "channel_type")
    private Integer channelType;

    @Column(name = "seq_id")
    private Integer seqId;

    @Column(name = "channel_device_key")
    private String channeldeviceKey;
}

UserRoomChannel.java

public class UserRoomChannel extends BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @OneToOne
    @JoinColumn(name = "user_house_id")
    private UserHouse userHouse;

    @OneToOne
    @JoinColumn(name = "room_id")
    private Room room;

    @LazyCollection(LazyCollectionOption.FALSE)
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Channel> channels;
}

UserRoomChannelReposirtory.java

public interface UserRoomChannelRepository extends JpaRepository<UserRoomChannel, Long> {
    @Query(value = "DELETE FROM user_room_channel_channels WHERE channels_id=?1", nativeQuery = true)
    void deleteUserRoomChannelChannels(Long id);
}

I can save the data successfully. When data is saved through this a third table named user_room_channel_channels is created.

EX:

    user_room_channel_id   channels_id
           1                   1                   
           1                   2

But When I tried to delete with channels_id it give me the error

A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance:.....

The native query what I write it execute from the command line.

But using JPA I can't do that.

Any help or any suggestion for resolving this issue?

Upvotes: 1

Views: 2597

Answers (2)

R.A.Munna
R.A.Munna

Reputation: 1709

I just have done the below step and it works perfectly.

  • Reomve:
    • myUserRoomChannel.setChannels(channels)
  • Add
    • myUserRoomChannel.getChannels().removeAll(channels) and then
    • userRoomChannelRepository.save(myUserRoomChannel)

Upvotes: 0

arthur
arthur

Reputation: 3325

A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance

is because before you delete your channels(and its children), you somehow do the following:

  • you load your UserRoomChannel along with its Channels children in the collection from the Database.
  • somewhere in your code you change the reference of the children collection : myUserRoomChannel.setChannels(newChannelCollections) or myUserRoomChannel.channels =new ChannelCollections();

  • and you try to delete the user with your repositorisory.

Hibernate who remembered having set the children collection with reference A to the User can find the collection anymore, because User.channels is now User.channels == B (with B being a new reference to your collection).

How to fix it: just find the place where you are replacing your children collections and instead of:

  • myUserRoomChannel.setChannels(newChannelCollections), or

  • myUserRoomChannel.channels =new ChannelCollections(),

just do

myUserRoomChannel.getChannels().add/delete/clearYourChannels()

Upvotes: 2

Related Questions