1

I have an entity called StockLevel and another one called Version and they are in a relationship like this:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "version", nullable = false, referencedColumnName = "pk")
private VersionModel version;

Now I want to create a new StockLevel via Spring-Data-REST so I issue the following POST request to http://somedomain.com:8111/storefront/rest/stock_level/

{
   "uid" : "mynewuidForStockLevel",
   "version" : 
   {
      "rel" : "version",
      "href":"http://localhost:8111/storefront/rest/version/3378719354003680"
   }
}

Here is my Version model:

@Cacheable 
@Entity(name = VersionModel.NAME) 
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) 
@Table(name = VersionModel.NAME, 
       uniqueConstraints = { @UniqueConstraint(columnNames = { "pk" }) }, 
       indexes = { @Index(columnList = "id")}) 
public class VersionModel extends AbstractEntityModel {
    public static final String NAME = "version";
} 

however I get a Null-pointer exception in Fasterxml Jackson:

Caused by: java.lang.NullPointerException
at java.net.URI$Parser.parse(URI.java:3023)
at java.net.URI.<init>(URI.java:595)
at java.net.URI.create(URI.java:857)
at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$UriStringDeserializer.deserialize(PersistentEntityJackson2Module.java:359)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)

What am I doing wrong?

JBCP
  • 13,109
  • 9
  • 73
  • 111
Petar Tahchiev
  • 4,336
  • 4
  • 35
  • 48
  • 1
    Shouldn't it be `"version": "http://localhost:8111/storefront/rest/version/3378719354003680"`? – beerbajay Jun 10 '14 at 12:17
  • How does your VersionModel entity look like? – cy3er Jun 10 '14 at 13:09
  • Here's the VersionModel: – Petar Tahchiev Jun 10 '14 at 15:07
  • @Cacheable @Entity(name = VersionModel.NAME) @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @Table(name = VersionModel.NAME, uniqueConstraints = { @UniqueConstraint(columnNames = { "pk" }) }, indexes = { @Index(columnList = "id")}) public class VersionModel extends AbstractEntityModel { public static final String NAME = "version"; } – Petar Tahchiev Jun 10 '14 at 15:08
  • This website is such a SHIT!!!!! I cannot post code in the reply - I added 4 spaces and it still doesn't show as code. – Petar Tahchiev Jun 10 '14 at 15:10
  • @beerbajay It's a relation, not a `String` property! – Petar Tahchiev Jun 10 '14 at 15:11
  • @PetarTahchiev - the best thing to do here would be to update the main question with the new code, and comment about it here. That also makes the Question/Answer format of the website easier to read for future users, and prevents the site from turning into a forum. – JBCP Dec 31 '14 at 18:14

1 Answers1

3

If you want to create an association to the related resource directly when creating a resource, the value submitted for the relation has to be a URI directly, not an object of some sorts. The sample you showed will work if you adapt it like this:

{ "uid" : "mynewuidForStockLevel",
  "version" : "http://localhost:8111/storefront/rest/version/3378719354003680" }
Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211