Reputation: 279
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.
Upvotes: 1
Views: 3605
Reputation: 175
Based on Maciej Kowalski answer
To find by UUID on repository:
Optional<User> findByUUID(UUID uuid);
On service to delete:
Optional<User> optionalUser = repository.findByUUID(uuid);
if (optionalUser.isPresent()) {
User user = optionalUser.get();
repository.delete(user);
}
// here on else you can throw an Exception
On service to update:
Optional<User> optionalUser = repository.findByUUID(uuid);
if (optionalUser.isPresent()) {
User user = optionalUser.get();
// Make changes here for example user.setName(otherName)
repository.save(user);
}
// here on else you can throw an Exception
Upvotes: 0
Reputation: 26522
You have all the right set up regarding the cascading:
The problem is that you are triggering a native query. This bypasses all the JPA configuration and cascading altogether. Even if you used a JPQL delete without native, this would still omit all the cascading and JPA config.
Somewhere in your code, you need to fetch the User, using findOne
for example. After that use the delete
method. Only then the cascading will work.
Upvotes: 4