use of org.datanucleus.metadata.MetaDataManager in project datanucleus-api-jdo by datanucleus.
the class JDOReplicationManager method replicateRegisteredClasses.
/**
* Method to perform the replication for all objects registered in the pmf source.
*/
public void replicateRegisteredClasses() {
ClassLoaderResolver clr = ((JDOPersistenceManager) pmfSource.getPersistenceManager()).getExecutionContext().getClassLoaderResolver();
MetaDataManager mmgr = ((JDOPersistenceManagerFactory) pmfSource).getNucleusContext().getMetaDataManager();
Collection classNames = mmgr.getClassesWithMetaData();
ArrayList arrayTypes = new ArrayList();
Iterator iterator = classNames.iterator();
while (iterator.hasNext()) {
String className = (String) iterator.next();
AbstractClassMetaData cmd = mmgr.getMetaDataForClass(className, clr);
if (// Omit embedded-only classes since can't replicate those
!cmd.isEmbeddedOnly()) {
arrayTypes.add(clr.classForName(className));
}
}
replicate((Class[]) arrayTypes.toArray(new Class[arrayTypes.size()]));
}
use of org.datanucleus.metadata.MetaDataManager in project datanucleus-api-jdo by datanucleus.
the class JDOReplicationManager method replicate.
/**
* Method to perform the replication for all objects of the specified types.
* @param types Classes to replicate
*/
public void replicate(Class... types) {
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("012052", pmfSource, pmfTarget, StringUtils.objectArrayToString(types)));
}
// Check if classes are detachable
NucleusContext nucleusCtxSource = ((JDOPersistenceManagerFactory) pmfSource).getNucleusContext();
MetaDataManager mmgr = nucleusCtxSource.getMetaDataManager();
ClassLoaderResolver clr = nucleusCtxSource.getClassLoaderResolver(null);
for (int i = 0; i < types.length; i++) {
AbstractClassMetaData cmd = mmgr.getMetaDataForClass(types[i], clr);
if (!cmd.isDetachable()) {
throw new JDOUserException("Class " + types[i] + " is not detachable so cannot replicate");
}
}
Object[] detachedObjects = null;
// Detach from datastore 1
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("012053"));
}
PersistenceManager pm1 = pmfSource.getPersistenceManager();
Transaction tx1 = pm1.currentTransaction();
if (getBooleanProperty("datanucleus.replicateObjectGraph")) {
pm1.getFetchPlan().setGroup(javax.jdo.FetchPlan.ALL);
pm1.getFetchPlan().setMaxFetchDepth(-1);
}
try {
tx1.begin();
ArrayList objects = new ArrayList();
for (int i = 0; i < types.length; i++) {
AbstractClassMetaData cmd = mmgr.getMetaDataForClass(types[i], clr);
if (!cmd.isEmbeddedOnly()) {
Extent ex = pm1.getExtent(types[i]);
Iterator iter = ex.iterator();
while (iter.hasNext()) {
objects.add(iter.next());
}
}
}
Collection detachedColl = pm1.detachCopyAll(objects);
detachedObjects = detachedColl.toArray();
tx1.commit();
} finally {
if (tx1.isActive()) {
tx1.rollback();
}
pm1.close();
}
replicateInTarget(detachedObjects);
}
use of org.datanucleus.metadata.MetaDataManager in project datanucleus-core by datanucleus.
the class PersistenceNucleusContextImpl method initialiseNamedQueries.
/**
* Method to compile all registered named queries (when the user has initialised using a persistence-unit).
* @param clr ClassLoader resolver
*/
protected void initialiseNamedQueries(ClassLoaderResolver clr) {
MetaDataManager mmgr = getMetaDataManager();
Set<String> queryNames = mmgr.getNamedQueryNames();
if (queryNames != null) {
// Compile all named queries of JDOQL/JPQL language using dummy ExecutionContext
ExecutionContext ec = getExecutionContext(null, null);
for (String queryName : queryNames) {
QueryMetaData qmd = mmgr.getMetaDataForQuery(null, clr, queryName);
if (qmd.getLanguage().equals(QueryLanguage.JPQL.toString()) || qmd.getLanguage().equals(QueryLanguage.JDOQL.toString())) {
if (NucleusLogger.QUERY.isDebugEnabled()) {
NucleusLogger.QUERY.debug(Localiser.msg("008017", queryName, qmd.getQuery()));
}
org.datanucleus.store.query.Query q = storeMgr.newQuery(qmd.getLanguage().toString(), ec, qmd.getQuery());
q.compile();
q.closeAll();
}
}
ec.close();
}
}
use of org.datanucleus.metadata.MetaDataManager in project datanucleus-core by datanucleus.
the class PersistenceNucleusContextImpl method initialiseSchema.
/**
* Method to handle generation (create, drop, drop+create) of a schema at initialisation.
* Will generate the schema for all classes that have had their metadata loaded at this point, which
* typically means the persistence-unit.
* @param generateModeStr Generate "mode"
* @param generateScripts Whether to generate
*/
protected void initialiseSchema(String generateModeStr, boolean generateScripts) {
Mode mode = null;
if (generateModeStr.equalsIgnoreCase("create")) {
mode = Mode.CREATE;
} else if (generateModeStr.equalsIgnoreCase("drop")) {
mode = Mode.DELETE;
} else if (generateModeStr.equalsIgnoreCase("drop-and-create")) {
mode = Mode.DELETE_CREATE;
}
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
if (mode == Mode.CREATE) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014000"));
} else if (mode == Mode.DELETE) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014001"));
} else if (mode == Mode.DELETE_CREATE) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014045"));
}
}
// Extract the classes that have metadata loaded (e.g persistence-unit)
Set<String> schemaClassNames = null;
MetaDataManager metaDataMgr = getMetaDataManager();
FileMetaData[] filemds = metaDataMgr.getFileMetaData();
schemaClassNames = new TreeSet<String>();
if (filemds == null) {
throw new NucleusUserException("No classes to process in generateSchema");
}
for (int i = 0; i < filemds.length; i++) {
for (int j = 0; j < filemds[i].getNoOfPackages(); j++) {
for (int k = 0; k < filemds[i].getPackage(j).getNoOfClasses(); k++) {
String className = filemds[i].getPackage(j).getClass(k).getFullClassName();
if (!schemaClassNames.contains(className)) {
schemaClassNames.add(className);
}
}
}
}
StoreManager storeMgr = getStoreManager();
if (storeMgr instanceof SchemaAwareStoreManager) {
SchemaAwareStoreManager schemaStoreMgr = (SchemaAwareStoreManager) storeMgr;
SchemaTool schemaTool = new SchemaTool();
if (mode == Mode.CREATE) {
if (generateScripts) {
// Generate the required script
schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_TARGET));
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
} else {
// Process the required metadata/script
String createOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_CREATE_ORDER);
String createScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_SOURCE);
if (StringUtils.isWhitespace(createScript)) {
createScript = null;
}
if (StringUtils.isWhitespace(createOrder)) {
createOrder = (createScript != null) ? "script" : "metadata";
} else {
if (createOrder.equals("script") || createOrder.equals("metadata-then-script") || createOrder.equals("script-the-metadata")) {
if (createScript == null) {
NucleusLogger.DATASTORE_SCHEMA.warn("create order set to " + createOrder + " but no script defined, so using metadata instead");
createOrder = "metadata";
}
}
}
if (createOrder.equals("script")) {
processDatastoreScript(createScript);
} else if (createOrder.equals("script-then-metadata")) {
processDatastoreScript(createScript);
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
} else if (createOrder.equals("metadata-then-script")) {
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
processDatastoreScript(createScript);
} else {
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
}
}
} else if (mode == Mode.DELETE) {
if (generateScripts) {
// Generate the required script
schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_TARGET));
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
} else {
// Process the required metadata/script
String dropOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_DROP_ORDER);
String dropScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_SOURCE);
if (StringUtils.isWhitespace(dropScript)) {
dropScript = null;
}
if (StringUtils.isWhitespace(dropOrder)) {
dropOrder = (dropScript != null) ? "script" : "metadata";
} else {
if (dropOrder.equals("script") || dropOrder.equals("metadata-then-script") || dropOrder.equals("script-the-metadata")) {
if (dropScript == null) {
NucleusLogger.DATASTORE_SCHEMA.warn("drop order set to " + dropOrder + " but no script defined, so using metadata instead");
dropOrder = "metadata";
}
}
}
if (dropOrder.equals("script")) {
processDatastoreScript(dropScript);
} else if (dropOrder.equals("script-then-metadata")) {
processDatastoreScript(dropScript);
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
} else if (dropOrder.equals("metadata-then-script")) {
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
processDatastoreScript(dropScript);
} else {
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
}
}
} else if (mode == Mode.DELETE_CREATE) {
if (generateScripts) {
// Generate the required scripts
schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_TARGET));
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_TARGET));
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
} else {
// Process the required metadata/scripts
String dropOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_DROP_ORDER);
String dropScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_SOURCE);
if (StringUtils.isWhitespace(dropScript)) {
dropScript = null;
}
if (StringUtils.isWhitespace(dropOrder)) {
dropOrder = (dropScript != null) ? "script" : "metadata";
} else {
if (dropOrder.equals("script") || dropOrder.equals("metadata-then-script") || dropOrder.equals("script-the-metadata")) {
if (dropScript == null) {
NucleusLogger.DATASTORE_SCHEMA.warn("drop order set to " + dropOrder + " but no script defined, so using metadata instead");
dropOrder = "metadata";
}
}
}
if (dropOrder.equals("script")) {
processDatastoreScript(dropScript);
} else if (dropOrder.equals("script-then-metadata")) {
processDatastoreScript(dropScript);
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
} else if (dropOrder.equals("metadata-then-script")) {
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
processDatastoreScript(dropScript);
} else {
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
}
String createOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_CREATE_ORDER);
String createScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_SOURCE);
if (StringUtils.isWhitespace(createScript)) {
createScript = null;
}
if (StringUtils.isWhitespace(createOrder)) {
createOrder = (createScript != null) ? "script" : "metadata";
} else {
if (createOrder.equals("script") || createOrder.equals("metadata-then-script") || createOrder.equals("script-the-metadata")) {
if (createScript == null) {
NucleusLogger.DATASTORE_SCHEMA.warn("create order set to " + createOrder + " but no script defined, so using metadata instead");
createOrder = "metadata";
}
}
}
if (createOrder.equals("script")) {
processDatastoreScript(createScript);
} else if (createOrder.equals("script-then-metadata")) {
processDatastoreScript(createScript);
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
} else if (createOrder.equals("metadata-then-script")) {
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
processDatastoreScript(createScript);
} else {
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
}
}
}
String loadScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_LOAD_SOURCE);
if (!StringUtils.isWhitespace(loadScript)) {
String scriptContent = getDatastoreScriptForResourceName(loadScript);
if (storeMgr instanceof SchemaScriptAwareStoreManager && !StringUtils.isWhitespace(scriptContent)) {
((SchemaScriptAwareStoreManager) storeMgr).executeScript(scriptContent);
}
}
} else {
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("008016", StringUtils.toJVMIDString(storeMgr)));
}
}
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014043"));
}
}
use of org.datanucleus.metadata.MetaDataManager in project tests by datanucleus.
the class AnnotationPlusXMLTest method testOneToManyBiJoin.
/**
* Test of JPA 1-N unidir FK relation.
* This is really a 1-N uni join since JPA doesnt support 1-N uni FK.
*/
/*public void testOneToManyUniFK()
{
NucleusContext nucleusCtx = new NucleusContext("JPA", null);
ClassLoaderResolver clr = new ClassLoaderResolverImpl();
MetaDataManager metaDataMgr = new JPAMetaDataManager(pmfcontext);
PersistenceUnitMetaData pumd = metaDataMgr.getMetaDataForPersistenceUnit("JPATest");
metaDataMgr.initialise(pumd, clr);
// owner side
ClassMetaData cmd1 = (ClassMetaData)metaDataMgr.getMetaDataForClass(Site.class.getName(), clr);
AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("offices");
assertNotNull("Site.offices is null!", fmd1);
assertEquals("Site.offices mapped-by is incorrect", fmd1.getMappedBy(), null);
assertEquals("Site.offices relationType is incorrect",
fmd1.getRelationType(clr), Relation.ONE_TO_MANY_UNI);
assertEquals("Site.offices jointable name is incorrect", fmd1.getTable(), null);
assertNotNull("Site.offices should have join but doesnt", fmd1.getJoinMetaData());
ElementMetaData elemmd = fmd1.getElementMetaData();
assertNotNull("Site.offices has no element column info but should", elemmd);
ColumnMetaData[] colmds = elemmd.getColumnMetaData();
assertNotNull("Site.offices has incorrect element columns", colmds);
assertEquals("Site.offices has incorrect number of element columns", colmds.length, 1);
assertEquals("Site.offices has incorrect element column name", colmds[0].getName(), "SITE_ID");
}*/
/**
* Test of JPA 1-N bidir JoinTable relation
*/
public void testOneToManyBiJoin() {
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(Manager.class.getName(), clr);
assertEquals("Manager has incorrect table name", cmd1.getTable(), "JPA_AX_MANAGER");
AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("subordinates");
assertNotNull("Manager.subordinates is null!", fmd1);
assertEquals("Manager.subordinates mapped-by is incorrect", fmd1.getMappedBy(), "manager");
assertEquals("Manager.subordinates relationType is incorrect", fmd1.getRelationType(clr), RelationType.ONE_TO_MANY_BI);
assertEquals("Manager.subordinates jointable name is incorrect", fmd1.getTable(), "JPA_AX_MGR_EMPLOYEES");
// non-owner side
ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Employee.class.getName(), clr);
assertEquals("Employee has incorrect table name", cmd2.getTable(), "JPA_AX_EMPLOYEE");
AbstractMemberMetaData fmd2 = cmd2.getMetaDataForMember("manager");
assertNotNull("Employee.manager is null!", fmd2);
assertEquals("Employee.manager mapped-by is incorrect", fmd2.getMappedBy(), null);
assertEquals("Employee.manager relationType is incorrect", fmd2.getRelationType(clr), RelationType.MANY_TO_ONE_BI);
assertEquals("Employee.manager jointable name is incorrect", fmd2.getTable(), null);
// join-table
JoinMetaData joinmd = fmd1.getJoinMetaData();
assertNotNull("Manager.subordinates has no join table!", joinmd);
assertNotNull("Manager.subordinates has incorrect join columns", joinmd.getColumnMetaData());
assertEquals("Manager.subordinates has incorrect number of join columns", 1, joinmd.getColumnMetaData().length);
assertEquals("Manager.subordinates has incorrect owner join column name", "MGR_ID", joinmd.getColumnMetaData()[0].getName());
ElementMetaData elemmd = fmd1.getElementMetaData();
assertNotNull("Manager.subordinates has no element column info but should", elemmd);
assertNotNull("Manager.subordinates has incorrect element columns", elemmd.getColumnMetaData());
assertEquals("Manager.subordinates has incorrect number of element columns", 1, elemmd.getColumnMetaData().length);
assertEquals("Manager.subordinates has incorrect element join column name", "EMP_ID", elemmd.getColumnMetaData()[0].getName());
}
Aggregations