use of org.datanucleus.metadata.FetchGroupMetaData in project datanucleus-api-jdo by datanucleus.
the class JDOPersistenceManager method newNamedQuery.
/**
* Construct a query instance with the candidate class and the query name.
* @param cls The class to query
* @param queryName Name of the query.
* @return The query
* @param <T> Candidate type for the query
*/
public <T> Query<T> newNamedQuery(Class<T> cls, String queryName) {
assertIsOpen();
// Throw exception on incomplete input
if (queryName == null) {
throw new JDOUserException(Localiser.msg("011005", null, cls));
}
// Find the Query for the specified class
ClassLoaderResolver clr = ec.getClassLoaderResolver();
QueryMetaData qmd = ec.getMetaDataManager().getMetaDataForQuery(cls, clr, queryName);
if (qmd == null) {
throw new JDOUserException(Localiser.msg("011005", queryName, cls));
}
// Create the Query
Query query = newQuery(qmd.getLanguage(), qmd.getQuery());
if (cls != null) {
query.setClass(cls);
if (!ec.getStoreManager().managesClass(cls.getName())) {
// Load the candidate class since not yet managed
ec.getStoreManager().manageClasses(clr, cls.getName());
}
}
// Optional args that should only be used with SQL
if (qmd.getLanguage().equals(QueryLanguage.JDOQL.toString()) && (qmd.isUnique() || qmd.getResultClass() != null)) {
throw new JDOUserException(Localiser.msg("011007", queryName));
}
if (qmd.isUnique()) {
query.setUnique(true);
}
if (qmd.getResultClass() != null) {
// Set the result class, allowing for it being in the same package as the candidate
Class resultCls = null;
try {
resultCls = clr.classForName(qmd.getResultClass());
} catch (ClassNotResolvedException cnre) {
if (cls != null) {
try {
String resultClassName = cls.getPackage().getName() + "." + qmd.getResultClass();
resultCls = clr.classForName(resultClassName);
} catch (ClassNotResolvedException cnre2) {
throw new JDOUserException(Localiser.msg("011008", queryName, qmd.getResultClass()));
}
}
}
query.setResultClass(resultCls);
}
// Add any extensions
Map<String, String> extmds = qmd.getExtensions();
if (extmds != null) {
Iterator<Entry<String, String>> entryIter = extmds.entrySet().iterator();
while (entryIter.hasNext()) {
Entry<String, String> entry = entryIter.next();
query.addExtension(entry.getKey(), entry.getValue());
}
}
if (qmd.isUnmodifiable()) {
query.setUnmodifiable();
}
if (qmd.getFetchPlanName() != null) {
// Apply any named FetchPlan to the query
FetchPlanMetaData fpmd = ec.getMetaDataManager().getMetaDataForFetchPlan(qmd.getFetchPlanName());
if (fpmd != null) {
org.datanucleus.FetchPlan fp = new org.datanucleus.FetchPlan(ec, clr);
fp.removeGroup(org.datanucleus.FetchPlan.DEFAULT);
FetchGroupMetaData[] fgmds = fpmd.getFetchGroupMetaData();
for (FetchGroupMetaData fgmd : fgmds) {
fp.addGroup(fgmd.getName());
}
fp.setMaxFetchDepth(fpmd.getMaxFetchDepth());
fp.setFetchSize(fpmd.getFetchSize());
((JDOQuery) query).getInternalQuery().setFetchPlan(fp);
}
}
return query;
}
use of org.datanucleus.metadata.FetchGroupMetaData in project datanucleus-api-jdo by datanucleus.
the class TypeMetadataImpl method getFetchGroups.
public FetchGroupMetadata[] getFetchGroups() {
Set<FetchGroupMetaData> internalFgmds = getInternal().getFetchGroupMetaData();
if (internalFgmds == null) {
return null;
}
FetchGroupMetadataImpl[] fgmds = new FetchGroupMetadataImpl[internalFgmds.size()];
int i = 0;
for (FetchGroupMetaData fgmd : internalFgmds) {
fgmds[i] = new FetchGroupMetadataImpl(fgmd);
fgmds[i].parent = this;
i++;
}
return fgmds;
}
use of org.datanucleus.metadata.FetchGroupMetaData in project datanucleus-api-jdo by datanucleus.
the class FetchPlanMetadataImpl method newFetchGroupMetadata.
/* (non-Javadoc)
* @see javax.jdo.metadata.FetchPlanMetadata#newFetchGroupMetadata(java.lang.String)
*/
public FetchGroupMetadata newFetchGroupMetadata(String name) {
FetchGroupMetaData internalFgmd = getInternal().newFetchGroupMetaData(name);
FetchGroupMetadataImpl fgmd = new FetchGroupMetadataImpl(internalFgmd);
fgmd.parent = this;
return fgmd;
}
use of org.datanucleus.metadata.FetchGroupMetaData in project tests by datanucleus.
the class AnnotationTest method testFetchGroups.
/**
* Test for use of annotations with fetch groups
*/
public void testFetchGroups() {
ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Person.class.getName(), clr);
String prefix = cmd1.getFullClassName() + " : ";
Set<FetchGroupMetaData> fgmds = cmd1.getFetchGroupMetaData();
assertNotNull(prefix + "fetch group info is incorrect", fgmds);
FetchGroupMetaData fgmd = fgmds.iterator().next();
assertEquals(prefix + "fetch group name incorrect", "names", fgmd.getName());
assertFalse(prefix + "fetch group fields incorrect", fgmd.getPostLoad());
Set<FetchGroupMemberMetaData> fgmmds = fgmd.getMembers();
assertNotNull(fgmmds);
assertEquals(prefix + "number of fetch group fields is wrong", 2, fgmmds.size());
Iterator<FetchGroupMemberMetaData> fgmmdIter = fgmmds.iterator();
FetchGroupMemberMetaData fgmmd0 = fgmmdIter.next();
FetchGroupMemberMetaData fgmmd1 = fgmmdIter.next();
assertTrue(prefix + "fields in fetch group are incorrect", (fgmmd0.getName().equals("firstName") && fgmmd1.getName().equals("lastName")) || (fgmmd1.getName().equals("firstName") && fgmmd0.getName().equals("lastName")));
}
use of org.datanucleus.metadata.FetchGroupMetaData in project datanucleus-api-jdo by datanucleus.
the class JDOMetaDataHelper method getXMLForMetaData.
public String getXMLForMetaData(FetchGroupMetaData fgmd, String prefix, String indent) {
StringBuilder str = new StringBuilder();
str.append(prefix).append("<fetch-group name=\"" + fgmd.getName() + "\">\n");
// Add fetch-groups
Set<FetchGroupMetaData> fetchGroups = fgmd.getFetchGroups();
if (fetchGroups != null) {
for (FetchGroupMetaData subfgmd : fetchGroups) {
str.append(getXMLForMetaData(subfgmd, prefix + indent, indent));
}
}
// Add fields
Set<FetchGroupMemberMetaData> members = fgmd.getMembers();
if (members != null) {
for (FetchGroupMemberMetaData fgmmd : members) {
str.append(getXMLForMetaData(fgmmd, prefix + indent, indent));
}
}
str.append(prefix + "</fetch-group>\n");
return str.toString();
}
Aggregations