Links

About

Smart Updates

It is important to understand how updates are done by MentaBean. There are 3 ways of doing it:

Attached Bean Update (the recommended one)

You load a bean so it is attached to the session, modify some of its properties and then call session.update(bean).

When you do that, MentaBean keeps track of the bean properties that were modified and performs an update on just the modified properties. So you can load a bean with 100 properties, modify a single one, perform an update and the only property that gets written to the database is the one that was modified, not all 100 properties of the bean. Below we have an example of performing an update on an attached bean:

User u = new User();
u.setUsername("saoj");
u = session.loadUnique(u); // the u bean is now loaded and ATTACHED to the session
u.setStatus(Status.PREMIUM);
session.update(u); // only the status column will be updated in the database because it was the only one modified
// The SQL generated might be: update Users set status = 3 where id = 123123;

De-attached Bean Update (may be useful for some scenarios)

You instantiate a bean (without loading it from the database), set some of its properties and then call session.update(bean).

When you do that, MentaBean will only update the bean properties it considers to be set. Below is the rule it follows to determine whether a property is set or not:

Primitives numbers (int, float, char, byte, etc.) with value 0 are considered NOT set

Boolean primitive with value false is considered NOT set

Any null object, including the primitive wrapper objects (java.lang.Integer, java.lang.Boolean, etc.), is considered NOT set

Everything else is considered to be set

Below is an example of a de-attached bean update:

User u = new User(34); // without a primary key no update can happen so the user id must be set or an exception is thrown
u.setStatus(Status.PREMIUM);
session.update(u); // only the status column will be updated in the database because it was the only one set
// Ex: update Users set status = 3 where id = 34;

You should note that the primary key (i.e. the user id) must be set otherwise the update will throw an exception. The obvious drawback of this approach is that you cannot update a column to null, because null is considered NOT set. You will have the same problem if you try to update a primitive boolean to false or a primitive number to 0.

All Properties Updates (not recommended unless you know what you are doing)

You instantiate a bean (without loading it from the database), set ALL its properties and then call session.updateAll(bean).

When you do this, MentaBean will generate an update with all properties of your bean. Keep in mind that even if you did not set a property (or if you set it to null) it will be updated, in other words, it will be included in the generated update SQL. This approach is only recommended if you really know what you are doing, because even if you forget to set a property by mistake it will be updated to null in the database. Note that to use this approach you have to call session.updateAll(bean) instead of session.update(bean).