SAP / olingo-jpa-processor-v4

The JPA Processor fills the gap between Olingo V4 and the database, by providing a mapping between JPA metadata and OData metadata, generating queries and supporting the entity manipulations.
Apache License 2.0
123 stars 76 forks source link

POST new entity with @ID set as @GeneratedValue(strategy = GenerationType.AUTO) fails #108

Open ajain72 opened 3 years ago

ajain72 commented 3 years ago

Hello, I have found this library pretty useful and works for most scenarios. Post/Create entity works as long as @Id isn't auto-generated but supplied by the ODATA consumer.

An entity with @Id and @GeneratedValue(strategy = GenerationType.AUTO) fails while creating the entity. The failure happens #56 of JPAExampleCUDRequestHandler in createEntity while trying to find the entity. Error is that ID is needed although it is expected to be generated by Hibernate. java.lang.IllegalArgumentException: id to load is required for loading

If consumer were to pass the ID, the error then happens while persisting the entity on line #65 of JPAExampleCUDRequestHandler javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: ..

In both cases error happens in Spring's ExtendedEntityManagerCreator while delegating the EM calls to hibernate.

Also the errors never get logged in in the logs, had to debug to get to the exceptions.

Everything works fine if I were to remove @GeneratedValue. There are not relationships in the entity btw.

ajain72 commented 3 years ago

I am more than willing to contribute but wanted to see if any one else has seen this issue. Regards!!!

kurobirds commented 3 years ago

@ajain72 I have the same issue with you. Do you know any way to create a new entity without an Id?

ajain72 commented 3 years ago

While we ended up not using this library, a very standard approach in non-sql dbs is to use UUIDs as the key.  You could do that in your relational database as well. Just default your id to a UUID when ever entity pojo is created, that could work. It has been a while when I was looked at the source here, it was indeed creating a new object, so defaulting your key to UUID should work.

Meini777 commented 2 years ago

I had the same Issue and I just registered my own JPACUDRequestHandler based on the JPAExampleCUDRequestHandler where you have full control about what has to be done on creates, updates and deletes.