I have two entities which are connected with ManyToMany relation:
User.java
@Id
@Column(name = "user_id", updatable = false, nullable = false, unique = true)
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;
@Column(name = "name")
private String name;
@Column(name = "product")
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinTable(name = "products_users",
joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "product_id")})
private Set<Product> products;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd@HH:mm:ss")
@Column(name = "created_on")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Date createdOn;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd@HH:mm:ss")
@Column(name = "modified_on")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Date modifiedOn;
// construuctors, getter, setter
}
Product .java
@Id
@Column(name = "product_id", updatable = false, nullable = false, unique = true)
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;
@Column(name = "name")
private String name;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd@HH:mm:ss")
@Column(name = "created_on")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Date createdOn;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd@HH:mm:ss")
@Column(name = "modified_on")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Date modifiedOn;
//getters, setters, contructors
}
I want to delete data from DB by users UUID. How i write query like this
@Transactional
@Modifying
@Query(value = "delete from users where user_id = ?1", nativeQuery = true)
void deleteByUUID(UUID uuid);
but it deletes the only row in the users table. I want all the data about this user and his products to be deleted.
And also I don't know how to perform an update of user and it's products correctly.