У меня есть запрос на вставку в JPQL следующим образом:
@Modifying
@Query(value = "insert into Product_Category (product_id, category_id , description , numberOfProduct, image, price ) values (:product_id, :category_id, :description, :numberOfProduct, :image, :price)", nativeQuery = true)
void insertProduct(@Param("product_id") int product_id, @Param("category_id") int category_id,
@Param("description") String description, @Param("numberOfProduct") int numberOfProduct,
@Param("image") String image, @Param("price") int price);
Я не понимаю, почему это не работает. У меня есть эта ошибка:
javax.persistence.TransactionRequiredException: Executing an update/delete query
Спасибо, я исправил транзакционную аннотацию: @Transactional @Modifying @Query(value = "insert into Product_Category (product_id, category_id , description , numberOfProduct, image, price ) values (:product_id, :category_id, :description, :numberOfProduct, :image, :price)", nativeQuery = true) void insertUser(@Param("product_id") int product_id, @Param("category_id") int category_id, @Param("description") String description, @Param("numberOfProduct") int numberOfProduct, @Param("image") String image, @Param("price") int prices);
@SInsansun Не стесняйтесь добавлять свой комментарий в качестве ответа, чтобы люди могли проголосовать за него, и вы могли его принять.




Вам нужно обернуть свои операторы с помощью @Transactional. Чтобы быть совместимым с JPA, вы должны использовать javax.transaction.Transactional, хотя в некоторых версиях комбинация spring-data, если javax.transaction.Transactional вызывает проблемы, вы можете попробовать использовать org.springframework.transaction.annotation.Transactional.
Имейте в виду, что в JPA все дело в переходах между состояниями сущностей. Когда вы изменяете объекты, hiberante обновляет контекст сохранения и периодически сбрасывает его. Порядок, в котором это происходит, следующий:
Поэтому, естественно, вставки и обновления выполняются перед удалением. Когда у вас есть код, который выполняет: удаление + вставку, вставка будет фактически выполнена перед удалением. В таких случаях правильным способом является выборка и обновление.
вы должны открыть сеанс для опроса с BD
import org.springframework.transaction.annotation.Transactional.
@Transactional //try to add this annotation
@Modifying
@Query(value = "insert into Product_Category (product_id, category_id , description , numberOfProduct, image, price ) values (:product_id, :category_id, :description, :numberOfProduct, :image, :price)", nativeQuery = true)
void insertProduct(@Param("product_id") int product_id, @Param("category_id") int category_id,
@Param("description") String description, @Param("numberOfProduct") int numberOfProduct,
@Param("image") String image, @Param("price") int price);
попробуйте использовать org.springframework.transaction.annotation.Transactional вместо javax.transaction.Transactional