第一实体:
@Entity
@Table(name = "PIL_P_DOMAINE")
public class PIL_P_DOMAINE {
@NotBlank
@Column(nullable = false, unique = true, name = "DOMAINE_ID")
private String DOMAINE_ID;
@NotBlank
@Column(nullable = false, name = "DOMAINE_DS")
private String DOMAINE_DS;
public PIL_P_DOMAINE() {
}
}
第二实体:
@Entity
public class PIL_P_DOMAINE_TABLE {
@NotBlank
@JoinColumn(nullable = false, name = "DOMAINE_ID")
@ManyToOne
private PIL_P_DOMAINE DOMAINE_ID;
@NotBlank
@Column(nullable = false, name = "DATABASE_NM")
private String DATABASE_NM;
@NotBlank
@Column(nullable = false, name = "TABLE_NM")
private String TABLE_NM;
@Column(name = "APPLCTN_COLNM_NM")
private String APPLCTN_COLNM_NM;
@Column(name = "CRITERE_FILTRE_NM")
private String CRITERE_FILTRE_NM;
public PIL_P_DOMAINE_TABLE() {
}
}
转换器:
@Component("domaineToDomTabConverter")
public class DomaineToDomTabConverter implements Converter<Object, PIL_P_DOMAINE> {
static final Logger logger = LoggerFactory.getLogger(DomaineToDomTabConverter.class);
@Autowired
@Qualifier("pIL_P_DOMAINE_SERVICE")
IService<PIL_P_DOMAINE> domService;
/**
*
*
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/
public PIL_P_DOMAINE convert(Object element) {
Integer id = Integer.parseInt((String) element);
PIL_P_DOMAINE dom = domService.findById(id);
logger.info("Domaine : {}", dom);
return dom;
}
}
的AppConfig:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.fussa.fyby")
public class AppConfig extends WebMvcConfigurerAdapter {
@Autowired
DomaineToDomTabConverter domaineToDomTabConverter;
@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(domaineToDomTabConverter);
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
@Override
public void configurePathMatch(PathMatchConfigurer matcher) {
matcher.setUseRegisteredSuffixPatternMatch(true);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
当我尝试保留 PIL_P_DOMAINE 对象时,添加
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(domaineToDomTabConverter);
}
到我的 AppConfig 类,对象是持久化的(y)。 我遇到的问题是,当我想要持久保存包含对象 PIL_P_DOMAINE 的PIL_P_DOMAINE_TABLE对象时,我添加了 DomaineToDomTabConverter 以转换为字符串我将获得表单中的对象,所以我在我的 AppCongig 类中添加了转换器,由于这些修改我不能保留 PIL_P_DOMAINE 对象,我收到此错误:
警告: org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - 无法绑定请求元素:org.springframework.beans.TypeMismatchException:转换失败 [com.fussa.fyby.model.PIL_P_DOMAINE]类型的值为必需类型 [com.fussa.fyby.model.PIL_P_DOMAINE];嵌套异常是 org.springframework。 core.convert.ConversionFailedException:失败 从类型[com.fussa.fyby.model.PIL_P_DOMAINE]转换为类型 [@javax.validation.Valid @ org.springframework.web.bi nd.annotation.ModelAttribute com.fussa.fyby.model.PIL_P_DOMAINE] for value'com.fussa.fyby.model.PIL_P_DOMAINE@716898c0';嵌套异常 是java.lang.ClassCastExce ption:com.fussa.fyby.model.PIL_P_DOMAINE 无法强制转换为java.lang.String
如果我想要保留第二个实体而不是第一个实体,我怎样才能调用转换器?
感谢您的任何建议..
答案 0 :(得分:0)
通过参数化类型i转换为字符串而不是对象
来解决此问题@Component("domaineToDomTabConverter")
public class DomaineToDomTabConverter implements Converter<String, PIL_P_DOMAINE> {
public PIL_P_DOMAINE convert(Stringelement) {
//
}
}