79208792

Date: 2024-11-20 20:08:16
Score: 0.5
Natty:
Report link

Finally, the problem was that I didn't add the task inside the transaction

public TicketEntity addTask(int ticketId, TaskEntity taskEntity) {
    Transaction transaction = null;
    TicketEntity ticketEntity = null;

    try (Session session = HibernateUtil.getSessionFactory().openSession()) {
        transaction = session.beginTransaction();
        ticketEntity = session.get(TicketEntity.class, ticketId);
        if (ticketEntity == null) {
            throw new IllegalArgumentException("Ticket not found with ID: " + ticketId);
        }
        taskEntity.setTicket(ticketEntity);
        ticketEntity.addTask(taskEntity);

        session.merge(ticketEntity);
        transaction.commit();
    } catch (Exception e) {
        if (transaction != null) {
            transaction.rollback();
        }
        e.printStackTrace();
    }

    return ticketEntity;
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: GeoD