Is this a "stateless update"?
If the numberConf instance has come from a query then it's a "normal update" but if the numberConf instance was just "new'ed up" then it is a "stateless update".
There is a difference on how to handle this depending on if it's a stateless update or normal update.
For the "stateless update" case we need to get the BeanState and set the "loaded state" for the property that is being changed via reflection. That is, for "stateless update" it is the "loaded" properties that are included in the update. [For a normal update it is the "changed" properties that are included in the update].
In the example code below, it is a stateless update and the property being reflectively changed is "email" [and for the above it is "nextProductNbr"+type].
@Test
void statelessUpdate_via_reflection() throws NoSuchFieldException, IllegalAccessException {
// populate db with a user
User seed = new User();
seed.setName("someName");
seed.setEmail("so...@junk.com");
seed.save();
Field field = User.class.getDeclaredField("email");
field.setAccessible(true);
// our bean to perform stateless update
User user = new User();
user.setId(seed.getId());
user.setName("mod");
field.set(user, "cha...@junk.com");
// need to set property loaded state to include in the stateless update
BeanState beanState = DB.beanState(user);
beanState.setPropertyLoaded("email", true);
LoggedSql.start();
user.update();
List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("update c_user set name=?, email=?, when_modified=? where id=?");
}
--
---
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ebean/fe6741da-0b91-4072-83e0-61a4e70e5741n%40googlegroups.com.
@Test
void normalUpdate_via_reflection() throws NoSuchFieldException, IllegalAccessException {
// populate db with a user
User seed = new User();
seed.setName("someName");
seed.setEmail("so...@junk.com");
seed.save();
Field field = User.class.getDeclaredField("email");
field.setAccessible(true);
// fetching the bean from database, so a "normal update"
User user = DB.find(User.class, seed.getId());
user.setName("mod");
// ideally we get the old value first (if there are change listeners etc)
Object oldValue = field.get(user);
// reflectively modify
field.set(user, "cha...@junk.com");
// BeanState beanState = DB.beanState(user);
// need to use EntityBeanIntercept rather than BeanState
// so this isn't great !!
EntityBean eb = (EntityBean) user;
EntityBeanIntercept ebi = eb._ebean_getIntercept();
int pos = ebi.findProperty("email");
// EITHER ideally mark as dirty with the original value
//ebi.setChangedPropertyValue(pos, true, oldValue);
// OR just mark as changed
ebi.markPropertyAsChanged(pos);
LoggedSql.start();
user.update();
List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("update c_user set name=?, email=?, version=?, when_modified=? where id=? and version=?");
}