4.6.2: Problem with Saving an Entity, Adding child entities and saving again.

1,216 views
Skip to first unread message

kraythe

unread,
Jun 3, 2015, 6:01:17 PM6/3/15
to eb...@googlegroups.com
Greetings, 

I am having problems with a scenario in which I save an entity and then add some child entities and save again. The first save works but the second save does not run insert statements for the child entities and then throws an optimistic lock exception. Naturally I cant give out my company's code but essentially it looks like this: 


@Entity
@Table(name = "parent")
public class Parent {
 
@Id
 
@Column(name = "id")
 
int id;

 
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
 
List<Child> children;

 
public List<Child> getChildren() {
   
return children;
 
}

 
public void setChildren(final List<Child> children) {
   
this.children = children;
 
}

 
public int getId() {
   
return id;
 
}
}

public class Child {
 
@Id
 
@Column(name = "id")
 
int id;

 
@ManyToOne
 
@Column(name = "parent_id")
 
public Parent parent;

 
public int getId() {
   
return id;
 
}

 
public Parent getParent() {
   
return parent;
 
}

 
public void setParent(final Parent parent) {
   
this.parent = parent;
 
}
}

public void testIt() {
 
try {
   
Ebean.beginTransaction();
   
Parent parent = new Parent();
   parent
.children.add(new Child());
   
Ebean.save(parent); // <--- Works Fine
   parent
.children.add(new Child());
   parent
.children.add(new Child());
   
Ebean.save(parent); <-- Optimistic Lock exception
   
Ebean.commitTransaction();
 
} catch (final Exception ex) {
   
Ebean.rollbackTransaction();
 
}
}

As you can see from the mock example, first we create the parent and a single child. Then we save the parent which cascades to the children. I can see through SQL logging that the children were saved to the database with "insert" calls. Next we add two more children. However when we go to save this time there is an optimistic lock exception. The SQL lock shows no inserts for the new children and the records are not saved to the system at all, however the system does run an update call for the brand new child record and that will, of course fail. I can look into the database and see that the new records have not been added. 

So what am I doing wrong here ? Any ideas what might be causing this? 

Version Information: 
  • Play 2.4.x
  • SBT: org.avaje.ebeanorm:avaje-ebeanorm:4.6.2:jar
  • SBT: org.avaje.ebeanorm:avaje-ebeanorm-agent:4.5.3:jar 

Rob Bygrave

unread,
Jun 3, 2015, 6:21:48 PM6/3/15
to ebean@googlegroups
however the system does run an update call for the brand new child record and that will, of course fail.

So that is the problem (that update should be an insert). There is no obvious problem with the above code (except outside of Play we need parent.getChildren() rather the parent.children).  Are you able to reproduce the error with a small test case like the one above?


Thanks, Rob.


--

---
You received this message because you are subscribed to the Google Groups "Ebean ORM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ebean+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kraythe

unread,
Jun 3, 2015, 6:34:05 PM6/3/15
to eb...@googlegroups.com
I can try. I can whip up a test application in Play to do this. Give me a few min.  Or do you need it in some other context outside of play for example ? 

-- Robert

Rob Bygrave

unread,
Jun 3, 2015, 6:39:25 PM6/3/15
to ebean@googlegroups
Yes, I'd like the failing test case outside of Play.

Cheers, Rob.

kraythe

unread,
Jun 3, 2015, 6:50:23 PM6/3/15
to eb...@googlegroups.com
Interestingly i have never used ebeans outside of play. So that will take a bit more time. :) 

Rob Bygrave

unread,
Jun 3, 2015, 7:34:36 PM6/3/15
to ebean@googlegroups

kraythe

unread,
Jun 4, 2015, 1:54:34 AM6/4/15
to eb...@googlegroups.com
I have created a sample project, attached. Actually there is a problem where it seems that Ebeans is generating bad DDL for the Scala entities? I couldn't figure out why this was happening. Our model is a mix of Java and Scala entities and they worked just fine under Play 2.3 and the Ebeans version therein. So is Scala support in this version basically just completely broken ? Both java and scala use cases are in there. I cant get the Java One to replicate the previous error I reported when running standalone. As for the scala parts, they dont even run in the standalone project. But in our main codebase they mostly work except for the child update issue.  

Thanks in advance. 
ebean-demo.zip

Rob Bygrave

unread,
Jun 4, 2015, 1:59:44 AM6/4/15
to ebean@googlegroups
 I cant get the Java One to replicate the previous error I reported when running standalone. 

So there is a failing test case here?

kraythe

unread,
Jun 4, 2015, 11:57:49 AM6/4/15
to eb...@googlegroups.com
Ok rob. I finished this at a stupid hour of the morning here and I didnt realize that the classes "Outer" and "Inner" are reserved words in SQL and the DDL generator wasn't escaping them with back ticks. I have changed the names to Container and Element and attached the new version.

 If you do the following command you will see the OptimisticLockException: 

