我通过DTO从表单中获取数据并将其映射到我的Hibernate实体。我想跳过一个日期字段,因为传入的数据是所有字符串,我需要将字符串字段转换为与其他字段分开的日期。其余字段将从传入的DTO直接映射。该过程适用于第一个请求,但在后续请求中,抛出以下异常:
1) A mapping already exists for com.ibm.calypso.entity.WmCompIdfn.setEffStartDate().
1 error
at org.modelmapper.internal.Errors.toConfigurationException(Errors.java:250)
at org.modelmapper.internal.TypeMapImpl.addMappings(TypeMapImpl.java:76)
at org.modelmapper.internal.TypeMapStore.getOrCreate(TypeMapStore.java:110)
at org.modelmapper.ModelMapper.addMappings(ModelMapper.java:93)
at com.ibm.calypso.service.CompanyServiceImpl.updateIdentificationIdfn(CompanyServiceImpl.java:48)
at com.ibm.calypso.controller.CompanyController.updateIdentificationIdfn(CompanyController.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:651)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at com.ibm.calypso.filter.CORSFilter.doFilterInternal(CORSFilter.java:29)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2500)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2489)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
错误已经提出,但似乎没有人回应。
https://github.com/jhalterman/modelmapper/issues/103
我的代码结构类似于链接中的代码结构。
答案 0 :(得分:9)
我不确定你是否已找到解决方案。但我做了一个工作来解决这个问题。 我在我的服务方法中做了modelMapper.addMappings(myPropertymap)。因此它尝试为每个传入的请求添加映射。我添加了以下检查以确保仅在映射不存在时才添加
// Before
modelMapper.addMappings(myPropertyMap);
// After
TypeMap<Source, Dest> typeMap = modelMapper.getTypeMap(Source.class, Dest.class);
if (typeMap == null) { // if not already added
modelMapper.addMappings(myPropertyMap);
}
答案 1 :(得分:2)
上述解决方法有效,但条件如下:
if (typeMap == null) {
modelMapper.addMappings(myPropertyMap);
}
使用非空条件映射永远不会添加到modelMapper。
答案 2 :(得分:1)
我知道这是一个老问题,但是我有相同的错误,但原因可能不同。这是引起问题的代码:
private static final ModelMapper mapper = new ModelMapper();
{
PropertyMap<SomeSourceClass, SomeTargetClass> map =
new PropertyMap<SomeSourceClass, SomeTargetClass>() {
@Override
protected void configure() {
// my configuration
}
};
mapper.addMappings(map);
}
问题在于ModelMapper是一个静态字段,但是每次创建此类的新对象时都会发生初始化,因此正确的代码改为使用static initializer:
private static final ModelMapper mapper = new ModelMapper();
static {
PropertyMap<SomeSourceClass, SomeTargetClass> map =
new PropertyMap<SomeSourceClass, SomeTargetClass>() {
@Override
protected void configure() {
// my configuration
}
};
mapper.addMappings(map);
}
希望这可以帮助遇到相同问题的人!