0

I'm new to spring so I don't fully understand yet how to make custom queries. I tried this code below but it's giving me some errors.

I have this 2 tables:

Plan Table

enter image description here

Plan Coverage

enter image description here

I want to get all the plan coverage description with a 'TEST123' plan_code.

This is my current 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>

I want to access the coverage description but it's giving me this error.

Any help would be greatly appreciated.

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
trumanblack1025
  • 471
  • 2
  • 8
  • 19

1 Answers1

0

Your Entity „Plan“ does not have a property (or simply speaking a „getter method“) that is called „coverage_description“. You need to define it first. Atm you only have „planCoverage“ as a useful property on the plan entity to use in your jsp page.

Peter Branforn
  • 1,679
  • 3
  • 17
  • 29
  • When I added a getter for coverage_description it's giving me this error: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'plan0_.coverage_description' in 'field list' It's not in my plan table. Please help me :'( Is there any way I can access the coverage_description from the plan entity? – trumanblack1025 Sep 23 '18 at 15:19
  • I had a quick look just now and it looks good to me now. Is the error message really still the same? Sometime it happens to me that I think it is similar and then it slightly changed (I have the doubt that it doesn't associate the 'plan' entity with your EL expression somehow). – Peter Branforn Sep 24 '18 at 13:27
  • It says "Unknown column 'plan0_.coverage_description' in 'field list' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_181]" – trumanblack1025 Sep 24 '18 at 13:45
  • dude, this actually good news: this basically means the SQL query is broke and it cannot find the column (https://stackoverflow.com/questions/1346209/unknown-column-in-field-list-error-on-mysql-update-query). so just fix the query and you are ready to go party :-). – Peter Branforn Sep 26 '18 at 19:08