【问题标题】:Invalid custom update query defined in Spring Data JPA CrudRepositorySpring Data JPA CrudRepository 中定义的自定义更新查询无效
【发布时间】:2016-09-17 10:22:36
【问题描述】:

我在 Spring CrudRepository 中有一个自定义 JPQL 查询不起作用。

这是我的实体类,PK类和实体的CrudRepository接口:

@Entity(name = "TBL_PRINCIPAL_CREDENTIAL")
public class PrincipalCredential {

    @EmbeddedId
    private PrincipalCredentialPK principalCredentialPK;

    // ...getter & setter for principalCredentialPK
}

@Embeddable
public class PrincipalCredentialPK implements Serializable {

    @Column(name = "PRINCIPAL_TYPE_ID", nullable = false)
    private String principalTypeID;

    @Column(name = "PRINCIPAL_ID", nullable = false)
    private String principalID;

    @Column(name = "CREDENTIAL_ID", nullable = false)
    private Integer credentialID;

    @Column(name = "CREDENTIAL_TYPE_ID", nullable = false)
    private String credentialTypeID;

    // ...getters & setters for all fields...
}

@Transactional
public interface PrincipalCredentialRepository extends CrudRepository<PrincipalCredential, PrincipalCredentialPK> {

    @Modifying
    @Query("update PrincipalCredential pc set pc.principalCredentialPK.principalID =:newPrincipalID " +
            "where pc.principalCredentialPK.principalID =:oldPrincipalID and pc.principalCredentialPK.principalTypeID =:principalType")
    void updatePrincipalID(@Param("oldPrincipalID") String oldPrincipalID, @Param("newPrincipalID") String newPrincipalID,
                           @Param("principalType") String principalType);
}

当我使用 SpringBoot 启动我的项目时,无法实例化存储库 bean,我得到以下异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'principalCredentialRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract void com.consorsbank.services.banking.caas.repositories.PrincipalCredentialRepository.updatePrincipalID(java.lang.String,java.lang.String,java.lang.String)!

Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract void com.consorsbank.services.banking.caas.repositories.PrincipalCredentialRepository.updatePrincipalID(java.lang.String,java.lang.String,java.lang.String)!

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: PrincipalCredential is not mapped [update PrincipalCredential pc set pc.principalCredentialPK.principalID =:newPrincipalID where pc.principalCredentialPK.principalID =:oldPrincipalID and pc.principalCredentialPK.principalTypeID =:principalType]

对于另一个存储库,此查询也有效,不同之处在于另一个实体的 PK 更简单,并且两个 id 都在那里提供...

@Entity
@Table(name = "TBL_PRINCIPALS")
public class Principal implements Serializable {

    @EmbeddedId
    private PrincipalPK principalPK;

    @OneToOne
    @JoinColumn(name = "PRINCIPAL_TYPE_ID", insertable = false, updatable = false)
    private PrincipalType principalType;

    @Column(name = "USER_ID")
    private Integer userID;

    @Column(name = "VALID_UNTIL")
    private Date validUntil;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "ZAAS_TBL_PRINCIPAL_CREDENTIAL",
            joinColumns = {@JoinColumn(name = "PRINCIPAL_ID"), @JoinColumn(name = "PRINCIPAL_TYPE_ID")},
            inverseJoinColumns = {@JoinColumn(name = "CREDENTIAL_ID"), @JoinColumn(name="CREDENTIAL_TYPE_ID")})
    public Set<Credential> credentials;

    // ...getters and setters...
}

@Embeddable
public class PrincipalPK implements Serializable {

    @Column(name = "PRINCIPAL_TYPE_ID", nullable = false)
    private String principalTypeID;

    @Column(name = "PRINCIPAL_ID", nullable = false)
    private String principalID;

    // ...getters and setters
}

@Transactional
public interface PrincipalsRepository extends CrudRepository<Principal, PrincipalPK> {

    @Modifying
    @Query("update Principal p set p.principalPK.principalID =:newPrincipalID " +
            "where p.principalPK.principalID =:oldPrincipalID and p.principalPK.principalTypeID =:principalType")
    void updatePrincipalID(@Param("oldPrincipalID") String oldPrincipalID, @Param("newPrincipalID") String newPrincipalID,
                           @Param("principalType") String principalType);
}

所以上面的查询正在工作...... 有人可以指出我在 PrincipalCredentialRepository 中定义的查询缺少什么吗?

【问题讨论】:

    标签: java spring jpa spring-data spring-data-jpa


    【解决方案1】:

    实体定义似乎有误。使用以下注释

    @Entity
    @Table(name = "TBL_PRINCIPAL_CREDENTIAL")
    public class PrincipalCredential {
    //...
    

    【讨论】:

    • 确实是问题所在...看不到 :) 谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-12-07
    • 2015-03-25
    • 1970-01-01
    • 2012-09-13
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    相关资源
    最近更新 更多