我是春季新手,所以我还不完全了解如何进行自定义查询。我在下面尝试了此代码,但它给了我一些错误。
我有这2张桌子:
计划表
计划覆盖范围
我想使用“ TEST123” plan_code来获得所有的计划覆盖范围描述。
这是我当前的代码:
Plan.java
@Entity
@Table(name="plan")
public class Plan {
/....
@OneToMany(targetEntity=PlanCoverage.class, mappedBy="plan",cascade=CascadeType.ALL, fetch = FetchType.LAZY)
private List<PlanCoverage> planCoverage;
public List<PlanCoverage> getPlanCoverage() {
return planCoverage;
}
public void setPlanCoverage(List<PlanCoverage> planCoverage) {
this.planCoverage = planCoverage;
}
private String coverage_description;
public String getCoverage_description() {
return coverage_description;
}
}
PlanCoverage.java
@Entity
@Table(name="plan_coverage")
public class PlanCoverage {
@ManyToOne()
@JoinColumn(name="plan_code", referencedColumnName = "plan_code",insertable=false, updatable=false)
private Plan plan;
public Plan getPlan() {
return plan;
}
public void setPlan(Plan plan) {
this.plan = plan;
}
}
PlanRepository.java
public interface PlanRepository extends CrudRepository<Plan, String> {
@Query(nativeQuery = true, value= "SELECT * FROM plan_coverage WHERE plan_code=:planCode")
public List<Plan> findPlanCoverage(@Param("planCode") String planCode);
}
PlanController.java
@RequestMapping(value="/PlanDescription/{plan_code}", method=RequestMethod.GET)
public ModelAndView planDescription(@PathVariable String plan_code) {
ModelAndView model = new ModelAndView();
Plan plan = planService.getPlanById(plan_code);
model.addObject("planForm",plan);
List<Plan> coverageList = planService.findByPlanCode(plan_code);
model.addObject("coverageList",coverageList);
model.setViewName("plan_description");
return model;
}
plan_description.jsp
<c:forEach items="${coverageList }" var="coverage">
<span>${coverage.coverage_description}</span><br>
</c:forEach>
我想访问险种说明,但这给了我这个错误。
任何帮助将不胜感激。
答案 0 :(得分:0)
您的实体“计划”没有称为“ coverage_description”的属性(或简单地说是“ getter方法”)。您需要先定义它。自动取款机在计划实体上仅具有“ planCoverage”作为有用的属性,可在您的jsp页面中使用。