#641 - ENH: Add EbeanServer.setBeanId(bean, id) ... to make it easy to convert id value to appropriate type and set

This commit is contained in:
Robin Bygrave
2016-04-08 01:15:34 +12:00
parent 649c14644e
commit 524be4ce55
4 changed files with 58 additions and 4 deletions
@@ -146,6 +146,17 @@ public interface EbeanServer {
*/
Object getBeanId(Object bean);
/**
* Set the Id value onto the bean converting the type of the id value if necessary.
* <p>
* For example, if the id value passed in is a String but ought to be a Long or UUID etc
* then it will automatically be converted.
* </p>
* @param bean The entity bean to set the id value on.
* @param id The id value to set.
*/
Object setBeanId(Object bean, Object id);
/**
* Return a map of the differences between two objects of the same type.
* <p>
@@ -1957,14 +1957,23 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
return typeInfo != null && getBeanDescriptor(typeInfo.getBeanType()) != null;
}
@Override
public Object setBeanId(Object bean, Object id) {
EntityBean eb = checkEntityBean(bean);
BeanDescriptor<?> desc = getBeanDescriptor(bean.getClass());
if (desc == null) {
throw new PersistenceException(bean.getClass().getName() + " is NOT an Entity Bean registered with this server?");
}
return desc.convertSetId(id, eb);
}
@Override
public Object getBeanId(Object bean) {
EntityBean eb = checkEntityBean(bean);
BeanDescriptor<?> desc = getBeanDescriptor(bean.getClass());
if (desc == null) {
String m = bean.getClass().getName() + " is NOT an Entity Bean registered with this server?";
throw new PersistenceException(m);
throw new PersistenceException(bean.getClass().getName() + " is NOT an Entity Bean registered with this server?");
}
return desc.getId(eb);
}