use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.
the class ExpressionQueryMechanism method buildConcreteSelectStatement.
/**
* Return the appropriate select statement containing the fields in the table.
* This is used as a second read to a concrete class with subclasses in an abstract-multiple table read.
*/
protected SQLSelectStatement buildConcreteSelectStatement() {
// 2612538 - the default size of Map (32) is appropriate
Map clonedExpressions = new IdentityHashMap();
SQLSelectStatement selectStatement = buildBaseSelectStatement(false, clonedExpressions);
ClassDescriptor descriptor = getDescriptor();
InheritancePolicy policy = descriptor.getInheritancePolicy();
// so if a root or branch (!shouldReadSubclasses means leaf), then it must be appended.
if (policy.shouldReadSubclasses()) {
Expression indicatorExpression = null;
// If the descriptor is a single table branch, then select the whole branch in a single query.
if (this.query.isReadAllQuery() && policy.hasChildren() && !policy.hasMultipleTableChild()) {
indicatorExpression = policy.getWithAllSubclassesExpression();
} else {
indicatorExpression = policy.getOnlyInstancesExpression();
}
if ((indicatorExpression != null) && (selectStatement.getWhereClause() != null)) {
selectStatement.setWhereClause(selectStatement.getWhereClause().and(indicatorExpression));
} else if (indicatorExpression != null) {
selectStatement.setWhereClause((Expression) indicatorExpression.clone());
}
}
selectStatement.setFields(getSelectionFields(selectStatement, false));
selectStatement.normalize(getSession(), descriptor, clonedExpressions);
// Allow for joining indexes to be computed to ensure distinct rows.
if (((ObjectLevelReadQuery) this.query).hasJoining()) {
((ObjectLevelReadQuery) this.query).getJoinedAttributeManager().computeJoiningMappingIndexes(false, getSession(), 0);
}
return selectStatement;
}
use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.
the class ExpressionQueryMechanism method prepareCursorSelectAllRows.
/**
* Pre-build the SQL statement from the expression.
*/
@Override
public void prepareCursorSelectAllRows() {
if (getQuery().isReportQuery()) {
SQLSelectStatement statement = buildReportQuerySelectStatement(false);
setSQLStatement(statement);
// For bug 2718118 inheritance with cursors is supported provided there is a read all subclasses view.
} else if (getDescriptor().hasInheritance() && getDescriptor().getInheritancePolicy().requiresMultipleTableSubclassRead() && getDescriptor().getInheritancePolicy().hasView()) {
InheritancePolicy inheritancePolicy = getDescriptor().getInheritancePolicy();
SQLSelectStatement statement = inheritancePolicy.buildViewSelectStatement((ObjectLevelReadQuery) getQuery());
setSQLStatement(statement);
} else {
setSQLStatement(buildNormalSelectStatement());
}
super.prepareCursorSelectAllRows();
}
use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testCascadePersistToNonEntitySubclass.
// Glassfish bug 1021 - allow cascading persist operation to non-entities
public void testCascadePersistToNonEntitySubclass() {
EntityManager em = createEntityManager();
InheritancePolicy ip = getDatabaseSession().getDescriptor(Project.class).getInheritancePolicy();
boolean describesNonPersistentSubclasses = ip.getDescribesNonPersistentSubclasses();
ip.setDescribesNonPersistentSubclasses(true);
beginTransaction(em);
Employee emp = new Employee();
emp.setFirstName("Albert");
emp.setLastName("Einstein");
SuperLargeProject s1 = new SuperLargeProject("Super 1");
Collection projects = new ArrayList();
projects.add(s1);
emp.setProjects(projects);
em.persist(emp);
try {
commitTransaction(em);
} catch (Exception e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
fail("Persist operation was not cascaded to related non-entity, thrown: " + e);
} finally {
ip.setDescribesNonPersistentSubclasses(describesNonPersistentSubclasses);
closeEntityManager(em);
}
}
use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.
the class EntityManagerTLRJUnitTestSuite method testCascadePersistToNonEntitySubclass.
// Glassfish bug 1021 - allow cascading persist operation to non-entities
public void testCascadePersistToNonEntitySubclass() {
EntityManager em = createEntityManager("fieldaccess");
InheritancePolicy ip = getServerSession("fieldaccess").getDescriptor(Project.class).getInheritancePolicy();
boolean describesNonPersistentSubclasses = ip.getDescribesNonPersistentSubclasses();
ip.setDescribesNonPersistentSubclasses(true);
beginTransaction(em);
Employee emp = new Employee();
emp.setFirstName("Albert");
emp.setLastName("Einstein");
SuperLargeProject s1 = new SuperLargeProject("Super 1");
Collection projects = new ArrayList();
projects.add(s1);
emp.setProjects(projects);
em.persist(emp);
try {
commitTransaction(em);
} catch (Exception e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
fail("Persist operation was not cascaded to related non-entity, thrown: " + e);
} finally {
ip.setDescribesNonPersistentSubclasses(describesNonPersistentSubclasses);
closeEntityManager(em);
}
}
use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.
the class ObjectBuilder method buildObjectsInto.
/**
* Return a container which contains the instances of the receivers javaClass.
* Set the fields of the instance to the values stored in the database rows.
*/
public Object buildObjectsInto(ReadAllQuery query, List databaseRows, Object domainObjects) {
if (databaseRows instanceof ThreadCursoredList) {
return buildObjectsFromCursorInto(query, databaseRows, domainObjects);
}
int size = databaseRows.size();
if (size > 0) {
AbstractSession session = query.getSession();
session.startOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
try {
InheritancePolicy inheritancePolicy = null;
if (this.descriptor.hasInheritance()) {
inheritancePolicy = this.descriptor.getInheritancePolicy();
}
boolean isUnitOfWork = session.isUnitOfWork();
boolean shouldCacheQueryResults = query.shouldCacheQueryResults();
boolean shouldUseWrapperPolicy = query.shouldUseWrapperPolicy();
// PERF: Avoid lazy init of join manager if no joining.
JoinedAttributeManager joinManager = null;
if (query.hasJoining()) {
joinManager = query.getJoinedAttributeManager();
}
if (this.descriptor.getCachePolicy().shouldPrefetchCacheKeys() && query.shouldMaintainCache() && !query.shouldRetrieveBypassCache()) {
Object[] pkList = new Object[size];
for (int i = 0; i < size; ++i) {
pkList[i] = extractPrimaryKeyFromRow((AbstractRecord) databaseRows.get(i), session);
}
query.setPrefetchedCacheKeys(session.getIdentityMapAccessorInstance().getAllCacheKeysFromIdentityMapWithEntityPK(pkList, descriptor));
}
ContainerPolicy policy = query.getContainerPolicy();
if (policy.shouldAddAll()) {
List domainObjectsIn = new ArrayList(size);
List<AbstractRecord> databaseRowsIn = new ArrayList(size);
for (int index = 0; index < size; index++) {
AbstractRecord databaseRow = (AbstractRecord) databaseRows.get(index);
// PERF: 1-m joining nulls out duplicate rows.
if (databaseRow != null) {
domainObjectsIn.add(buildObject(query, databaseRow, joinManager, session, this.descriptor, inheritancePolicy, isUnitOfWork, shouldCacheQueryResults, shouldUseWrapperPolicy));
databaseRowsIn.add(databaseRow);
}
}
policy.addAll(domainObjectsIn, domainObjects, session, databaseRowsIn, query, null, true);
} else {
boolean quickAdd = (domainObjects instanceof Collection) && !this.hasWrapperPolicy;
for (int index = 0; index < size; index++) {
AbstractRecord databaseRow = (AbstractRecord) databaseRows.get(index);
// PERF: 1-m joining nulls out duplicate rows.
if (databaseRow != null) {
Object domainObject = buildObject(query, databaseRow, joinManager, session, this.descriptor, inheritancePolicy, isUnitOfWork, shouldCacheQueryResults, shouldUseWrapperPolicy);
if (quickAdd) {
((Collection) domainObjects).add(domainObject);
} else {
policy.addInto(domainObject, domainObjects, session, databaseRow, query, null, true);
}
}
}
}
} finally {
session.endOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
}
}
return domainObjects;
}
Aggregations