Search in sources :

Example 1 with TableGeneratorMetaData

use of org.datanucleus.metadata.TableGeneratorMetaData in project datanucleus-core by datanucleus.

the class AbstractStoreManager method getValueGeneratorForMember.

protected synchronized ValueGenerator getValueGeneratorForMember(ClassLoaderResolver clr, AbstractClassMetaData cmd, int absoluteFieldNumber) {
    String memberKey = valueGenerationMgr.getMemberKey(cmd, absoluteFieldNumber);
    // Check if we have a ValueGenerator already created for this member
    ValueGenerator generator = valueGenerationMgr.getValueGeneratorForMemberKey(memberKey);
    if (generator != null) {
        // Return the ValueGenerator already registered against this member "key"
        return generator;
    }
    // No ValueGenerator registered for this memberKey, so need to determine which to use and create it as required.
    String fieldName = null;
    ValueGenerationStrategy strategy = null;
    String sequence = null;
    String valueGeneratorName = null;
    if (absoluteFieldNumber >= 0) {
        // real field
        AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(absoluteFieldNumber);
        fieldName = mmd.getFullFieldName();
        strategy = mmd.getValueStrategy();
        sequence = mmd.getSequence();
        valueGeneratorName = mmd.getValueGeneratorName();
    } else {
        // datastore-identity surrogate field
        fieldName = cmd.getFullClassName() + " (datastore id)";
        strategy = cmd.getIdentityMetaData().getValueStrategy();
        sequence = cmd.getIdentityMetaData().getSequence();
        valueGeneratorName = cmd.getIdentityMetaData().getValueGeneratorName();
    }
    String strategyName = strategy.toString();
    if (strategy.equals(ValueGenerationStrategy.CUSTOM)) {
        // Using a "custom" generator
        strategyName = strategy.getCustomName();
    } else if (strategy.equals(ValueGenerationStrategy.NATIVE)) {
        strategyName = getValueGenerationStrategyForNative(cmd, absoluteFieldNumber);
        strategy = ValueGenerationStrategy.getIdentityStrategy(strategyName);
    }
    // Check for this strategy being a "unique" ValueGenerator, and created if not yet present
    generator = valueGenerationMgr.getUniqueValueGeneratorByName(strategyName);
    if (generator != null) {
        // "unique" ValueGenerator already defined for this strategy, so register it against the member
        valueGenerationMgr.registerValueGeneratorForMemberKey(memberKey, generator);
        return generator;
    }
    // Must be "datastore" specific generator so use plugin mechanism to create one and register against this member "key"
    // Set up the default properties available for all value generators
    // Extract any metadata-based generation information keyed by the "valueGeneratorName"
    TableGeneratorMetaData tableGeneratorMetaData = null;
    SequenceMetaData sequenceMetaData = null;
    if (valueGeneratorName != null) {
        if (strategy == ValueGenerationStrategy.INCREMENT) {
            tableGeneratorMetaData = getMetaDataManager().getMetaDataForTableGenerator(clr, valueGeneratorName);
            if (tableGeneratorMetaData == null) {
                throw new NucleusUserException(Localiser.msg("038005", fieldName, valueGeneratorName));
            }
        } else if (strategy == ValueGenerationStrategy.SEQUENCE) {
            sequenceMetaData = getMetaDataManager().getMetaDataForSequence(clr, valueGeneratorName);
            if (sequenceMetaData == null) {
                throw new NucleusUserException(Localiser.msg("038006", fieldName, valueGeneratorName));
            }
        }
    } else if (strategy == ValueGenerationStrategy.SEQUENCE && sequence != null) {
        // TODO Allow for package name of this class prefix for the sequence name
        sequenceMetaData = getMetaDataManager().getMetaDataForSequence(clr, sequence);
        if (sequenceMetaData == null) {
            // No <sequence> defining the datastore sequence name, so fallback to this name directly in the datastore
            NucleusLogger.VALUEGENERATION.info("Member " + fieldName + " has been specified to use sequence '" + sequence + "' but there is no <sequence> specified in the MetaData. Falling back to use a sequence in the datastore with this name directly.");
        }
    }
    Properties props = getPropertiesForValueGenerator(cmd, absoluteFieldNumber, clr, sequenceMetaData, tableGeneratorMetaData);
    return valueGenerationMgr.createAndRegisterValueGenerator(memberKey, strategyName, props);
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ValueGenerator(org.datanucleus.store.valuegenerator.ValueGenerator) ValueGenerationStrategy(org.datanucleus.metadata.ValueGenerationStrategy) TableGeneratorMetaData(org.datanucleus.metadata.TableGeneratorMetaData) Properties(java.util.Properties) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) SequenceMetaData(org.datanucleus.metadata.SequenceMetaData)

Example 2 with TableGeneratorMetaData

use of org.datanucleus.metadata.TableGeneratorMetaData in project tests by datanucleus.

the class AnnotationPlusXMLTest method testTableGenerator.

/**
 * Test of JPA <table-generator>
 */
