3

I have got a problem with Ebean 2.7.3 integrated in the Play Framework 2.0.4.

Let me explain how it works in the framework first. When I want to create a new object I call a function, which creates new instance of model object and sends it to the web form. After a form is submitted, a save() function is called. This function binds values from the form and assigns them to another new instance of model object. Than it calls update() function on the object and everything is correctly written to the database. When I want to update an object, the way is almost the same with some little differences. At the beginning the object is loaded from the database, sent to the form, parsed back. The difference is, that I am also parsing the original primary key of updated object. Then, when I call update() function, everything is correctly written because ebean knows the primary id and so the correct object could be updated.

The problems begins, when I add an @Embedded property to the model. I am able to create new object, save it, read it but I cannot update it. Whe I try to update object using the same principle as I described, I run into the OptimisticLockException. When I inevstigated the SQL logs, I found out, that Ebean is doing update with all the model properties in the SET part of SQL and also with all the properties in the WHERE clause. I know that this is connected to the optimistic locking mechanism as described in the Ebean docs. The problem is that Ebean is using the same values in both parts (SET and WHERE). It seems that Ebean is unable to look up the original entity so it is unable to use the old values in the WHERE part. As a result to this, coresponding entity in the database is never found and never updated returning back an OptimistickLockException. I also know that this problem is usually solved with @Version annotation. So I created new property called updatedStamp and annotated it with @Version. Using this principle I run into the NullPointerException while calling update().

Caused by: java.lang.NullPointerException: null
com.avaje.ebeaninternal.server.core.PersistRequestBean.hasChanged(PersistRequestBean.java:728)

So my question is.. IS there a proper way of using @Embedded properties in the models ? I know that this has been discussed in previous versions of Ebean but it seems that no solution is available.

Well .. actually I found out one way how to do that. When I load an object from db and I KEEP this class instance, I am able to change its properties and call the update() without problems. But actually this is not a solution for me as the play framework is stateless so I am unable to keep the original object during the session. I must create a new one, fill it with values and save it. Without @Embedded this is working ok.

Any clues on how to solve that ?

ndeverge
  • 21,378
  • 4
  • 56
  • 85
spidla
  • 231
  • 1
  • 9
  • Here you will find solution to the problem: [http://stackoverflow.com/a/13109319/1780839][1] [1]: http://stackoverflow.com/a/13109319/1780839 – msnt Oct 28 '12 at 13:54
  • 4
    Unfortunately it is not working. Anyway I found the solution which works for me. I just added call to function `_ebean_setEmbeddedLoaded()` on Model class just before calling `update()`. Everything is now working flawlessly. – spidla Nov 04 '12 at 20:50
  • You saved my day! Where did you find out that solution? – reen Feb 19 '13 at 15:49
  • Please create a test case and open a github issue if you still have this problem. Thanks. – Rob Bygrave Apr 29 '14 at 08:57

0 Answers0