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 ?