It is important to understand how updates are done by MentaBean. There are 3 ways of doing it:
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;
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:
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.
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).