无法确定Field中的写入方法:Dozer中Class的'id'

时间:2014-02-11 21:00:35

标签: java hibernate mapping jetty dozer

我在一个项目上使用Dozer并且它工作正常,但过了一段时间我之前执行的相同映射开始给我下一个例外:

15:13:29,928 ERROR MappingProcessor:295 - Field mapping error -->
MapId: null
Type: null
Source parent class: org.persistencias.PP
Source field name: id
Source field type: class java.lang.Long
Source field value: 1
Dest parent class: org.dtos.PPDTO
Dest field name: id
Dest field type: java.lang.Long
org.dozer.MappingException: java.lang.NoSuchMethodException: Unable to determine write method for Field: 'id' in Class: class org.dtos.PPDTO
at org.dozer.util.MappingUtils.throwMappingException(MappingUtils.java:82)
at org.dozer.propertydescriptor.GetterSetterPropertyDescriptor.invokeWriteMethod(GetterSetterPropertyDescriptor.java:280)
at org.dozer.propertydescriptor.GetterSetterPropertyDescriptor.setPropertyValue(GetterSetterPropertyDescriptor.java:102)
at org.dozer.fieldmap.FieldMap.writeDestValue(FieldMap.java:94)
at org.dozer.MappingProcessor.writeDestinationValue(MappingProcessor.java:895)
at org.dozer.MappingProcessor.mapFromFieldMap(MappingProcessor.java:349)
at org.dozer.MappingProcessor.mapField(MappingProcessor.java:288)
at org.dozer.MappingProcessor.map(MappingProcessor.java:248)
at org.dozer.MappingProcessor.map(MappingProcessor.java:197)
at org.dozer.MappingProcessor.map(MappingProcessor.java:187)
at org.dozer.MappingProcessor.map(MappingProcessor.java:124)
at org.dozer.MappingProcessor.map(MappingProcessor.java:119)
at org.dozer.DozerBeanMapper.map(DozerBeanMapper.java:120)
at org.server.ServiciosP.getTodasPP(ServiciosP.java:86)
at org.server.ServicioImpl.getTodasPP(ServicioImpl.java:7668)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
    Caused by: java.lang.NoSuchMethodException: Unable to determine write method for Field: 'id' in Class: class org.dtos.PPDTO
at org.dozer.propertydescriptor.JavaBeanPropertyDescriptor.getWriteMethod(JavaBeanPropertyDescriptor.java:47)
at org.dozer.propertydescriptor.GetterSetterPropertyDescriptor.invokeWriteMethod(GetterSetterPropertyDescriptor.java:278)
... 40 more
org.dozer.MappingException: java.lang.NoSuchMethodException: Unable to determine write method for Field: 'id' in Class: class org.dtos.PPDTO

但我在我的POJO和我的DTO上有id setter方法。

POJO:

// Property accessors
@GenericGenerator(name = "generator", strategy = "increment")
@Id
@GeneratedValue(generator = "generator")
@Column(name = "id", unique = true, nullable = false)
public Long getId() {
    return this.id;
}

public void setId(Long id) {
    this.id = id;
}

DTO:

public Long getId() {
    return this.id;
}

public void setId(Long id) {
    this.id = id;
}

最后我的映射代码:

<mapping>
  <class-a>org.persistencias.PP</class-a>
  <class-b>org.dtos.PPDTO</class-b>
</mapping>

这里有没有人知道如何解决这个问题或者指出问题根源?

我使用的是dozer 5.4.0(las稳定版)

3 个答案:

答案 0 :(得分:1)

正如本期GitHub中所述,它似乎是不同库中的错误组合: https://github.com/DozerMapper/dozer/issues/118

JDK,BeanUtils和Dozer中存在一些错误,它们会导致这种奇怪的行为。

当我在界面中定义getter并在类本身中实现setter时,我遇到了同样的问题。

<强> MyInterface.java

public interface MyInterface {
    String getMe();
}

<强> MyClass.java

public class MyClass implements MyInterface {

    private String me;

    @Override
    public String getMe() {
        return me;
    }

    public void setMe(String me) {
        this.me = me;
    }
}

要解决此问题,您需要在同一个类或接口中定义setter和getter。 使用JDK 8时也不会发生这种情况。

答案 1 :(得分:0)

不确定为什么,一切看起来都正确,但例外:

java.lang.NoSuchMethodException: Unable to determine write method for Field: 'id' in Class: class org.dtos.PPDTO

说它无法找到用于将ID值写入PPDTO类的方法。

可能是因为在源头上,这是你的类型:

class java.lang.Long

在目的地上只是

java.lang.Long中

不确定为什么会这样,但这可能是一个因素。您是否尝试将它们更改为Primitive long?

答案 2 :(得分:0)

我会检查serialVersionUID的源类和目标类。它们必须具有相同的版本/值才能被推土机正确序列化和反序列化。