1

When I have entities having following -

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "question", nullable = false, referencedColumnName="id")

I get below error. The error goes away however after making only @ManyToOne associations as EAGER, leaving bidirectional @OneToMany as LAZY.

How to make this work with LAZY @ManyToOne with Spring DATA REST

java.lang.IllegalArgumentException: Id must be assignable to Serializable! Object of class [null] must be an instance of interface java.io.Serializable
at org.springframework.util.Assert.isInstanceOf(Assert.java:339)
at org.springframework.data.rest.webmvc.support.RepositoryEntityLinks.linkToSingleResource(RepositoryEntityLinks.java:160)
at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.getSelfLinkFor(PersistentEntityResourceAssembler.java:203)
at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.wrap(PersistentEntityResourceAssembler.java:107)
at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:84)
at org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController$1.apply(RepositoryPropertyReferenceController.java:141)
at org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController$1.apply(RepositoryPropertyReferenceController.java:110)
at org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.doWithReferencedProperty(RepositoryPropertyReferenceController.java:464)
at org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.followPropertyReference(RepositoryPropertyReferenceController.java:148)
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:483)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:769)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1125)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1059)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:110)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:497)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:248)
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:610)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:539)
at java.lang.Thread.run(Thread.java:745)

Error on URL : /api/users/2/question

{
  "cause" : null,
  "message" : "Id must be assignable to Serializable! Object of class [null] must be an instance of interface java.io.Serializable"
}
Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
fortm
  • 4,066
  • 5
  • 49
  • 79

2 Answers2

1

Issue now seems to be solved in 1.8 M1 release. As mentioned in JIRA,

"Be sure to add to add Jackson Hibernate 4 module to the classpath to let Jackson render proxies appropriately."

slavogiez
  • 146
  • 1
  • 7
  • 1
    I used above 1.8M1 of Spring data JPA with ackson-datatype-hibernate4 2.4.4. and spring-data-rest-webmvc 2.3.0.M1 without success. Though some URL were working but not all and here is related question. I found some ManyToOne sides giving content:null. Their ResourceProcessor were being called but no query could be seen in log for related resource. I finally moved to using @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) in entities at OneToMany Side with @AccessType(Type.PROPERTY) – fortm Dec 28 '14 at 16:05
  • http://stackoverflow.com/questions/27647672/blank-screen-even-though-log-has-results-for-manytoone-lazy-associated-resource – fortm Dec 28 '14 at 16:05
  • is this in spring boot 1.3.RELEASE or have to add an entry for spring data jpa dependency – Gaurav Rawat Jan 19 '16 at 14:50
0

Fetch in @ManyToOne annotation is optional so why to care about this. @ManyToOne annotation defines a single-valued association to another entity class that has many-to-one multiplicity. So try following scenario.

@ManyToOne
@JoinColumn(name="childTable_id", nullable = false, referencedColumnName="id")

If you want to work that with lazy initialization so Try this,

@ManyToOne(fetch=FetchType.Lazy)
@JoinColumn(name="childTable_id", nullable = false, referencedColumnName="id")
Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
  • This did not help and I already had above JoinColumn in place with referencedColumnName and I get same error, changing from LAZY to EAGER has this side effect that a Join will be done even when it might not be necessary – fortm Oct 13 '14 at 05:30