i'm trying to add a register in database using hibernate (OneToMany, ManyToOne) + spring mvc, i'm using a converter to convert String to object....this is my code:
Model
@Entity
@Table(name="sl_players")
public class Player implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id_player")
private int id;
@Column(name="name_player")
private String name
@ManyToOne
@JoinColumn(name="id_team")
private Team team;
--------------------------------------------------------
@Entity
@Table(name="sl_teams")
public class Team implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id_team")
private int id;
@Column(name="name_team")
private String name;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="id_team")
private List<Player> players = new ArrayList<Player>();
CONTROLLER
@Controller
public class PlayerController {
@Autowired
private PlayerService playerService;
@Autowired
private TeamService teamService;
@RequestMapping(value="/admin/players/save", method=RequestMethod.POST)
public String addPlayer(@RequestParam("teamId") Integer teamId, @ModelAttribute("player") Player player, BindingResult result){
System.out.print("Result: "+result.getAllErrors());
if(0 == player.getId())
this.playerService.save(player);
else
this.playerService.update(player,teamId);
return "redirect:/admin/players";
}
@ModelAttribute("teamOptions")
public List<Team> getTeam(){
return new LinkedList<Team>(teamService.loadAll());
}
Converter
@Component
public class StringToTeam implements Converter<String, Team>{
@Autowired
TeamService teamService;
@Override
public Team convert(String source) {
System.out.print("*** inside Converter ***");
return teamService.get(Integer.parseInt(source));
}
servlet-context
<mvc:annotation-driven conversion-service="conversionService"/>
<beans:bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<beans:property name="converters">
<beans:list>
<beans:bean id="roleToUserProfile" class="com.project.converter.StringToTeam"/>
</beans:list>
</beans:property>
</beans:bean>
this is the output:
Result: [Field error in object 'player' on field 'team': rejected value [2]; codes [typeMismatch.player.team,typeMismatch.team,typeMismatch.com.project.model.Team,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [player.team,team]; arguments []; default message [team]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.project.model.Team' for property 'team'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.project.soccerleagueapp.model.Team] for property 'team': no matching editors or conversion strategy found]]
ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Column 'id_team' cannot be null
Anyone can help me, please?
Aucun commentaire:
Enregistrer un commentaire