我遇到了擴展存盤庫的問題CrudRepository,特別是更新和洗掉queries。倉庫界面如下圖:
import com.rmit.sept.bk_loginservices.model.Business;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
public interface BusinessRepository extends CrudRepository<Business, Long> {
// Don't use `SELECT * FROM Business WHERE businessId = :businessId` it doesn't work!
@Query("FROM Business b WHERE b.userKey = :userKey")
Business getBusinessByUserPrimaryKey(Long userKey);
@Modifying
@Transactional
@Query("UPDATE Business b SET b.approved = 1 WHERE b.businessId = :businessId")
void approveBusiness(String businessId);
@Modifying
@Transactional
@Query("DELETE FROM Business WHERE businessId = :businessId")
void rejectBusiness(String businessId);
}
無論出于何種原因,approveBusiness和rejectBusiness方法都不會拋出任何錯誤并且不會修改 MySQL 資料庫。我不知道為什么要這樣做,并且正在努力找到問題所在。到目前為止,我有:
- 通過打開驗證是否正在執行正確的查詢
spring.jpa.show-sql=true - 驗證控制器和服務層是否按預期運行,并且正在傳遞正確的
businessId引數。 - 洗掉并讀取了
@Transactional類級別和每個方法的注釋(如圖所示)
任何輸入都會很棒。這是在 中使用的配置application.properties:
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57InnoDBDialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
這是商務艙本身:
package com.rmit.sept.bk_loginservices.model;
import javax.persistence.*;
@Entity
@Table(name="business")
public class Business{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true, name = "businessId")
private String businessId;
@Column(name = "approved")
private Boolean approved;
@Column(name = "businessAddress")
private String businessAddress;
@Column(name = "userKey")
private Long userKey;
// Hibernate needs this
public Business(){
}
public Business(String businessId, boolean isApproved, String businessAddress, Long userKey){
this.businessId = businessId;
this.approved = isApproved;
this.businessAddress = businessAddress;
this.userKey = userKey;
}
public Long getUserKey() {
return userKey;
}
public void setUserKey(Long userKey) {
this.userKey = userKey;
}
public String getBusinessAddress() {
return businessAddress;
}
public void setBusinessAddress(String businessAddress) {
this.businessAddress = businessAddress;
}
public String getBusinessId() {
return businessId;
}
public void setBusinessId(String businessId){
this.businessId = businessId;
}
public Boolean getApproved() {
return approved;
}
public void setApproved(Boolean isApproved) {
this.approved = isApproved;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
uj5u.com熱心網友回復:
您能否檢查一下您的主鍵是否為 Long 型別但是,您以字串格式傳遞了它?嘗試進行型別轉換以檢查問題是否是由于型別轉換造成的。
此外,請確保您正在修改現有物體。
uj5u.com熱心網友回復:
我認為您需要指定引數的名稱,例如
void approveBusiness(@Param("businessId") String businessId);
// it's org.springframework.data.repository.query.Param class for import
至于rejectBusiness,我會另外嘗試Param 更改注釋@Modifying 如下:
@Modifying(clearAutomatically = true)
@Query("DELETE FROM Business WHERE businessId = :businessId")
void rejectBusiness(@Param("businessId") String businessId);
嘗試洗掉兩種方法的 @Transactional 注釋
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/341624.html
