gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:54:37 25 4
gpt4 key购买 nike

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

这是我的实体类、PK 类和实体的 CrudRepository 接口(interface):

@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);
}

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

最佳答案

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

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

关于java - Spring Data JPA CrudRepository 中定义的自定义更新查询无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37341833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com