私の Spring MVC Web アプリケーションは、2 つのモデル クラス Player と Team を使用します。Player と Team の間にはManyToMany 関係があります。モデル クラスは次のとおりです。
@Entity
@Table(name="TEAMS")
public class Team {
@Id
@Column(name="ID")
private int id;
@Column(name="NAME")
@Size(min=2, max=30)
private String name;
@ManyToMany(mappedBy="teams")
private Set<Player> players = new HashSet<Player>();
//getter and setter methods
}
@Entity
@Table(name="PLAYERS")
public class Player {
@Id
@Column(name="ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="players_seq")
@SequenceGenerator(name="players_seq", sequenceName="PLAYERS_SEQ")
private int id;
@Column(name="LASTNAME")
@Size(min=2, max=30)
private String lastname;
@Column(name="FIRSTNAME")
@Size(min=2, max=30)
private String firstname;
@Column(name="BIRTHDAY")
@DateTimeFormat(pattern="MM/dd/yyyy")
@NotNull @Past
private Date dob;
@Column(name="PLAYING_ROLE")
@NotNull
private String playingRole;
@ManyToMany(cascade =CascadeType.ALL)
@JoinTable(name="TEAM_PLAYER",
joinColumns={@JoinColumn(name="PLAYER_ID")},
inverseJoinColumns={@JoinColumn(name="TEAM_ID")})
private Set<Team> teams = new HashSet<Team>();
//getter and setters
}
チェックボックスを使用してチームをプレーヤーに割り当てようとしていますが、JSP は次のようになります
<table>
<form:form modelAttribute="player" method="POST"
action="${editPlayer }">
<tr>
<td>Last name:</td>
<td><form:input path="lastname" /> <form:errors
path="lastname" cssStyle="color:red;" /></td>
</tr>
<tr>
<td>First Name:</td>
<td><form:input path="firstname" /> <form:errors
path="firstname" cssStyle="color:red;" /></td>
</tr>
<tr>
<td>Birthday:</td>
<td><form:input path="dob" /> <form:errors path="dob"
cssStyle="color:red;" /></td>
</tr>
<tr>
<td>Playing Style:</td>
<td><form:input path="playingRole" /> <form:errors
path="playingRole" cssStyle="color:red;" /></td>
</tr>
<tr>
<td>Teams:</td>
<td><form:checkboxes items="${teams}" path="teams"
itemValue="id" itemLabel="name" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Update Player" /></td>
</tr>
</form:form>
</table>
私の PlayerController.java は次のようになります
@Controller
public class PlayerController {
@Autowired
private PlayerService playerService;
@Autowired
private TeamService teamService;
@ModelAttribute("teams")
public List<Team> getTeams(){
return teamService.getTeams();
}
@RequestMapping(value="/player/edit/{id}", method=RequestMethod.GET)
public ModelAndView updatePlayerPage(@PathVariable int id){
Player player = playerService.getPlayer(id);
ModelAndView modelAndView = new ModelAndView("player/edit");
modelAndView.addObject("player", player);
return modelAndView;
}
@RequestMapping(value="/player/edit/{id}", method=RequestMethod.POST)
public ModelAndView updatePlayer(@ModelAttribute @Valid Player player, BindingResult result, @PathVariable int id){
if(result.hasErrors()){
System.out.println("HAS ERRORS!. Number of errors: "+result.getAllErrors().size());
for(Object o : result.getAllErrors()){
System.out.println(o);
}
System.out.println("\n Player details: "+ player );
return new ModelAndView("player/edit");
}
playerService.updatePlayer(player);
ModelAndView modelAndView = new ModelAndView("home");
String message = "Player updated successfully";
modelAndView.addObject("message", message);
return modelAndView;
}
//it has got some other methods also that does some other functionality
}
JSP ページは適切にレンダリングされていますが、保存しようとすると例外が発生します
Field error in object 'player' on field 'teams': rejected value [5050]; codes [typeMismatch.player.teams,typeMismatch.teams,typeMismatch.java.util.Set,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [player.teams,teams]; arguments []; default message [teams]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Set' for property 'teams'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.sunil.model.Team] for property 'teams[0]': no matching editors or conversion strategy found]
例外の背後にある理由は、チームをプレーヤーに割り当てる必要があるためですが、これを行う方法がわかりません. 誰かがこれを解決する方法を教えてください。
ありがとう