Search code examples
javaspringjspspring-form

NotReadablePropertyException Bean property is not readable or has an invalid getter method


Having error: org.springframework.beans.NotReadablePropertyException: Invalid property 'organizations' of bean class [com.sprhib.model.Team]: Bean property 'organizations' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

There are tables: teams, organization. There is One-to-Many relationship.

Team model

@Entity
@Table(name="teams")
public class Team {

    private Organization organization;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "FK_Organization_id", nullable = false)
    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }
}

Organization

@Entity
@Table(name = "organization")
public class Organization {
    private Set<Team> teams;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "organization")
    public Set<Team> getTeams() {
        return teams;
    }

    public void setTeams(Set<Team> teams) {
        this.teams = teams;
    }
}

JSP

<form:form method="POST" commandName="team" action="${pageContext.request.contextPath}/team/add.html">    
     <form:select path="organizations">
         <form:option value="${organization}">
             <c:out value="${organization.name}"/>
         </form:option>
     </form:select>
</form:form>

How to make spring get all organizations to JSP?

UPDATE:

I pass a list of all organizations and new Team object to jsp using controller:

@Controller
@RequestMapping(value="/team")
public class TeamController {

    @Autowired
    private TeamService teamService;

    @Autowired
    private OrganizationService organizationService;

    @RequestMapping(value="/add", method=RequestMethod.GET)
    public ModelAndView addTeamPage() {
        ModelAndView modelAndView = new ModelAndView("teams/add-team-form");
        modelAndView.addObject("team", new Team());
        modelAndView.addObject("organizations", organizationService.getOrganizations());

        return modelAndView;
    }

UPDATE2:

Does commandName="team" restricts usage of more than 1 model attributes, in this case there are two: organizations and team? How to make it work?

Custom attribute name: commandName Description: Name of the model attribute under which the form object is exposed. Defaults to 'command'. Required: false Can have runtime value: true


Solution

  • <form:option> is used to append a single option to a select list. You can add all of elements in a collection using <form:options>"

    <form:select path="organization">
        <form:options items="${organizations}" />
    </form:select>
    

    you can also use both, like for adding a default unselected option:

    <form:select path="organization">
        <form:option value="" label="- Select -"/>
        <form:options items="${organizations}" />
    </form:select>
    

    The value which will be selected, will end in the Team bean passed thru the form; the path attribute is used for determining which property will be set in Team, in this case it is organisation (not organisations)