We have 2 entities having OneToMany and ManyToOne cardinality:
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 can have multiple Tags whereas each tag is associated with one Plan. Here is the sample repository for the tag entity:
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);
}
But when running the above code, getting the following error:
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
Code is compiled as per Sprig JPA. How to resolve this issue. Any pointers are highly appreciated.
CodePudding user response:
I think you should correct your repo methodName from findByPlan() to findByPlanId() and remove that @Query() it should work just by using findByPlanId() method.
In case it does not work, then try making changes on the @Query line. @Query("Select te from TagEntity te where te.plan in ?1") Remove .id from plan.id in query
CodePudding user response:
Select pe.* from TagEntity pe where pe.plan_id
in ?1
I guess plan_id was your column name
CodePudding user response:
When you are introducing a custom query it's better to follow query language instead. where plan.id is plan_id in your case.
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); }