Spring Data JPA CrudRepository中定义的自定义更新查询无效

时间:2016-05-20 08:34:27

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

我在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中定义的查询缺少什么吗?

1 个答案:

答案 0 :(得分:1)

实体定义似乎是错误的。使用以下注释

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