The method should delete all ads in a person by his id. it doesn't give errors, I don't understand how to make a debug to check what is wrong.
public void deleteAllAdByPersonById(int id) {
EntityManager em = FACTORY.createEntityManager();
EntityTransaction transaction = em.getTransaction();
transaction.begin();
Query query = em.createQuery("DELETE FROM Ad a WHERE a.person.id = :id");
query.setParameter("id", id);
transaction.commit();
em.close();
}
Person
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "person_id")
private int id;
@NotEmpty
@NotNull(message = "Name cannot be null")
private String name;
@OneToMany(mappedBy = "person", fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
private Set<Ad> ads = new HashSet<>();
Ad
@Entity
public class Ad {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ad_id")
private int id;
...
@ManyToOne
@JoinColumn(name = "person_fk_id")
private Person person;
you just had to write
query.executeUpdate();
after
query.setParameter("id", id);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.