1

I am writing a school project where people can buy cinema tickets. I want to be able to send the users free cinema tickets for certain occasions that are only valid for a certain day. I run a CRON job at midnight that will set the tickets to inactive if they have expired so that they cannot be used anymore. I am experiencing weird behavior with the deactivation query and I can't see why.

I have this JPA Repository:

public interface UserTicketRepo extends JpaRepository<UserTicket, Long> { 
@Query(value = "update UserTicket ut\n" +
        "set isvalid = 0\n" +
        "where id in (\n" +
        "  select ticketId\n" +
        "  from (\n" +
        "         select ut.id as ticketId\n" +
        "         from UserTicket ut\n" +
        "                inner join TicketPrice tp on ut.ticketPrice_id = tp.id\n" +
        "                inner join PurchaseItem pi on ut.purchaseItemId = pi.id\n" +
        "                inner join Purchase p on pi.purchaseId = p.id\n" +
        "         where ut.isvalid = 1\n" +
        "           and tp.id = '" + TicketPrice.FREE_TICKET_ID + "'\n" +
        "           and TIMESTAMPDIFF(SECOND, NOW(), DATE_ADD(p.added, INTERVAL ut.activationPeriod SECOND)) < 0) as t\n" +
        ");", nativeQuery = true)
@Modifying(clearAutomatically = true, flushAutomatically = true)
@Transactional
void deactivateAllUserTicketsThatAreOverTheValidityEnd();
}

When I run this exact query manually in a DB tool, all my data is updated correctly, but when I run the query using the method in the code above, it does not update the UserTickets that expired yesterday for some unknown reason.

Edit: After some more research, I found this post: JPQL Query and Native Query returning different results in spring-data-jpa It looks like there is some weirdness going on in native queries with dates. Maybe that is the reason why I am having troubles. I will keep you posted if I find out more.

L.Butz
  • 2,466
  • 25
  • 44

0 Answers0