繁体   English   中英

JPA CrudRepository 更新查询的问题

[英]Problem with JPA CrudRepository Update Query

我遇到了扩展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);
}

无论出于何种原因, approveBusinessrejectBusiness方法都不会抛出任何错误,也不会修改 MySQL 数据库。 我不知道为什么要这样做,并且正在努力找到问题所在。 到目前为止,我有:

  1. 通过打开spring.jpa.show-sql=true验证是否正在执行正确的查询
  2. 验证控制器和服务层是否按预期运行,并且正在传递正确的businessId参数。
  3. 在类级别和每个方法中删除并重新读取@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;
    }
}

您能否检查一下您的主键是否为 Long 类型但是,您以字符串格式传递了它? 尝试进行类型转换以检查问题是否是由于类型转换造成的。

此外,请确保您正在修改现有实体。

我认为您需要指定参数的名称,例如

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 注释

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM