mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
#668 - ENH: Add ability to register a custom IdGenerator
This commit is contained in:
1 parent
036092d470
commit
4e9e11ac34
15 files changed
+371
-129
No files matched your search
@@ -153,8 +153,6 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
|
||||
|
||||
private final DatabasePlatform databasePlatform;
|
||||
|
||||
private final UuidIdGenerator uuidIdGenerator = new UuidIdGenerator();
|
||||
|
||||
private final ServerCacheManager cacheManager;
|
||||
|
||||
private final BackgroundExecutor backgroundExecutor;
|
||||
@@ -1177,6 +1175,10 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
|
||||
*/
|
||||
private <T> void setIdGeneration(DeployBeanDescriptor<T> desc) {
|
||||
|
||||
if (desc.getIdGenerator() != null) {
|
||||
// already assigned (So custom or UUID)
|
||||
return;
|
||||
}
|
||||
if (desc.propertiesId().size() == 0) {
|
||||
// bean doesn't have an Id property
|
||||
if (desc.isBaseTableType() && desc.getBeanFinder() == null) {
|
||||
@@ -1209,14 +1211,6 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
|
||||
desc.setIdTypePlatformDefault();
|
||||
}
|
||||
|
||||
if (IdType.GENERATOR.equals(desc.getIdType())) {
|
||||
String genName = desc.getIdGeneratorName();
|
||||
if (UuidIdGenerator.AUTO_UUID.equals(genName)) {
|
||||
desc.setIdGenerator(uuidIdGenerator);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (desc.getBaseTable() == null) {
|
||||
// no base table so not going to set Identity
|
||||
// of sequence information
|
||||
@@ -1240,8 +1234,7 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
|
||||
}
|
||||
|
||||
// create the sequence based IdGenerator
|
||||
PlatformIdGenerator seqIdGen = createSequenceIdGenerator(seqName);
|
||||
desc.setIdGenerator(seqIdGen);
|
||||
desc.setIdGenerator(createSequenceIdGenerator(seqName));
|
||||
}
|
||||
|
||||
private PlatformIdGenerator createSequenceIdGenerator(String seqName) {
|
||||
|
||||
+53
-1
@@ -1,12 +1,18 @@
|
||||
package com.avaje.ebeaninternal.server.deploy.generatedproperty;
|
||||
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.ebean.config.ClassLoadConfig;
|
||||
import com.avaje.ebean.config.CurrentUserProvider;
|
||||
import com.avaje.ebean.config.IdGenerator;
|
||||
import com.avaje.ebean.config.ServerConfig;
|
||||
import com.avaje.ebean.config.dbplatform.PlatformIdGenerator;
|
||||
import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanProperty;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Default implementation of GeneratedPropertyFactory.
|
||||
@@ -27,7 +33,9 @@ public class GeneratedPropertyFactory {
|
||||
|
||||
private final ClassLoadConfig classLoadConfig;
|
||||
|
||||
public GeneratedPropertyFactory(ServerConfig serverConfig) {
|
||||
private final Map<String,PlatformIdGenerator> idGeneratorMap = new HashMap<String,PlatformIdGenerator>();
|
||||
|
||||
public GeneratedPropertyFactory(ServerConfig serverConfig, List<IdGenerator> idGenerators) {
|
||||
|
||||
this.classLoadConfig = serverConfig.getClassLoadConfig();
|
||||
this.insertFactory = new InsertTimestampFactory(classLoadConfig);
|
||||
@@ -51,6 +59,12 @@ public class GeneratedPropertyFactory {
|
||||
numberTypes.add(Double.class.getName());
|
||||
numberTypes.add(double.class.getName());
|
||||
numberTypes.add(BigDecimal.class.getName());
|
||||
|
||||
if (idGenerators != null) {
|
||||
for (IdGenerator idGenerator : idGenerators) {
|
||||
idGeneratorMap.put(idGenerator.getName(), new CustomIdGenerator(idGenerator));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ClassLoadConfig getClassLoadConfig() {
|
||||
@@ -98,4 +112,42 @@ public class GeneratedPropertyFactory {
|
||||
property.setGeneratedProperty(generatedWhoModified);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the named custom IdGenerator (wrapped as a PlatformIdGenerator).
|
||||
*/
|
||||
public PlatformIdGenerator getIdGenerator(String generatorName) {
|
||||
return idGeneratorMap.get(generatorName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps the custom IdGenerator to implement PlatformIdGenerator.
|
||||
*/
|
||||
private static class CustomIdGenerator implements PlatformIdGenerator {
|
||||
|
||||
private final IdGenerator generator;
|
||||
|
||||
CustomIdGenerator(IdGenerator generator) {
|
||||
this.generator = generator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return generator.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDbSequence() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object nextId(Transaction transaction) {
|
||||
return generator.nextValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preAllocateIds(int allocateSize) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import com.avaje.ebeaninternal.server.deploy.DeployNamedQuery;
|
||||
import com.avaje.ebeaninternal.server.deploy.DeployNamedUpdate;
|
||||
import com.avaje.ebeaninternal.server.deploy.InheritInfo;
|
||||
import com.avaje.ebeaninternal.server.deploy.parse.DeployBeanInfo;
|
||||
import com.avaje.ebeaninternal.server.idgen.UuidIdGenerator;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
@@ -830,6 +831,24 @@ public class DeployBeanDescriptor<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign the standard UUID generator.
|
||||
*/
|
||||
public void setUuidGenerator() {
|
||||
this.idType = IdType.EXTERNAL;
|
||||
this.idGeneratorName = UuidIdGenerator.AUTO_UUID;
|
||||
this.idGenerator = UuidIdGenerator.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign a custom external IdGenerator.
|
||||
*/
|
||||
public void setCustomIdGenerator(PlatformIdGenerator idGenerator) {
|
||||
this.idType = IdType.EXTERNAL;
|
||||
this.idGeneratorName = idGenerator.getName();
|
||||
this.idGenerator = idGenerator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Summary description.
|
||||
*/
|
||||
|
||||
@@ -6,12 +6,12 @@ import com.avaje.ebean.config.EncryptDeploy.Mode;
|
||||
import com.avaje.ebean.config.dbplatform.DbEncrypt;
|
||||
import com.avaje.ebean.config.dbplatform.DbEncryptFunction;
|
||||
import com.avaje.ebean.config.dbplatform.IdType;
|
||||
import com.avaje.ebean.config.dbplatform.PlatformIdGenerator;
|
||||
import com.avaje.ebeaninternal.server.deploy.generatedproperty.GeneratedPropertyFactory;
|
||||
import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanProperty;
|
||||
import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanPropertyAssoc;
|
||||
import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanPropertyAssocOne;
|
||||
import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanPropertyCompound;
|
||||
import com.avaje.ebeaninternal.server.idgen.UuidIdGenerator;
|
||||
import com.avaje.ebeaninternal.server.lib.util.StringHelper;
|
||||
import com.avaje.ebeaninternal.server.type.CtCompoundType;
|
||||
import com.avaje.ebeaninternal.server.type.DataEncryptSupport;
|
||||
@@ -419,12 +419,8 @@ public class AnnotationFields extends AnnotationParser {
|
||||
prop.setNullable(false);
|
||||
|
||||
if (prop.getPropertyType().equals(UUID.class)) {
|
||||
// An Id of type UUID
|
||||
if (descriptor.getIdGeneratorName() == null) {
|
||||
// Without a generator explicitly specified
|
||||
// so will use the default one AUTO_UUID
|
||||
descriptor.setIdGeneratorName(UuidIdGenerator.AUTO_UUID);
|
||||
descriptor.setIdType(IdType.GENERATOR);
|
||||
descriptor.setUuidGenerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -449,14 +445,20 @@ public class AnnotationFields extends AnnotationParser {
|
||||
|
||||
} else if (strategy == GenerationType.SEQUENCE) {
|
||||
descriptor.setIdType(IdType.SEQUENCE);
|
||||
if (genName != null && genName.length() > 0) {
|
||||
if (!genName.equals("")) {
|
||||
descriptor.setIdGeneratorName(genName);
|
||||
}
|
||||
|
||||
} else if (strategy == GenerationType.AUTO) {
|
||||
if (prop.getPropertyType().equals(UUID.class)) {
|
||||
descriptor.setIdGeneratorName(UuidIdGenerator.AUTO_UUID);
|
||||
descriptor.setIdType(IdType.GENERATOR);
|
||||
if (!genName.equals("")) {
|
||||
// use a custom IdGenerator
|
||||
PlatformIdGenerator idGenerator = generatedPropFactory.getIdGenerator(genName);
|
||||
if (idGenerator == null) {
|
||||
throw new IllegalStateException("No custom IdGenerator registered with name "+genName);
|
||||
}
|
||||
descriptor.setCustomIdGenerator(idGenerator);
|
||||
} else if (prop.getPropertyType().equals(UUID.class)) {
|
||||
descriptor.setUuidGenerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user