I want to create MySQL Event programmatically with Query Method (@Query) in spring data. And my code like this:
TestService.java
@Transactional(propagation=Propagation.REQUIRED)
public void saveAndCreateEvent(Test test) {
save(test);
testStartEvent(test);
testEndEvent(test);
}
@Transactional(propagation=Propagation.REQUIRED)
public void save(Test test){
testRepository.save(test);
}
@Transactional(propagation=Propagation.REQUIRED)
public void testStartEvent(Test test){
byte statusId = 2;//Open
testRepository.createEventTestOpen(test.getId(), statusId, test.getStartDate());
};
TestRepository.java
@Query(value="DELIMITER |"
+ " CREATE EVENT test_open"
+ " ON SCHEDULE AT :date"
+ " DO"
+ " BEGIN"
+ " UPDATE `test`"
+ " SET `test_status_id` = :statusId"
+ " WHERE `id` = :id ;"
+ " UPDATE `user`"
+ " SET ...
+ " WHERE `id` = (SELECT `user_id` FROM `test_participant` WHERE `test_id` = :id);"
+ " END |"
+ " DELIMITER ;", nativeQuery=true)
public void createEventTestOpen(
@Param("id") Long id,
@Param("statusId") byte statusId,
@Param("date") Date date);
But i get error like this:
o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1064, SQLState: 42000 o.h.engine.jdbc.spi.SqlExceptionHelper : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER | CREATE EVENT test_open ON SCHEDULE AT '2019-02-27 05:20:0' at line 1 The Ex: org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
I call saveAndCreateEvent() method to save test details and create start_event and end_event in MySQL. The problem occurred in method testStartEvent() that call createEventTestOpen().
I guess that happen because DELIMITER |
.
If I change query, without delimiter
and only one update statement in body (first one), it work fine.
UPDATED 27 Feb 2019 : This method is not work fine. This query also cause commit transaction immediately.
@Query(value="CREATE EVENT test_open"
+ " ON SCHEDULE AT :date"
+ " DO"
+ " UPDATE `test`"
+ " SET `test_status_id` = :statusId"
+ " WHERE `id` = :id ;", nativeQuery=true)
public void createEventTestOpen(
@Param("id") Long id,
@Param("statusId") byte statusId,
@Param("date") Date date);
If I change query, without delimiter
and with two update statement in body it work too, but transaction committed before method testEndEvent().
@Query(value="CREATE EVENT test_open"
+ " ON SCHEDULE AT :date"
+ " DO"
+ " BEGIN"
+ " UPDATE `test`"
+ " SET `test_status_id` = :statusId"
+ " WHERE `id` = :id ;"
+ " UPDATE `user`"
+ " SET ...
+ " WHERE `id` = (SELECT `user_id` FROM `test_participant` WHERE `test_id` = :id);"
+ " END", nativeQuery=true)
public void createEventTestOpen(
@Param("id") Long id,
@Param("statusId") byte statusId,
@Param("date") Date date);
I am using spring-boot-starter-parent version 1.5.13.RELEASE.
How can I make a MySQL event with delimiter using the query method in Spring...?