If you have a self-created object and save () is called, Ebean does the insert statement as SQL. If you call update (), it will do the update statement as SQL.
There is no logic in Ebean that would make an additional query to the database only to check if the database has a row with the same primary key, and if so, then use the update statement, in other words, the insert statement. This would be simply inefficient behavior, for you will usually know which one to do, and therefore do not want to execute two SQL statements, only one. In this case, you really need to tell him to use the update, and not try to insert the row again.
// Create user. User user1 = new User(); user1.setId(1L); user1.setVersion(1L); user1.setUserName("foo"); user1.setWorkPlace("bar"); user1.setEmail(" foo@bar.com "); user1.save(); // Change details later and update DB. User user2 = new User(); user2.setId(1L); user2.setVersion(1L); user2.setUserName("scooby_doo"); user2.save(); // This will crash. user2.update(); // This will work.
However, if you select an object from the database, then calling save () on it will use the update statement in SQL, because Ebean knows that its state is not new, but not existing.
// Find user (and let Ebean know its state as an existing row). User user3 = User.find.byId(1L); // Now you can change details and save without issues. user3.setEmail(" foo@gmail.com "); user3.setVersion(2L); user3.save(); // This will work.
See the Ebean Documentation .
source share