Code Example - Optimistic Concurrency Checking
For "ALL COLUMN" Optimistic Concurrency Checking the "Old Values" are required to build and
bind to the WHERE clause of the UPDATE or DELETE statement.
This is some code that SIMULATES they way this typically occurs with Ebean.
You would ONLY use similar code if you wanted to simulate an Update or Delete without
fetching data from the database. Otherwise this is not realistic code but hopefully
gives some insight into how this works.
Note: EntityBeanIntercept intercepts the 'getters' and 'setters' for detecting Lazy Loading
and intercepts 'setters' for creating the 'OldValues'. Those are its two main jobs.
package unittest.ex;
import app.data.Topic;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.bean.EntityBeanIntercept;
public class OldValues {
public static void main(String[] args) {
// SIMULATE A FETCH AND UPDATE
// 1. WE CREATE A BEAN
EbeanServer server = Ebean.getServer(null);
// this instance is a 'byte code generated' EntityBean
Topic topic = (Topic)server.createEntityBean(Topic.class);
// it implements EntityBean and has a EntityBeanIntercept
EntityBean entityBean = (EntityBean)topic;
EntityBeanIntercept ebi = entityBean.getEbeanIntercept();
// className is app.data.Topic$$EntityBean
String className = topic.getClass().getName();
System.out.println("className ["+className+"]");
// 2. POPULATE BEAN FROM RESULTSET
// the original values are typically read from
// a jdbc resultset and set into the bean
topic.setTitle("Original Title");
topic.setBody("Original Body text");
topic.setEditorNote("Editors Note");
// when all the properties are loaded into the bean
// we tell EntityBeanIntercept that it can start
// intercepting 'setter' methods to build the
// oldValues if the bean is being changed
ebi.setLoaded(true);
// 3. GIVE THE BEAN TO THE APPLICATION
// 4. APPLICATION MODIFIES THE BEAN
// modifying the bean the EntityBeanIntercept
// intercepts the 'setter' methods and detects the
// bean is being changed. Before the change it
// creates an 'oldValues' Bean with all the
// original values before they are modified
topic.setTitle("New Title");
topic.setBody("New Body");
// 5. APPLICATION SAVES BEAN - UPDATE
// If Ebean is performing an update and needs to use
// "ALL COLUMN" Optimistic Concurrency Checking then
// it can get the original values from the
// 'oldValues' Bean it created
Topic oldValues = (Topic)ebi.getOldValues();
// Get the 'New/Current' values
String newTitle = topic.getTitle();
String newBody = topic.getBody();
String newNote = topic.getEditorNote();
// Get the 'Old/Original' values
String oldTitle = oldValues.getTitle();
String oldBody = oldValues.getBody();
String oldNote = oldValues.getEditorNote();
// 6. USE NEW AND OLD TO BUILD THE UDPATE STATEMENT
// Instead well just print out the info
System.out.println("newTitle ["+newTitle+"]");
System.out.println("newBody ["+newBody+"]");
System.out.println("newNote ["+newNote+"]");
System.out.println("oldTitle ["+oldTitle+"]");
System.out.println("oldBody ["+oldBody+"]");
System.out.println("oldNote ["+oldNote+"]");
}
}
The Output from running this bit of code is:
className [app.data.Topic$$EntityBean]
newTitle [New Title]
newBody [New Body]
newNote [Editors Note]
oldTitle [Original Title]
oldBody [Original Body text]
oldNote [Editors Note]
|