mvn install exec:java 

The java verson works. The scala version throws an OLE. 

Something I am doing wrong here or an issue with Ebeans? 

-- Robert
ebean-demo.zip

kraythe

unread,
Jun 5, 2015, 11:48:55 AM6/5/15
to eb...@googlegroups.com
Hey Rob. Were you able to run the example? Any idea what the problem is Is there a workaround I can employ? 

Mick Giles

unread,
Jun 16, 2015, 2:21:40 PM6/16/15
to eb...@googlegroups.com
I am experiencing the exact same problem. This affects all of my scala models and is a major blocker for me. Is there any hope of a fix or a workaround?

Rob Bygrave

unread,
Jun 16, 2015, 5:15:38 PM6/16/15
to ebean@googlegroups
Hi,

I have not got around to looking into this yet.  I have a few things on my list ahead of this (2 bugs, organising a new job etc).

Can you elaborate on why this is a blocker for you?  I viewed it as a 'good thing to look at shortly' rather than 'this is blocking my development because ...' type issue.


Cheers, Rob.


kraythe

unread,
Jun 16, 2015, 5:30:20 PM6/16/15
to eb...@googlegroups.com
Speaking for myself it blocks me because I want to move to Play 2.4 and the most recent version of Ebeans. Right now I am on the version of Ebeans bundled with play 2.3 and it DOES work properly. So between then and the most recent version of Ebeans this got broken. Given that I have DOZENS of models in scala and it would be a major development project to convert them to java (a project Id have trouble selling much less liking :) ). I tried figuring it out myself but I dont know the code or architecture and have limited bandwidth.

Thanks a bunch Rob. 

-- Robert

Mick Giles

unread,
Jun 16, 2015, 5:32:22 PM6/16/15
to eb...@googlegroups.com
Most of our scala models are experiencing this issue with version 4.6.2 and the framework we are using requires version 4.6.2 which has us unable to move forward with our new feature set as those features depend on framework features that depend on ebeans 4.6.2 which does not handle scala models properly. 
Without a fix or workaround our options are to rewrite all of our models in Java or replace our underlying ORM. Until one of those we are unable to move forward with our key features.

Rob Bygrave

unread,
Jun 16, 2015, 5:35:59 PM6/16/15
to ebean@googlegroups
Thanks Robert, Mick.  Yes can understand the impact makes it a blocker ... I'll try to look at it in the next 2 days.

Cheers, Rob.

Rob Bygrave

unread,
Jun 17, 2015, 7:30:04 AM6/17/15
to ebean@googlegroups
Ok, so I'm looking at the example application - great work, nice and chopped down and perfect for diagnosing issues like this.

Now with regards to this issue I did reproduce ...  so the Java example worked as expected and the scala example threw the OptimisticLockingException.

The 'issue' is that with the Scala "Element" on construction it is setting it's id to 0 and parent to null (when compared with the Java "Child" bean which does not).  In Ebean internals ... BeanDescriptor.isInsertMode() is doing a check for hasIdProperty() and with "Element" that is returning true ... and so it tries to do an update rather than an insert as we would hope. When the update executes and updates 0 rows that is interpreted as an OptimisticLockingException.

The fix is to treat Id's with a value of 0 as being not set.  Now interestingly enough Ebean does do that effectively for 'top level' beans but this bug is that it was not doing that when it cascaded down a level.  So for example, I could reproduce by changing the java "Child" bean to have ... int id = 0; 

Anyway, logged as https://github.com/ebean-orm/avaje-ebeanorm/issues/310 ... adding the test case and fix.

Rolling out release 4.7.1 now which contains this fix (so in maven central in an hour or so).


Cheers, Rob.


Side notes:

- Element.outer had an @Column when that should really be a @JoinColumn I believe.
- The logging was not including the DML statements or Transaction Begin, Commit, Rollback. If you are not seeing that in your logging you are missing out big time (the actual SQL with bind variables and transaction demarcation).
- There is an enhancement plugin for IDEA and Eclipse IDE.  With those turned on it is much easier to run/debug main methods


kraythe

unread,
Jun 17, 2015, 11:12:49 AM6/17/15
to eb...@googlegroups.com
Thats awesome Rob. Makes sense. Now if we can get that rolled into play 2.4 it will be really great. Let me know when you have the release out and I will talk with the play people to update the plugin. Or, do you know if i can independently update the version of ebeans in play 2.4 without the intervention of the play folks? 

-- Robert

Rob Bygrave

unread,
Jun 17, 2015, 5:27:45 PM6/17/15
to ebean@googlegroups
you have the release out 

That went out last night.  It can take 1 hr or so to get into maven central but yes, it is in now - http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22avaje-ebeanorm%22


Or, do you know if i can independently update the version of ebeans in play 2.4 without the intervention of the play folks? 

I don't know how that works yet no. 


Cheers, Rob.

Reply all
Reply to author
Forward
0 new messages