无法将类型'java.lang.String'的属性值转换为所需类型,Column不能为null,转换器

时间:2016-06-26 19:59:24

标签: java spring spring-mvc converter

我正在尝试使用hibernate(OneToMany,ManyToOne)+ spring mvc在数据库中添加一个寄存器,我正在使用转换器将String转换为对象....这是我的代码:

模型

def flip(L):
    return [ x ^ 1 for x in L ]

L = [0,1,0,1,1,0,1,0,0,1]
print flip(L)

CONTROLLER

@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
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());
}

servlet的上下文

@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));
}

这是输出:

<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>

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

试试这个例子:

<强> Myconverter

@Component("applicationToFluxConverter")
public class ApplicationToFluxConverter implements Converter<String, PIL_P_APPLCTN> {

    static final Logger logger = LoggerFactory.getLogger(ApplicationToFluxConverter.class);

    @Autowired
    @Qualifier("pIL_P_APPLCTN_SERVICE")
    IService<PIL_P_APPLCTN> appService;

    /**
     * 
     * 
     * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
     */
    public PIL_P_APPLCTN convert(String element) {
        Integer id = Integer.parseInt((String) element);
        PIL_P_APPLCTN app = appService.findById(id);
        logger.info("Application : {}", app);
        return app;
    }
}

<强>的AppConfig:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.fussa.fyby")
public class AppConfig extends WebMvcConfigurerAdapter {


    @Autowired
    ApplicationToFluxConverter applicationToFluxConverter;

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(applicationToFluxConverter);
    }
//....
    }