Search in sources :

Example 1 with SequenceObjectDefinition

use of org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition in project eclipselink by eclipse-ee4j.

the class OracleNativeSeqInitTest method setup.

@Override
public void setup() {
    if (!getSession().getPlatform().supportsSequenceObjects()) {
        throw new TestWarningException("This test requires a platform that supports sequence objects");
    }
    ClassDescriptor descriptor = getSession().getDescriptor(Employee.class);
    if (!descriptor.usesSequenceNumbers()) {
        throw new TestWarningException("Employee doesn't use sequencing");
    }
    originalSequence = getSession().getPlatform().getSequence(descriptor.getSequenceNumberName());
    usesNativeSequencingOriginal = (originalSequence instanceof NativeSequence || (originalSequence instanceof DefaultSequence && getSession().getPlatform().getDefaultSequence() instanceof NativeSequence)) && !originalSequence.shouldAcquireValueAfterInsert();
    if (!usesNativeSequencingOriginal) {
        NativeSequence newSequence = new NativeSequence(originalSequence.getName(), originalSequence.getPreallocationSize());
        newSequence.onConnect(originalSequence.getDatasourcePlatform());
        getAbstractSession().getPlatform().addSequence(newSequence);
        sequence = newSequence;
    } else {
        sequence = originalSequence;
    }
    seqPreallocationSizeOriginal = originalSequence.getPreallocationSize();
    lastSeqNumberOriginal = getSession().getNextSequenceNumberValue(Employee.class).intValue() - 1;
    usesBatchWritingOriginal = getSession().getPlatform().usesBatchWriting();
    shouldCacheAllStatementsOriginal = getSession().getPlatform().shouldCacheAllStatements();
    getDatabaseSession().getSequencingControl().initializePreallocated();
    sequenceDefinition = new SequenceObjectDefinition(sequence);
    sequenceDefinition.setQualifier(getSession().getLogin().getTableQualifier());
    if (shouldUseSchemaManager) {
        schemaManager = new SchemaManager(getDatabaseSession());
        // make sure that upcoming DROP and CREATE haven't been cached
        // and therefore for sure will go through
        getSession().getPlatform().setShouldCacheAllStatements(false);
        // This is the worst case scenario settings - SchemaManager should handle it.
        getSession().getPlatform().setUsesBatchWriting(true);
        getSession().getPlatform().setShouldCacheAllStatements(true);
    } else {
        getSession().getPlatform().setUsesBatchWriting(false);
        getSession().getPlatform().setShouldCacheAllStatements(false);
    }
    // all three modes start with dropping an existing sequence (if any)
    try {
        drop();
    } catch (DatabaseException exception) {
    // Ignore already deleted
    }
    if (mode == DROP_CREATE) {
        // sequence doesn't exist.
        // create sequence with seqPreallocationSize.
        // note that both increment and starting value are set to
        // sequenceDefinition.getIncrement()
        sequence.setInitialValue(1);
        sequence.setPreallocationSize(seqPreallocationSize);
        create();
        // next available sequence number.
        idExpected = 1;
    } else if (mode == CREATE_CREATE) {
        // sequence doesn't exist,
        // create sequence with seqPreallocationSizeOld
        // note that both increment and starting value are set to
        // sequenceDefinition.getIncrement()
        sequence.setInitialValue(1);
        sequence.setPreallocationSize(seqPreallocationSizeOld);
        create();
        // now sequence exists,
        // create sequence with seqPreallocationSize
        // Note that createOnDatabase will call alterOnDatabase
        sequence.setInitialValue(1);
        sequence.setPreallocationSize(seqPreallocationSize);
        create();
        // next available sequence number.
        // note that the second createOnDatabase selects NEXTVAL during existance check,
        // because it is the first call to NEXTVAL, the starting sequence value is returned,
        // and this value was set to seqPreallocationSizeOld by the first createOnDatabase
        idExpected = 1 + seqPreallocationSizeOld;
    } else if (mode == NEXTVAL_ALTER) {
        // sequence doesn't exist,
        // create sequence with seqPreallocationSizeOld
        // note that both increment and starting value are set to
        // sequenceDefinition.getIncrement()
        sequence.setInitialValue(1);
        sequence.setPreallocationSize(seqPreallocationSizeOld);
        create();
        // now sequence exists,
        // select NEXTVAL
        // because it is the first call to NEXTVAL, the starting sequence value is returned,
        // and this value was set to seqPreallocationSizeOld by the first createOnDatabase
        sequenceDefinition.checkIfExist((AbstractSession) getSession());
        // alter increment of sequence with seqPreallocationSize.
        sequence.setInitialValue(1);
        sequence.setPreallocationSize(seqPreallocationSize);
        alter();
        // next available sequence number.
        // because there was just one call to NEXTVAL, the starting sequence value is returned,
        // and this value was set to seqPreallocationSizeOld by createOnDatabase
        idExpected = 1 + seqPreallocationSizeOld;
    } else if (mode == CREATE_ALTER) {
        // sequence doesn't exist,
        // create sequence with seqPreallocationSizeOld
        // note that both increment and starting value are set to
        // sequenceDefinition.getIncrement()
        sequence.setInitialValue(1);
        sequence.setPreallocationSize(seqPreallocationSizeOld);
        create();
        // alter increment of sequence with seqPreallocationSize.
        sequence.setInitialValue(1);
        sequence.setPreallocationSize(seqPreallocationSize);
        alter();
        // next available sequence number.
        idExpected = 1;
    }
    getSession().getPlatform().getSequence(descriptor.getSequenceNumberName()).setPreallocationSize(seqPreallocationSize);
}
Also used : NativeSequence(org.eclipse.persistence.sequencing.NativeSequence) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DefaultSequence(org.eclipse.persistence.sequencing.DefaultSequence) SequenceObjectDefinition(org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition) SchemaManager(org.eclipse.persistence.tools.schemaframework.SchemaManager)

