0

I've created a simple DB in MYSQL:

Database Model

I've also created the JPA entities based on this and exposed it using Olingo v2. When I try to create a client entity through postman it works fine

{
"Name": "John Doe"
}

however when I try to create an address

{
    "Street": "Street for John Doe",
    "Zipcode": "444465",
    "Client": 1
 }

it fails with

MySQLIntegrityConstraintViolationException: Column 'clientId' cannot be null

This error is expected since I made it not nullable

However I was assuming that "Client": 1 would be properly processed and transformed into the query:

ClientId Column is available in the Address entity as a join column:

//bi-directional many-to-one association to Client
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="clientId")
private Client client;

I've tried changing the JSON format to create a sub object Client within Address like this:

    {
    "Street": "Street for John Doe",
    "Zipcode": "444465",
    "Client": { "Id": 3 }
   }

but this ends up with a 400 - "The Request Body is malformed"

Am I mising some annotation on the Client property or something else?

Note: If I remove the join column and create a column clientId the insert/update works fine... however in this situation I end up loosing the navigation capabilitites of the Odata Model

Thanks for the feedback,

Regards

Pedro

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    How are you reading this JSON and converting it to JPA entities? Does the Address have an associated Client after you've parsed the JSON? There isn't enough information to know what you are doing or how it is handled. – Chris Jul 26 '16 at 19:55
  • @Chris, I'm not manipulating the JSON at all. I've simply override method initializeODataJPAContext() of class ODataJPAServiceFactory and was expecting the default methods to be able to handle this. – Pedro Barbosa Jul 27 '16 at 08:06
  • I don't know anything about OLingo so maybe someone else can help, but something is responsible for creating Java objects from the JSON and then persisting those java objects in the context as entities. Check the logic there, that the java objects built from the JSON are as you would expect, as I don't see how it can know "client" : 1 requires it to lookup a Client entity in a JPA context. – Chris Jul 27 '16 at 14:50

0 Answers0