相关疑难解决方法(0)

Spring Data JPA Update @Query没有更新?

我有一个更新查询:

@Modifying
@Transactional
@Query("UPDATE Admin SET firstname = :firstname, lastname = :lastname, login = :login, superAdmin = :superAdmin, preferenceAdmin = :preferenceAdmin, address =  :address, zipCode = :zipCode, city = :city, country = :country, email = :email, profile = :profile, postLoginUrl = :postLoginUrl WHERE id = :id")
public void update(@Param("firstname") String firstname, @Param("lastname") String lastname, @Param("login") String login, @Param("superAdmin") boolean superAdmin, @Param("preferenceAdmin") boolean preferenceAdmin, @Param("address") String address, @Param("zipCode") String zipCode, @Param("city") String city, @Param("country") String country, @Param("email") String email, @Param("profile") String …
Run Code Online (Sandbox Code Playgroud)

java jpa spring-data

47
推荐指数
3
解决办法
12万
查看次数

为什么手动定义的Spring Data JPA删除查询不会触发级联?

我有以下问题:当我尝试删除具有以下关系的实体时:

@OneToMany(mappedBy="pricingScheme", cascade=CascadeType.ALL, orphanRemoval=true)
private Collection<ChargeableElement> chargeableElements;
Run Code Online (Sandbox Code Playgroud)

CrudRepository通过提供删除方法它消除其所有的充电元件,其是精细沿着实体。当我尝试使用自定义删除时出现问题:

@Modifying
@Query("DELETE FROM PricingScheme p WHERE p.listkeyId = :listkeyId")
void deleteByListkeyId(@Param("listkeyId") Integer listkeyId);
Run Code Online (Sandbox Code Playgroud)

它说:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: 
  Cannot delete or update a parent row: a foreign key constraint fails
  (`listkey`.`chargeableelements`, CONSTRAINT `FK_pox231t1sfhadv3vy7ahsc1wt` 
  FOREIGN KEY (`pricingScheme_id`) REFERENCES `pricingschemes` (`id`))
Run Code Online (Sandbox Code Playgroud)

为什么不允许我这样做?难道@Query方法不支持级联特性?我知道我可以findByListkeyId(…)先使用标准的delete方法删除持久性实体,但这是不雅致的。是否可以@Query按照我尝试的方式使用自定义方法?

jpa cascade jpql cascading-deletes spring-data-jpa

1
推荐指数
1
解决办法
4457
查看次数