Example 2 with SequenceObjectDefinition

use of org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method internalTestSequenceObjectDefinition.

protected void internalTestSequenceObjectDefinition(int preallocationSize, int startValue, String seqName, EntityManager em, AbstractSession ss) {
    NativeSequence sequence = new NativeSequence(seqName, preallocationSize, startValue, false);
    sequence.onConnect(ss.getPlatform());
    SequenceObjectDefinition def = new SequenceObjectDefinition(sequence);
    try {
        // create sequence
        String createStr = def.buildCreationWriter(ss, new StringWriter()).toString();
        beginTransaction(em);
        em.createNativeQuery(createStr).executeUpdate();
        commitTransaction(em);
        // sequence value preallocated
        Vector seqValues = sequence.getGeneratedVector(null, ss);
        int firstSequenceValue = ((Number) seqValues.elementAt(0)).intValue();
        if (firstSequenceValue != startValue) {
            fail(seqName + " sequence with preallocationSize = " + preallocationSize + " and startValue = " + startValue + " produced wrong firstSequenceValue =" + firstSequenceValue);
        }
    } finally {
        sequence.onDisconnect(ss.getPlatform());
        // CREATE and DML on the sequence are still open.
        if (getDatabaseSession().getPlatform().isSymfoware())
            return;
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        // drop sequence
        String dropStr = def.buildDeletionWriter(ss, new StringWriter()).toString();
        beginTransaction(em);
        em.createNativeQuery(dropStr).executeUpdate();
        commitTransaction(em);
    }
}
Also used : NativeSequence(org.eclipse.persistence.sequencing.NativeSequence) StringWriter(java.io.StringWriter) PhoneNumber(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber) SequenceObjectDefinition(org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition) Vector(java.util.Vector)

Example 3 with SequenceObjectDefinition

use of org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition in project eclipselink by eclipse-ee4j.

the class SubstituteSequencingWithReturningPolicyAdapter method createSequences.

public void createSequences(Session session) {
    if (!session.getPlatform().supportsSequenceObjects()) {
        throw new TestWarningException("Requires database platform that supports sequence objects (like Oracle) - they will be used by triggers");
    }
    SchemaManager schemaManager = new SchemaManager((DatabaseSession) session);
    Hashtable sequenceNameToDefinition = new Hashtable();
    Enumeration tableNames = tableToField.keys();
    while (tableNames.hasMoreElements()) {
        String tableName = (String) tableNames.nextElement();
        String sequenceName = getSequenceNameFromTableName(tableName);
        if (!sequenceNameToDefinition.containsKey(sequenceName)) {
            SequenceObjectDefinition definition = new SequenceObjectDefinition(new NativeSequence(sequenceName, 1, false));
            sequenceNameToDefinition.put(sequenceName, definition);
            schemaManager.createObject(definition);
        }
    }
}
Also used : NativeSequence(org.eclipse.persistence.sequencing.NativeSequence) SequenceObjectDefinition(org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition) SchemaManager(org.eclipse.persistence.tools.schemaframework.SchemaManager)

Example 4 with SequenceObjectDefinition

use of org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method internalTestSequenceObjectDefinition.

