Search in sources :

Example 86 with AbstractClassMetaData

use of org.datanucleus.metadata.AbstractClassMetaData in project motech by motech.

the class StateManagerUtil method setTransactionVersion.

/**
 * Sets the given transaction version to the instance state manager.
 *
 * @param instance the instance from which state manager will be retrieved
 * @param version the transaction version
 * @param versionFieldName the name of the version field
 */
public static void setTransactionVersion(Object instance, Object version, String versionFieldName) {
    try {
        StateManagerImpl stateManager = getStateManager(instance);
        stateManager.setVersion(version);
        AbstractClassMetaData cmd = stateManager.getClassMetaData();
        int fieldPosition = cmd.getAbsolutePositionOfMember(versionFieldName);
        boolean[] dirtyFields = getDirtyFields(stateManager);
        // we must mark version field as non dirty
        dirtyFields[fieldPosition] = false;
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new MdsException("Cannot set proper transaction version", e);
    }
}
Also used : MdsException(org.motechproject.mds.exception.MdsException) StateManagerImpl(org.datanucleus.state.StateManagerImpl) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 87 with AbstractClassMetaData

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

the class JPANamingFactoryTest method testDatastoreIdColumnName.

public void testDatastoreIdColumnName() {
    JPAEntityManagerFactory jpaEMF = (JPAEntityManagerFactory) emf;
    NucleusContext nucCtx = jpaEMF.getNucleusContext();
    ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
    MetaDataManager mmgr = nucCtx.getMetaDataManager();
    AbstractClassMetaData cmd1 = mmgr.getMetaDataForClass(MyDatastoreId.class, clr);
    NamingFactory factory = new JPANamingFactory(nucCtx);
    factory.setMaximumLength(SchemaComponent.COLUMN, 128);
    factory.setNamingCase(NamingCase.LOWER_CASE);
    assertEquals("Column name for datastore-id is incorrect", "mydatastoreid_id", factory.getColumnName(cmd1, ColumnType.DATASTOREID_COLUMN));
}
Also used : JPAEntityManagerFactory(org.datanucleus.api.jpa.JPAEntityManagerFactory) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) NamingFactory(org.datanucleus.store.schema.naming.NamingFactory) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 88 with AbstractClassMetaData

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

the class JPANamingFactoryTest method testVersionColumnName.

public void testVersionColumnName() {
    JPAEntityManagerFactory jpaEMF = (JPAEntityManagerFactory) emf;
    NucleusContext nucCtx = jpaEMF.getNucleusContext();
    ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
    MetaDataManager mmgr = nucCtx.getMetaDataManager();
    AbstractClassMetaData cmd1 = mmgr.getMetaDataForClass(Trade4.class, clr);
    NamingFactory factory = new JPANamingFactory(nucCtx);
    factory.setMaximumLength(SchemaComponent.COLUMN, 128);
    factory.setNamingCase(NamingCase.LOWER_CASE);
    assertEquals("Column name for version is incorrect", "version", factory.getColumnName(cmd1, ColumnType.VERSION_COLUMN));
}
Also used : JPAEntityManagerFactory(org.datanucleus.api.jpa.JPAEntityManagerFactory) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) NamingFactory(org.datanucleus.store.schema.naming.NamingFactory) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 89 with AbstractClassMetaData

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

the class JPANamingFactoryTest method testTableName.

public void testTableName() {
    JPAEntityManagerFactory jpaEMF = (JPAEntityManagerFactory) emf;
    NucleusContext nucCtx = jpaEMF.getNucleusContext();
    ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
    MetaDataManager mmgr = nucCtx.getMetaDataManager();
    AbstractClassMetaData cmd1 = mmgr.getMetaDataForClass(ByteArray.class, clr);
    NamingFactory factory = new JPANamingFactory(nucCtx);
    factory.setMaximumLength(SchemaComponent.TABLE, 128);
    factory.setNamingCase(NamingCase.LOWER_CASE);
    assertEquals("Table name is incorrect", "bytearray", factory.getTableName(cmd1));
    factory.setNamingCase(NamingCase.LOWER_CASE_QUOTED);
    assertEquals("Table name is incorrect", "\"bytearray\"", factory.getTableName(cmd1));
    factory.setNamingCase(NamingCase.UPPER_CASE);
    assertEquals("Table name is incorrect", "BYTEARRAY", factory.getTableName(cmd1));
    factory.setNamingCase(NamingCase.UPPER_CASE_QUOTED);
    assertEquals("Table name is incorrect", "\"BYTEARRAY\"", factory.getTableName(cmd1));
    AbstractClassMetaData cmd2 = mmgr.getMetaDataForClass(SimpleClass.class, clr);
    factory.setNamingCase(NamingCase.LOWER_CASE);
    assertEquals("Table name is incorrect", "mysimpleclass", factory.getTableName(cmd2));
}
Also used : JPAEntityManagerFactory(org.datanucleus.api.jpa.JPAEntityManagerFactory) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) NamingFactory(org.datanucleus.store.schema.naming.NamingFactory) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 90 with AbstractClassMetaData

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

the class ConvertersTest method testUseOfPersistentConverter.

/**
 * Test the use of "@Persistent(converter="...")" annotation on a field.
 */
public void testUseOfPersistentConverter() {
    try {
        PersistenceNucleusContext nucCtx = ((JDOPersistenceManagerFactory) pmf).getNucleusContext();
        ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
        AbstractClassMetaData cmd = nucCtx.getMetaDataManager().getMetaDataForClass(PersonWithConverters.class, clr);
        // Check the converter is registered with metadata
        AbstractMemberMetaData mmd = cmd.getMetaDataForMember("myBool1");
        assertNotNull(mmd);
        assertTrue(mmd.hasExtension("type-converter-name"));
        String converterName = mmd.getValueForExtension("type-converter-name");
        assertEquals("org.datanucleus.samples.converters.Boolean10Converter", converterName);
        // Check the correct mapping is chosen for this field
        RDBMSStoreManager storeMgr = (RDBMSStoreManager) nucCtx.getStoreManager();
        DatastoreClass tbl = storeMgr.getDatastoreClass(PersonWithConverters.class.getName(), clr);
        JavaTypeMapping mapping = tbl.getMemberMapping(mmd);
        assertTrue(mapping instanceof TypeConverterMapping);
    } catch (Exception e) {
        LOG.error("Exception during test", e);
        fail("Exception was thrown : " + e.getMessage());
    } finally {
    }
}
Also used : TypeConverterMapping(org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) PersonWithConverters(org.datanucleus.samples.converters.PersonWithConverters) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Aggregations

AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)204 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)90 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)69 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)59 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)55 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)42 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)42 NucleusException (org.datanucleus.exceptions.NucleusException)37 MetaDataManager (org.datanucleus.metadata.MetaDataManager)37 ArrayList (java.util.ArrayList)31 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)26 SQLTable (org.datanucleus.store.rdbms.sql.SQLTable)23 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)22 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)19 MapTable (org.datanucleus.store.rdbms.table.MapTable)18 List (java.util.List)16 ObjectProvider (org.datanucleus.state.ObjectProvider)16 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)16 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)16 Iterator (java.util.Iterator)15