我們有 2 個具有 OneToMany 和 ManyToOne 基數的物體:
Class PlanEntity{
@Id
private Long id;
@OneToMany(mappedBy="plan", cascade=CascadeType.ALL, orphanRemoval=true)
private List<B> tags;
.........
}
Class TagEntity{
@Id
private Long id;
@ManyToOne
@JoinColumn(name="plan_id", referencedColumnName="id")
private PlanEntity plan;
.........
}
Plan 可以有多個標簽,而每個標簽都與一個 Plan 相關聯。這是標簽物體的示例存盤庫:
public interface TagEntityRepository extends JpaRepository<TagEntity, Long>{
@Query("Select pe from TagEntity pe where pe.plan.id in ?1")
public List<PlanEntity> findByPlan(Long planId);
}
但是在運行上面的代碼時,出現以下錯誤:
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.repository.TagEntityRepository.findByPlan(java.lang.Long)! No property id found for type PlanEntity! Traversed path: TagEntity.plan
代碼按照 Sprig JPA 編譯。如何解決這個問題。任何指標都受到高度贊賞。
uj5u.com熱心網友回復:
我認為你應該更正你的 repo methodName 從 findByPlan() 到 findByPlanId() 并洗掉@Query() 它應該只使用 findByPlanId() 方法。
如果它不起作用,請嘗試在 @Query 行上進行更改。@Query("Select te from TagEntity te where te.plan in ?1") 從查詢中的 plan.id 中洗掉 .id
uj5u.com熱心網友回復:
選擇 pe.* from TagEntity pe where pe.plan_idin ?1
我猜 plan_id 是你的列名
uj5u.com熱心網友回復:
當您引入自定義查詢時,最好遵循查詢語言。在您的情況下, plan.id 是plan_id。
public interface TagEntityRepository extends JpaRepository<TagEntity, Long>{
@Query("Select pe from TagEntity pe where pe.plan_id like ?1", nativeQuery = true) public List findByPlan(Long planId); }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/336698.html
