I have a list of sessions that are available to purchase. After a customer purchases a session how do I change the "available" field to 0 or false so that they no longer appear in the list? Thank you for any feed back!
Here is my Session entity class:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name="date")
private String date;
@Column(name="time")
private String time;
@Column(name="available")
private boolean available;
@Column(name="side")
private String side;
Here is my Query that i'm trying to use:
// Only update the available field while leaving everything else alone. Use the
// Modifying annotation when using UPDATE, INSERT and DELETE Queries
@Modifying(clearAutomatically = true)
@Query("UPDATE Session s SET s.available=0")
List<Session>updateStatusNotAvailable(Boolean notAvailable);
CodePudding user response:
Add Transactional
annotation on update query:
@Transactional
@Modifying(clearAutomatically = true)
@Query("UPDATE Session s SET s.available = false)
List<Session> updateStatusNotAvailable();
While calling above method:
List<Session> sessions = sessionRepository.updateStatusNotAvailable();
CodePudding user response:
In order to set your variable in your case "notAvailable", you have to inject your variable into Query annotation. There are two examples:
@Modifying(clearAutomatically = true)
@Query("UPDATE Session s SET s.available=?1")
List<Session>updateStatusNotAvailable(Boolean notAvailable);
Path param example:
// path param example
@Modifying(clearAutomatically = true)
@Query("UPDATE Session s SET s.available=:notAvailable")
List<Session>updateStatusNotAvailable(@Param("notAvailable") Boolean notAvailable);