protected void internalTestSequenceObjectDefinition(int preallocationSize, int startValue, String seqName, EntityManager em, ServerSession ss) {
    NativeSequence sequence = new NativeSequence(seqName, preallocationSize, startValue, false);
    sequence.onConnect(ss.getPlatform());
    SequenceObjectDefinition def = new SequenceObjectDefinition(sequence);
    boolean hasCreatedSequence = false;
    try {
        // create sequence
        String createStr = def.buildCreationWriter(ss, new StringWriter()).toString();
        beginTransaction(em);
        em.createNativeQuery(createStr).setHint(QueryHints.COMPOSITE_UNIT_MEMBER, ss.getName()).executeUpdate();
        commitTransaction(em);
        hasCreatedSequence = true;
        // sequence value preallocated
        Vector seqValues = sequence.getGeneratedVector(null, ss);
        int firstSequenceValue = ((Number) seqValues.elementAt(0)).intValue();
        if (firstSequenceValue != startValue) {
            fail(seqName + " sequence with preallocationSize = " + preallocationSize + " and startValue = " + startValue + " produced wrong firstSequenceValue =" + firstSequenceValue);
        }
    } finally {
        sequence.onDisconnect(ss.getPlatform());
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        // CREATE and DML on the sequence are still open.
        if (hasCreatedSequence && !ss.getPlatform().isSymfoware()) {
            // drop sequence
            String dropStr = def.buildDeletionWriter(ss, new StringWriter()).toString();
            beginTransaction(em);
            em.createNativeQuery(dropStr).setHint(QueryHints.COMPOSITE_UNIT_MEMBER, ss.getName()).executeUpdate();
            commitTransaction(em);
        }
    }
}
Also used : NativeSequence(org.eclipse.persistence.sequencing.NativeSequence) StringWriter(java.io.StringWriter) PhoneNumber(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.PhoneNumber) SequenceObjectDefinition(org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition) Vector(java.util.Vector)

Example 5 with SequenceObjectDefinition

use of org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method internalTestSequenceObjectDefinition.

protected void internalTestSequenceObjectDefinition(int preallocationSize, int startValue, String seqName, EntityManager em, ServerSession ss) {
    NativeSequence sequence = new NativeSequence(seqName, preallocationSize, startValue, false);
    sequence.onConnect(ss.getPlatform());
    SequenceObjectDefinition def = new SequenceObjectDefinition(sequence);
    try {
        // create sequence
        String createStr = def.buildCreationWriter(ss, new StringWriter()).toString();
        beginTransaction(em);
        Query query = em.createNativeQuery(createStr);
        query.setHint(QueryHints.BATCH_WRITING, false);
        query.executeUpdate();
        commitTransaction(em);
        // sequence value preallocated
        Vector seqValues = sequence.getGeneratedVector(null, ss);
        int firstSequenceValue = ((Number) seqValues.elementAt(0)).intValue();
        if (firstSequenceValue != startValue) {
            fail(seqName + " sequence with preallocationSize = " + preallocationSize + " and startValue = " + startValue + " produced wrong firstSequenceValue =" + firstSequenceValue);
        }
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        sequence.onDisconnect(ss.getPlatform());
        // CREATE and DML on the sequence are still open.
        if (JUnitTestCase.getServerSession().getPlatform().isSymfoware())
            return;
        // drop sequence
        String dropStr = def.buildDeletionWriter(ss, new StringWriter()).toString();
        beginTransaction(em);
        Query query = em.createNativeQuery(dropStr);
        query.setHint(QueryHints.BATCH_WRITING, false);
        query.executeUpdate();
        commitTransaction(em);
    }
}
Also used : NativeSequence(org.eclipse.persistence.sequencing.NativeSequence) StringWriter(java.io.StringWriter) ReportQuery(org.eclipse.persistence.queries.ReportQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) Query(jakarta.persistence.Query) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ValueReadQuery(org.eclipse.persistence.queries.ValueReadQuery) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery) TypedQuery(jakarta.persistence.TypedQuery) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) SequenceObjectDefinition(org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition) Vector(java.util.Vector)

Aggregations

NativeSequence (org.eclipse.persistence.sequencing.NativeSequence)6 SequenceObjectDefinition (org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition)6 StringWriter (java.io.StringWriter)4 Vector (java.util.Vector)4 SchemaManager (org.eclipse.persistence.tools.schemaframework.SchemaManager)2 Query (jakarta.persistence.Query)1 TypedQuery (jakarta.persistence.TypedQuery)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)1 DataModifyQuery (org.eclipse.persistence.queries.DataModifyQuery)1 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)1 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)1 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)1 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)1 ReportQuery (org.eclipse.persistence.queries.ReportQuery)1 ValueReadQuery (org.eclipse.persistence.queries.ValueReadQuery)1 DefaultSequence (org.eclipse.persistence.sequencing.DefaultSequence)1 Employee (org.eclipse.persistence.testing.models.employee.domain.Employee)1 PhoneNumber (org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)1 PhoneNumber (org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.PhoneNumber)1