-1

I have a Spring Boot project using JpaRepository.

I have two entities, Threat and Dimension, in the database I have 3 tables: tbl_threat, tbl_dimension and tbl_threat_dimension (that relates which dimension each threat has)

I want to delete a relationship row in thread_dimension table. How can I solve this?

@Entity(name = "tbl_threat")

public class Threat {

@Id
private String id;

@Column(nullable = false)
private String name;

@ManyToOne
private User user;

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "threat_dimension", joinColumns = {
        @JoinColumn(name = "threat_id")}, inverseJoinColumns = {
        @JoinColumn(name = "dimension_id")})
private List<Dimension> dimensions;


@Entity(name = "tbl_dimension")

public class Dimension {

@Id
private String id;

@Column(nullable = false)
private String name;

}

tbl_threat_dimension

Elizama Melo
  • 103
  • 2
  • 10

2 Answers2

0

For specifying the table name you should use @Table(name = "table_name"). Keep the @Entity annotation, but setting name for @Entity is not used to specify the table name.

Check out this answer to see the difference between setting the name for @Entity & @Table: https://stackoverflow.com/a/18732732/2464528

cbender
  • 2,231
  • 1
  • 14
  • 18
0

My guess is that you are missing a mapping Set<Thread> in your dimension class, since this is a many-to-many relationship. With a proper @mappedBy annotation a deletion should also delete the corresponding relationship entry.

I would also suggest you should use Set<Dimensions> instead of a List<Dimensions>. Sooner or later you would have a lot of performance issues. See: Hibernate Many to Many Relations Set Or List?

Also keep in mind that CascadeType.ALL also includes CascadeType.REMOVE which causes to delete e.g. all Thread entries which has a relationship for a specific Dimension. See: How to delete a row in join table with JPA