I have a pair of entities, call them "Ticket" and "TicketComment".
Ticket has this property:
@OneToMany(mappedBy="ticket", cascade = CascadeType.ALL)
@OrderBy("date")
private List<TicketComment> comments = new ArrayList<>();
And, TicketComment has this:
@ManyToOne
@JoinColumn(name="TKT_ID")
@NotNull
private Ticket ticket;
The code I'm using should, I think, add a single new comment. However, it seems to add the new comment to the database twice:
Ticket ticket = ticketRepo.findOne(id);
TicketComment newComment = new TicketComment();
// ...
newComment.setTicket(ticket);
ticket.getComments().add(newComment);
ticketRepo.save(ticket);
I think I've been able to do this successfully with similar entities before without the duplicate child entities... what might I be missing?
Update: a workaround seems to be:
- Create another JpaRepository for "TicketComment"
- Save any other updates to Ticket without creating/adding the comment using
ticketRepo
. - Create the new comment, linked up with the ticket (
newComment.setTicket(ticket);
), and save it with aticketCommentRepo
.
So, there's two distinct saves, one which should only affect the parent, and one which should only affect the child.
I'd like to bring it down to a single save, but I'm not sure that's possible?
Update 2: clarification of the observed symptoms.
What happens if I try to simply save my parent "Ticket" entity with the new "TicketComment" added is that I see two distinct records created in the database, with different primary keys, but which have identical comment text, and identical, or nearly so, timestamps.
So, for example, say I posted a comment like "Looks good". Even though I confirmed in a debugger that there's only a single "Looks good" showing in my ticket.getComments()
... after I load the ticket's details page again, I see something like:
me @ 2015-09-11 23:16:58: Looks good
me @ 2015-09-11 23:16:59: Looks good
When I looked at some debug logs of the SQL statements being prepared, I saw two identical looking INSERT statements being called for my TicketComments table, followed by a single UPDATE for my Tickets table...