0

I have a bidirectional ManyToMany mapping, and I am trying to delete a post entity. When I delete it, the reference in the posts_categories table should also be removed. However, despite trying the following two approaches, it’s not deleting the reference, and I am receiving the error below. I am using postgresql and I did not find solution for my problem.

ERROR: The UPDATE or DELETE operation on the "posts" table violates the "fkq4402eed597exuj50g73c7kmg" foreign key constraint on the "posts_categories" table.

First Approach

@Entity
@Data
@Table(name = "posts")
public class PostEntity extends BaseEntity {

@ManyToMany
@JoinTable(
        name = "posts_categories",
        joinColumns = {@JoinColumn(name = "post_id")},
        inverseJoinColumns = {@JoinColumn(name = "category_id")})
private Set<CategoryEntity> categories = new HashSet<>();
...
} 


@Entity
@Data
@Table(name = "categories")
public class CategoryEntity extends BaseEntity {

@ManyToMany(mappedBy = "categories")
private Set<PostEntity> posts = new HashSet<>();    
}

Second Approach

@Entity
@Data
@Table(name = "posts")
public class PostEntity extends BaseEntity {

@ManyToMany
@JoinTable(
        name = "posts_categories",
        joinColumns = {@JoinColumn(name = "post_id")},
        inverseJoinColumns = {@JoinColumn(name = "category_id")})
private Set<CategoryEntity> categories = new HashSet<>();  
... 
}


@Entity
@Data
@Table(name = "categories")
public class CategoryEntity extends BaseEntity {

@ManyToMany
@JoinTable(
        name = "posts_categories",
        joinColumns = {@JoinColumn(name = "category_id")},
        inverseJoinColumns = {@JoinColumn(name = "post_id")})
private Set<PostEntity> posts = new HashSet<>();
}

My Service:

@Transactional
public Boolean deleteByUUID(UUID uuid) {
    Entity entity = postRepository.findByUuid(uuid).orElse(null);
    if (entity != null) {
        postRepository.delete(entity);
        return Boolean.TRUE;
    } else {
        return Boolean.FALSE;
    }
}
2
  • what language is the code above? Commented Nov 11, 2024 at 23:05
  • java + java spring boot Commented Nov 12, 2024 at 2:16

2 Answers 2

0

U can try to make foreign relation with both tables and when u delete relation from table where is exists many to many relation, u can easily see that in both tables matched rows are deleted

Sign up to request clarification or add additional context in comments.

Comments

0

It turns out the issue was due to the @Data annotation, which was causing an infinite loop in the background, likely due to the toString, hashCode, and equals methods —or possibly just the toString method alone; I'm not entirely sure.

To resolve the issue, I removed the @Data annotation and implemented the getter and setter methods manually, which fixed the problem.

in addition, from my research, it appears that using a bulk remove method is more efficient in many-to-many relationships from a performance standpoint. Normally, Hibernate sends multiple queries for the deletion process, which can lead to performance issues. Therefore, it’s recommended to manually write a single delete query to improve performance in these cases.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.