Search in sources :

Example 1 with MetaDataManager

use of org.datanucleus.metadata.MetaDataManager in project datanucleus-rdbms by datanucleus.

the class EmbeddedMapping method initialize.

/**
 * Initialize for the specified member.
 * @param mmd metadata for the embedded member
 * @param table Table for persisting this field
 * @param clr The ClassLoaderResolver
 * @param emd Embedded MetaData for the object being embedded
 * @param typeName type of the embedded PC object being stored
 * @param objectType Object type of the PC object being embedded (see StateManagerImpl object types)
 */
public void initialize(AbstractMemberMetaData mmd, Table table, ClassLoaderResolver clr, EmbeddedMetaData emd, String typeName, int objectType) {
    super.initialize(mmd, table, clr);
    this.clr = clr;
    this.emd = emd;
    this.typeName = typeName;
    this.objectType = (short) objectType;
    // Find the MetaData for the embedded PC class
    MetaDataManager mmgr = table.getStoreManager().getMetaDataManager();
    AbstractClassMetaData rootEmbCmd = mmgr.getMetaDataForClass(typeName, clr);
    if (rootEmbCmd == null) {
        // Not found so must be an interface
        // Try using the fieldTypes on the field/property - we support it if only 1 implementation
        String[] fieldTypes = mmd.getFieldTypes();
        if (fieldTypes != null && fieldTypes.length == 1) {
            rootEmbCmd = mmgr.getMetaDataForClass(fieldTypes[0], clr);
        } else if (fieldTypes != null && fieldTypes.length > 1) {
            // TODO Cater for multiple implementations
            throw new NucleusUserException("Field " + mmd.getFullFieldName() + " is a reference field that is embedded with multiple possible implementations. " + "DataNucleus doesnt support embedded reference fields that have more than 1 implementation");
        }
        if (rootEmbCmd == null) {
            // Try a persistent interface
            rootEmbCmd = mmgr.getMetaDataForInterface(clr.classForName(typeName), clr);
            if (rootEmbCmd == null && mmd.getFieldTypes() != null && mmd.getFieldTypes().length == 1) {
                // No MetaData for the type so try "fieldType" specified on the field
                rootEmbCmd = mmgr.getMetaDataForInterface(clr.classForName(mmd.getFieldTypes()[0]), clr);
            }
        }
    }
    if (rootEmbCmd == null) {
        throw new NucleusUserException("Unable to find root class embedded metadata for field=" + mmd.getFullFieldName());
    }
    embCmd = rootEmbCmd;
    AbstractMemberMetaData[] embFmds = null;
    if (emd == null && rootEmbCmd.isEmbeddedOnly()) {
        // No <embedded> block yet the class is defined as embedded-only so just use its own definition of fields
        embFmds = rootEmbCmd.getManagedMembers();
    } else if (emd != null) {
        // <embedded> block so use those field definitions
        embFmds = emd.getMemberMetaData();
    }
    String[] subclasses = mmgr.getSubclassesForClass(rootEmbCmd.getFullClassName(), true);
    if (subclasses != null && subclasses.length > 0) {
        if (rootEmbCmd.hasDiscriminatorStrategy()) {
            // Fabricate a DiscriminatorMetaData to use for the embedded object
            discrimMetaData = new DiscriminatorMetaData();
            InheritanceMetaData embInhMd = new InheritanceMetaData();
            embInhMd.setParent(rootEmbCmd);
            discrimMetaData.setParent(embInhMd);
            // Set strategy based on the inheritance of the embedded object, otherwise class name.
            DiscriminatorMetaData dismd = rootEmbCmd.getDiscriminatorMetaDataRoot();
            if (dismd.getStrategy() != null && dismd.getStrategy() != DiscriminatorStrategy.NONE) {
                discrimMetaData.setStrategy(dismd.getStrategy());
            } else {
                // Fallback to class name
                discrimMetaData.setStrategy(DiscriminatorStrategy.CLASS_NAME);
            }
            // Set column for discriminator
            ColumnMetaData disColmd = new ColumnMetaData();
            disColmd.setAllowsNull(Boolean.TRUE);
            DiscriminatorMetaData embDismd = (emd != null) ? emd.getDiscriminatorMetaData() : null;
            if (embDismd != null && embDismd.getColumnMetaData() != null) {
                disColmd.setName(embDismd.getColumnMetaData().getName());
            } else {
                ColumnMetaData colmd = dismd.getColumnMetaData();
                if (colmd != null && colmd.getName() != null) {
                    disColmd.setName(colmd.getName());
                }
            }
            discrimMetaData.setColumnMetaData(disColmd);
            discrimMapping = DiscriminatorMapping.createDiscriminatorMapping(table, discrimMetaData);
            addDatastoreMapping(discrimMapping.getDatastoreMapping(0));
        } else {
            NucleusLogger.PERSISTENCE.info("Member " + mmd.getFullFieldName() + " is embedded and the type " + "(" + rootEmbCmd.getFullClassName() + ") has potential subclasses." + " Impossible to detect which is stored embedded. Add a discriminator to the embedded type");
        }
    }
    // Add all fields of the embedded class (that are persistent)
    int[] pcFieldNumbers = rootEmbCmd.getAllMemberPositions();
    for (int i = 0; i < pcFieldNumbers.length; i++) {
        AbstractMemberMetaData rootEmbMmd = rootEmbCmd.getMetaDataForManagedMemberAtAbsolutePosition(pcFieldNumbers[i]);
        if (rootEmbMmd.getPersistenceModifier() == FieldPersistenceModifier.PERSISTENT) {
            addMappingForMember(rootEmbCmd, rootEmbMmd, embFmds);
        }
    }
    // Add fields for any subtypes (that are persistent)
    if (discrimMapping != null && subclasses != null && subclasses.length > 0) {
        for (int i = 0; i < subclasses.length; i++) {
            AbstractClassMetaData subEmbCmd = storeMgr.getMetaDataManager().getMetaDataForClass(subclasses[i], clr);
            AbstractMemberMetaData[] subEmbMmds = subEmbCmd.getManagedMembers();
            if (subEmbMmds != null) {
                for (int j = 0; j < subEmbMmds.length; j++) {
                    if (subEmbMmds[j].getPersistenceModifier() == FieldPersistenceModifier.PERSISTENT) {
                        addMappingForMember(subEmbCmd, subEmbMmds[j], embFmds);
                    }
                }
            }
        }
    }
}
Also used : DiscriminatorMetaData(org.datanucleus.metadata.DiscriminatorMetaData) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) MetaDataManager(org.datanucleus.metadata.MetaDataManager) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) InheritanceMetaData(org.datanucleus.metadata.InheritanceMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 2 with MetaDataManager

use of org.datanucleus.metadata.MetaDataManager in project datanucleus-rdbms by datanucleus.

the class SQLStatementHelper method getDiscriminatorValuesForMember.

/**
 * Method to return all possible discriminator values for the supplied class and its subclasses.
 * @param className Name of the class
 * @param discMapping The discriminator mapping
 * @param storeMgr StoreManager
 * @param clr ClassLoader resolver
 * @return The possible discriminator values
 */
public static List getDiscriminatorValuesForMember(String className, JavaTypeMapping discMapping, RDBMSStoreManager storeMgr, ClassLoaderResolver clr) {
    List discrimValues = new ArrayList();
    DiscriminatorStrategy strategy = discMapping.getTable().getDiscriminatorMetaData().getStrategy();
    if (strategy != DiscriminatorStrategy.NONE) {
        MetaDataManager mmgr = storeMgr.getMetaDataManager();
        AbstractClassMetaData cmd = mmgr.getMetaDataForClass(className, clr);
        discrimValues.add(cmd.getDiscriminatorValue());
        Collection<String> subclasses = storeMgr.getSubClassesForClass(className, true, clr);
        if (subclasses != null && subclasses.size() > 0) {
            Iterator<String> subclassesIter = subclasses.iterator();
            while (subclassesIter.hasNext()) {
                String subclassName = subclassesIter.next();
                AbstractClassMetaData subclassCmd = mmgr.getMetaDataForClass(subclassName, clr);
                discrimValues.add(subclassCmd.getDiscriminatorValue());
            }
        }
    }
    return discrimValues;
}
Also used : DiscriminatorStrategy(org.datanucleus.metadata.DiscriminatorStrategy) ArrayList(java.util.ArrayList) MetaDataManager(org.datanucleus.metadata.MetaDataManager) List(java.util.List) ArrayList(java.util.ArrayList) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 3 with MetaDataManager

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

the class JDOTestBase method getEnhancedClassesFromFile.

/**
 * Method to return a set of enhanced classes for all of those found in the specified JDO MetaData file.
 * @param resourceName Name of the MetaData file (relative to the CLASSPATH).
 * @return Set of enhanced classes
 * @throws IllegalArgumentException if an error occurs reading the file resource
 */
public Class[] getEnhancedClassesFromFile(String resourceName) {
    InputStream in = JDOTestBase.class.getClassLoader().getResourceAsStream(resourceName);
    if (in == null) {
        throw new IllegalArgumentException("Cannot load resource :" + resourceName);
    }
    StringBuffer sb = new StringBuffer();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(in));
        String buf = null;
        while ((buf = br.readLine()) != null) {
            sb.append(buf);
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("Error reading MetaData file " + resourceName + ": " + e.getMessage(), e);
    } finally {
        try {
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    String jdoXmlContents = sb.toString();
    if (jdoXmlContents == null) {
        throw new IllegalArgumentException("Contents of file is null");
    }
    NucleusContext context = new EnhancementNucleusContextImpl("JDO", null);
    MetaDataManager mgr = new JDOMetaDataManager(context);
    MetaDataParser parser = new MetaDataParser(mgr, context.getPluginManager(), true, true);
    ClassLoaderResolverImpl clr = new ClassLoaderResolverImpl();
    // Parse the MetaData
    FileMetaData filemd = (FileMetaData) parser.parseMetaDataStream(new ByteArrayInputStream(jdoXmlContents.getBytes()), null, "JDO");
    if (filemd == null) {
        return null;
    }
    mgr.registerFile("EnhancerTestXMLFile", filemd, clr);
    // Populate/Initialise the MetaData for the actual classes.
    for (int i = 0; i < filemd.getNoOfPackages(); i++) {
        PackageMetaData pmd = filemd.getPackage(i);
        for (int j = 0; j < pmd.getNoOfClasses(); j++) {
            ClassMetaData cmd = pmd.getClass(j);
            cmd.populate(clr, null, mgr);
            cmd.initialise(clr);
        }
    }
    // Enhance the classes
    TestClassLoader cl = new TestClassLoader();
    Class[] result;
    ArrayList<Class> resultList = new ArrayList<Class>();
    for (int i = 0; i < filemd.getNoOfPackages(); i++) {
        PackageMetaData pmd = filemd.getPackage(i);
        for (int j = 0; j < pmd.getNoOfClasses(); j++) {
            ClassMetaData cmd = (ClassMetaData) pmd.getClass(j);
            // Enhance the class using the MetaData
            ClassEnhancer enhancer = getClassEnhancer(cmd, mgr);
            enhancer.enhance();
            // Save the enhanced class
            resultList.add(cl.getClass(cmd.getFullClassName(), enhancer.getClassBytes()));
        }
    }
    result = (Class[]) resultList.toArray(new Class[resultList.size()]);
    return result;
}
Also used : NucleusContext(org.datanucleus.NucleusContext) ArrayList(java.util.ArrayList) FileMetaData(org.datanucleus.metadata.FileMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PackageMetaData(org.datanucleus.metadata.PackageMetaData) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) IOException(java.io.IOException) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) MetaDataParser(org.datanucleus.metadata.xml.MetaDataParser) ClassEnhancer(org.datanucleus.enhancer.ClassEnhancer) EnhancementNucleusContextImpl(org.datanucleus.enhancer.EnhancementNucleusContextImpl) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 4 with MetaDataManager

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

the class AnnotationPlusXMLTest method testOneToManyUniMapFK.

/**
 * Test of JPA 1-N unidir Map relation
 */
public void testOneToManyUniMapFK() {
    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);
    // owner side
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Person.class.getName(), clr);
    AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("phoneNumbers");
    assertNotNull("Department.phoneNumbers is null!", fmd1);
    assertEquals("Department.phoneNumbers mapped-by is incorrect", fmd1.getMappedBy(), null);
    assertEquals("Department.phoneNumbers relationType is incorrect", fmd1.getRelationType(clr), RelationType.ONE_TO_MANY_UNI);
    assertEquals("Department.phoneNumbers jointable name is incorrect", fmd1.getTable(), null);
    MapMetaData mmd = fmd1.getMap();
    assertNotNull("Department.phoneNumbers has no Map metadata!", mmd);
    KeyMetaData keymd = fmd1.getKeyMetaData();
    assertNotNull("Department.phoneNumbers has no Key metadata!", keymd);
    assertEquals("Department.phoneNumbers has incorrect key mapped-by", keymd.getMappedBy(), "name");
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) KeyMetaData(org.datanucleus.metadata.KeyMetaData) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) MapMetaData(org.datanucleus.metadata.MapMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 5 with MetaDataManager

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

the class AnnotationPlusXMLTest method testOneToOneBi.

/**
 * Test of 1-1 bi relation
 */
public void testOneToOneBi() {
    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);
    // non-owner side
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Boiler.class.getName(), clr);
    assertEquals("Boiler has incorrect table name", "JPA_AX_BOILER", cmd1.getTable());
    AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("timer");
    assertNotNull("Boiler.timer is null!", fmd1);
    assertEquals("Boiler.timer mapped-by is incorrect", "boiler", fmd1.getMappedBy());
    assertEquals("Boiler.timer relationType is incorrect", RelationType.ONE_TO_ONE_BI, fmd1.getRelationType(clr));
    // owner side
    ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Timer.class.getName(), clr);
    assertEquals("Timer has incorrect table name", "JPA_AX_TIMER", cmd2.getTable());
    AbstractMemberMetaData fmd2 = cmd2.getMetaDataForMember("boiler");
    assertNotNull("Timer.boiler is null!", fmd2);
    assertEquals("Timer.boiler mapped-by is incorrect", null, fmd2.getMappedBy());
    assertEquals("Timer.boiler relationType is incorrect", RelationType.ONE_TO_ONE_BI, fmd2.getRelationType(clr));
    assertNotNull("Timer.boiler has no column info", fmd2.getColumnMetaData());
    assertEquals("Timer.boiler has incorrect number of columns", 1, fmd2.getColumnMetaData().length);
    assertEquals("Timer.boiler column name is wrong", "BOILER_ID", fmd2.getColumnMetaData()[0].getName());
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Aggregations

MetaDataManager (org.datanucleus.metadata.MetaDataManager)102 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)72 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)69 NucleusContext (org.datanucleus.NucleusContext)65 ClassMetaData (org.datanucleus.metadata.ClassMetaData)55 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)51 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)41 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)40 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)38 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)31 JDOMetaDataManager (org.datanucleus.api.jdo.metadata.JDOMetaDataManager)19 ElementMetaData (org.datanucleus.metadata.ElementMetaData)12 ArrayList (java.util.ArrayList)11 JoinMetaData (org.datanucleus.metadata.JoinMetaData)10 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)9 PackageMetaData (org.datanucleus.metadata.PackageMetaData)8 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)7 DN2NamingFactory (org.datanucleus.store.schema.naming.DN2NamingFactory)7 FetchPlanForClass (org.datanucleus.FetchPlanForClass)6 FileMetaData (org.datanucleus.metadata.FileMetaData)6