If Hibernate does not insert element in child table , The reason is because the mapping does not cascade the persist operation.

Example :

@OneToMany(fetch = FetchType.LAZY, mappedBy = "ticket")
Set<Message> message = new HashSet<Message>();

The solution is :

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "ticket")
Set<Message> message = new HashSet<Message>();

Cascade operations are extremely useful, but be mindful of their potential side effects, particularly if you modify Ticket which you don’t intend for an operation to be cascaded to Message.

Cascade operations are extremely useful, but be mindful of their potential side effects, particularly if you modify Ticket which you don’t intend for an operation to be cascaded to Message.

If you want to manage the persistence state of Message and Ticket separately, you’d need to modify your business logic code to persist the Messages and then persist the Ticket.

Commentaires

  1. Pingback: JPA Associations » JavaTuto

Laisser un commentaire