public void testTableGenerator() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
    metaDataMgr.loadPersistenceUnit(pumd, null);
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Employee.class.getName(), clr);
    PackageMetaData pmd = cmd1.getPackageMetaData();
    assertEquals("Number of TableGenerators registered for Employee class is wrong", pmd.getNoOfTableGenerators(), 1);
    TableGeneratorMetaData tgmd = pmd.getTableGenerators()[0];
    assertEquals("TableGenerator has incorrect name", tgmd.getName(), "EmployeeGenerator");
    assertEquals("TableGenerator has incorrect table", tgmd.getTableName(), "ID_TABLE");
    assertEquals("TableGenerator has incorrect pk column name", tgmd.getPKColumnName(), "TYPE");
    assertEquals("TableGenerator has incorrect value column name", tgmd.getValueColumnName(), "LATEST_VALUE");
    assertEquals("TableGenerator has incorrect pk column value", tgmd.getPKColumnValue(), "EMPLOYEE");
    assertEquals("TableGenerator has incorrect initial value", tgmd.getInitialValue(), 0);
    assertEquals("TableGenerator has incorrect allocation size", tgmd.getAllocationSize(), 50);
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) PackageMetaData(org.datanucleus.metadata.PackageMetaData) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) TableGeneratorMetaData(org.datanucleus.metadata.TableGeneratorMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 3 with TableGeneratorMetaData

use of org.datanucleus.metadata.TableGeneratorMetaData in project tests by datanucleus.

the class AnnotationTest method testTableGenerator.

/**
 * Test of basic JPA @TableGenerator
 */
public void testTableGenerator() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
    metaDataMgr.loadPersistenceUnit(pumd, null);
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Employee.class.getName(), clr);
    PackageMetaData pmd = cmd1.getPackageMetaData();
    assertEquals("Number of TableGenerators registered for Employee class is wrong", pmd.getNoOfTableGenerators(), 1);
    TableGeneratorMetaData tgmd = pmd.getTableGenerators()[0];
    assertEquals("TableGenerator has incorrect name", tgmd.getName(), "EmployeeGenerator");
    assertEquals("TableGenerator has incorrect table", tgmd.getTableName(), "ID_TABLE");
    assertEquals("TableGenerator has incorrect pk column name", tgmd.getPKColumnName(), "TYPE");
    assertEquals("TableGenerator has incorrect value column name", tgmd.getValueColumnName(), "LATEST_VALUE");
    assertEquals("TableGenerator has incorrect pk column value", tgmd.getPKColumnValue(), "EMPLOYEE");
    assertEquals("TableGenerator has incorrect initial value", tgmd.getInitialValue(), 0);
    assertEquals("TableGenerator has incorrect allocation size", tgmd.getAllocationSize(), 50);
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) PackageMetaData(org.datanucleus.metadata.PackageMetaData) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) TableGeneratorMetaData(org.datanucleus.metadata.TableGeneratorMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 4 with TableGeneratorMetaData

use of org.datanucleus.metadata.TableGeneratorMetaData in project tests by datanucleus.

the class XMLTest method testTableGenerator.

/**
 * Test of JPA <table-generator>
 */
public void testTableGenerator() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
    metaDataMgr.loadPersistenceUnit(pumd, null);
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Employee.class.getName(), clr);
    PackageMetaData pmd = cmd1.getPackageMetaData();
    assertEquals("Number of TableGenerators registered for Employee class is wrong", pmd.getNoOfTableGenerators(), 1);
    TableGeneratorMetaData tgmd = pmd.getTableGenerators()[0];
    assertEquals("TableGenerator has incorrect name", tgmd.getName(), "EmployeeGenerator");
    assertEquals("TableGenerator has incorrect table", tgmd.getTableName(), "ID_TABLE");
    assertEquals("TableGenerator has incorrect pk column name", tgmd.getPKColumnName(), "TYPE");
    assertEquals("TableGenerator has incorrect value column name", tgmd.getValueColumnName(), "LATEST_VALUE");
    assertEquals("TableGenerator has incorrect pk column value", tgmd.getPKColumnValue(), "EMPLOYEE");
    assertEquals("TableGenerator has incorrect initial value", tgmd.getInitialValue(), 0);
    assertEquals("TableGenerator has incorrect allocation size", tgmd.getAllocationSize(), 50);
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) PackageMetaData(org.datanucleus.metadata.PackageMetaData) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) TableGeneratorMetaData(org.datanucleus.metadata.TableGeneratorMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Aggregations

TableGeneratorMetaData (org.datanucleus.metadata.TableGeneratorMetaData)4 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)3 NucleusContext (org.datanucleus.NucleusContext)3 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)3 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)3 ClassMetaData (org.datanucleus.metadata.ClassMetaData)3 MetaDataManager (org.datanucleus.metadata.MetaDataManager)3 PackageMetaData (org.datanucleus.metadata.PackageMetaData)3 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)3 Properties (java.util.Properties)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 SequenceMetaData (org.datanucleus.metadata.SequenceMetaData)1 ValueGenerationStrategy (org.datanucleus.metadata.ValueGenerationStrategy)1 ValueGenerator (org.datanucleus.store.valuegenerator.ValueGenerator)1