mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1842 - Refactor persisting to delay the setting of generated Id value (sequences and deferred imported ids)
This commit is contained in:
@@ -1262,6 +1262,7 @@ public final class PersistRequestBean<T> extends PersistRequest implements BeanP
|
||||
}
|
||||
|
||||
private void executeInsert() {
|
||||
setGeneratedId();
|
||||
setTenantId();
|
||||
if (controller == null || controller.preInsert(this)) {
|
||||
beanManager.getBeanPersister().insert(this);
|
||||
@@ -1469,4 +1470,8 @@ public final class PersistRequestBean<T> extends PersistRequest implements BeanP
|
||||
public void setSaveRecurse() {
|
||||
saveRecurse = true;
|
||||
}
|
||||
|
||||
public void setGeneratedId() {
|
||||
beanDescriptor.setGeneratedId(entityBean, transaction);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3107,6 +3107,18 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
|
||||
return idType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the generated Id value if appropriate.
|
||||
*/
|
||||
public void setGeneratedId(EntityBean entityBean, Transaction transaction) {
|
||||
if (idGenerator == null || idProperty == null || idProperty.isEmbedded()) {
|
||||
return;
|
||||
}
|
||||
if (isNullOrZero(idProperty.getValue(entityBean))) {
|
||||
convertSetId(nextId(transaction), entityBean);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the Id value is marked as a <code>@GeneratedValue</code>.
|
||||
*/
|
||||
|
||||
@@ -506,11 +506,7 @@ public final class DefaultPersister implements Persister {
|
||||
// save associated One beans recursively first
|
||||
saveAssocOne(request);
|
||||
}
|
||||
|
||||
// set the IDGenerated value if required
|
||||
setIdGenValue(request);
|
||||
request.executeOrQueue();
|
||||
|
||||
if (request.isPersistCascade()) {
|
||||
// save any associated List held beans
|
||||
saveAssocMany(request);
|
||||
@@ -1197,33 +1193,6 @@ public final class DefaultPersister implements Persister {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Id Generated value for insert.
|
||||
*/
|
||||
private void setIdGenValue(PersistRequestBean<?> request) {
|
||||
|
||||
BeanDescriptor<?> desc = request.getBeanDescriptor();
|
||||
if (!desc.isUseIdGenerator()) {
|
||||
return;
|
||||
}
|
||||
|
||||
BeanProperty idProp = desc.getIdProperty();
|
||||
if (idProp == null || idProp.isEmbedded()) {
|
||||
// not supporting IdGeneration for concatenated or Embedded
|
||||
return;
|
||||
}
|
||||
|
||||
EntityBean bean = request.getEntityBean();
|
||||
Object uid = idProp.getValue(bean);
|
||||
|
||||
if (isNullOrZero(uid)) {
|
||||
// generate the nextId and set it to the property
|
||||
Object nextId = desc.nextId(request.getTransaction());
|
||||
// cast the data type if required and set it
|
||||
desc.convertSetId(nextId, bean);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the Persist Request Object that wraps all the objects used to
|
||||
* perform an insert, update or delete.
|
||||
|
||||
Reference in New Issue
Block a user