use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.
the class PreLoginMappingAdapter method preLogin.
/* (non-Javadoc)
* @see org.eclipse.persistence.internal.jaxb.SessionEventListener#preLogin(org.eclipse.persistence.sessions.SessionEvent)
*/
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void preLogin(SessionEvent event) {
Project project = event.getSession().getProject();
ClassLoader cl = jpaSession.getDatasourcePlatform().getConversionManager().getLoader();
DefaultXMLNameTransformer xmlNameTransformer = new DefaultXMLNameTransformer();
for (Object descriptorAlias : project.getAliasDescriptors().keySet()) {
ClassDescriptor descriptor = project.getAliasDescriptors().get(descriptorAlias);
if (!PersistenceWeavedRest.class.isAssignableFrom(descriptor.getJavaClass())) {
continue;
}
if (descriptor.isXMLDescriptor()) {
XMLDescriptor xmlDescriptor = (XMLDescriptor) project.getAliasDescriptors().get(descriptorAlias);
if (null != xmlDescriptor) {
if (null == xmlDescriptor.getDefaultRootElement()) {
// set root element
xmlDescriptor.setDefaultRootElement(xmlNameTransformer.transformRootElementName(xmlDescriptor.getJavaClass().getName()));
// set resultAlwaysXMLRoot to false, so that the elements are not wrapped in JAXBElements when unmarshalling.
xmlDescriptor.setResultAlwaysXMLRoot(false);
}
}
}
XMLCompositeCollectionMapping relationshipMapping = new XMLCompositeCollectionMapping();
relationshipMapping.setAttributeName("_persistence_relationshipInfo");
relationshipMapping.setGetMethodName("_persistence_getRelationships");
relationshipMapping.setSetMethodName("_persistence_setRelationships");
relationshipMapping.setDescriptor(descriptor);
CollectionContainerPolicy containerPolicy = new CollectionContainerPolicy(ArrayList.class);
relationshipMapping.setContainerPolicy(containerPolicy);
relationshipMapping.setField(new XMLField("_relationships"));
relationshipMapping.setReferenceClass(Link.class);
XMLJavaTypeConverter converter = new XMLJavaTypeConverter(RelationshipLinkAdapter.class);
converter.initialize(relationshipMapping, event.getSession());
relationshipMapping.setConverter(converter);
descriptor.addMapping(relationshipMapping);
XMLCompositeObjectMapping hrefMapping = new XMLCompositeObjectMapping();
hrefMapping.setAttributeName("_persistence_href");
hrefMapping.setGetMethodName("_persistence_getHref");
hrefMapping.setSetMethodName("_persistence_setHref");
hrefMapping.setDescriptor(descriptor);
hrefMapping.setField(new XMLField("_link"));
hrefMapping.setReferenceClass(Link.class);
hrefMapping.setXPath(".");
descriptor.addMapping(hrefMapping);
XMLCompositeObjectMapping itemLinksMapping = new XMLCompositeObjectMapping();
itemLinksMapping.setAttributeName("_persistence_links");
itemLinksMapping.setGetMethodName("_persistence_getLinks");
itemLinksMapping.setSetMethodName("_persistence_setLinks");
itemLinksMapping.setDescriptor(descriptor);
itemLinksMapping.setReferenceClass(ItemLinks.class);
itemLinksMapping.setXPath(".");
descriptor.addMapping(itemLinksMapping);
ClassDescriptor jpaDescriptor = jpaSession.getDescriptorForAlias(descriptor.getAlias());
Vector<DatabaseMapping> descriptorMappings = (Vector<DatabaseMapping>) descriptor.getMappings().clone();
for (DatabaseMapping mapping : descriptorMappings) {
if (mapping.isXMLMapping()) {
if (mapping.isAbstractCompositeObjectMapping() || mapping.isAbstractCompositeCollectionMapping()) {
if (mapping.isAbstractCompositeCollectionMapping()) {
XMLInverseReferenceMapping inverseMapping = ((XMLCompositeCollectionMapping) mapping).getInverseReferenceMapping();
if (inverseMapping != null) {
break;
}
} else if (mapping.isAbstractCompositeObjectMapping()) {
XMLInverseReferenceMapping inverseMapping = ((XMLCompositeObjectMapping) mapping).getInverseReferenceMapping();
if (inverseMapping != null) {
break;
}
}
if (jpaDescriptor != null) {
DatabaseMapping dbMapping = jpaDescriptor.getMappingForAttributeName(mapping.getAttributeName());
if ((dbMapping != null) && (dbMapping instanceof ForeignReferenceMapping)) {
ForeignReferenceMapping jpaMapping = (ForeignReferenceMapping) dbMapping;
if (jpaMapping.getMappedBy() != null) {
ClassDescriptor inverseDescriptor = project.getDescriptorForAlias(jpaMapping.getReferenceDescriptor().getAlias());
if (inverseDescriptor != null) {
DatabaseMapping inverseMapping = inverseDescriptor.getMappingForAttributeName(jpaMapping.getMappedBy());
if (inverseMapping != null) {
convertMappingToXMLInverseReferenceMapping(inverseDescriptor, inverseMapping, jpaMapping);
}
}
}
}
}
}
}
}
InheritancePolicy inheritancePolicy = descriptor.getInheritancePolicyOrNull();
if ((inheritancePolicy != null) && (inheritancePolicy.isRootParentDescriptor())) {
boolean isAbstract = Modifier.isAbstract(descriptor.getJavaClass().getModifiers());
if (isAbstract) {
Class subClassToInstantiate = null;
Map<?, ?> classIndicatorMapping = inheritancePolicy.getClassIndicatorMapping();
// get one of subclasses that extends this abstract class
for (Map.Entry<?, ?> entry : classIndicatorMapping.entrySet()) {
Object value = entry.getValue();
if (value instanceof Class) {
subClassToInstantiate = (Class) value;
isAbstract = Modifier.isAbstract(subClassToInstantiate.getModifiers());
if (!isAbstract) {
InstantiationPolicy instantiationPolicy = new InstantiationPolicy();
instantiationPolicy.useFactoryInstantiationPolicy(new ConcreteSubclassFactory(subClassToInstantiate), "createConcreteSubclass");
descriptor.setInstantiationPolicy(instantiationPolicy);
break;
}
}
}
}
}
}
for (Object descriptorAlias : project.getAliasDescriptors().keySet()) {
ClassDescriptor descriptor = project.getAliasDescriptors().get(descriptorAlias);
ClassDescriptor jpaDescriptor = jpaSession.getDescriptorForAlias(descriptor.getAlias());
Vector<DatabaseMapping> descriptorMappings = (Vector<DatabaseMapping>) descriptor.getMappings().clone();
for (DatabaseMapping mapping : descriptorMappings) {
if (mapping.isXMLMapping()) {
if (mapping.isAbstractCompositeObjectMapping() || mapping.isAbstractCompositeCollectionMapping()) {
if (jpaDescriptor != null) {
DatabaseMapping dbMapping = jpaDescriptor.getMappingForAttributeName(mapping.getAttributeName());
if ((dbMapping instanceof ForeignReferenceMapping)) {
ForeignReferenceMapping jpaMapping = (ForeignReferenceMapping) dbMapping;
ClassDescriptor jaxbDescriptor = project.getDescriptorForAlias(jpaMapping.getDescriptor().getAlias());
convertMappingToXMLChoiceMapping(jaxbDescriptor, jpaMapping, cl, jpaSession);
}
} else if (mapping instanceof XMLCompositeObjectMapping) {
// Fix for Bug 403113 - JPA-RS Isn't Serializing an Embeddable defined in an ElementCollection to JSON Correctly
// add choice mapping for one-to-one relationships within embeddables
// Based on (http://wiki.eclipse.org/EclipseLink/Examples/JPA/NoSQL#Step_2_:_Map_the_data),
// the mappedBy option on relationships is not supported for NoSQL data, so no need to add inverse mapping
XMLCompositeObjectMapping jpaMapping = (XMLCompositeObjectMapping) mapping;
ClassDescriptor jaxbDescriptor = project.getDescriptorForAlias(jpaMapping.getDescriptor().getAlias());
if (jaxbDescriptor != null) {
Class clazz = jpaMapping.getReferenceClass();
if (clazz != null) {
if ((jpaSession.getDescriptor(clazz) != null) && (jpaSession.getDescriptor(clazz).isEISDescriptor()))
convertMappingToXMLChoiceMapping(jaxbDescriptor, jpaMapping, cl, jpaSession);
}
}
}
}
}
}
}
}
use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.
the class ProjectClassGeneratorInheritanceMappingTest method setup.
/**
* Setup what we want written out.
*/
@Override
public void setup() {
InheritancePolicy iPolicy = project.getDescriptor(org.eclipse.persistence.testing.models.employee.domain.Project.class).getInheritancePolicy();
// Inheritance Properties.
iPolicy.addClassIndicator(org.eclipse.persistence.testing.models.employee.domain.SmallProject.class, Byte.valueOf("1"));
iPolicy.addClassIndicator(org.eclipse.persistence.testing.models.employee.domain.LargeProject.class, Short.valueOf("2"));
iPolicy.addClassIndicator(org.eclipse.persistence.testing.models.employee.domain.Project.class, Long.valueOf("3"));
iPolicy.addClassIndicator(org.eclipse.persistence.testing.models.employee.domain.PhoneNumber.class, Double.valueOf("4.0"));
}
use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.
the class ObjectBuilder method buildObject.
/**
* Return an instance of the receivers javaClass. Set the attributes of an instance
* from the values stored in the database row.
*/
public Object buildObject(ObjectBuildingQuery query, AbstractRecord databaseRow, JoinedAttributeManager joinManager) {
InheritancePolicy inheritancePolicy = null;
if (this.descriptor.hasInheritance()) {
inheritancePolicy = this.descriptor.getInheritancePolicy();
}
AbstractSession session = query.getSession();
session.startOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
Object domainObject = null;
try {
domainObject = buildObject(query, databaseRow, joinManager, session, this.descriptor, inheritancePolicy, session.isUnitOfWork(), query.shouldCacheQueryResults(), query.shouldUseWrapperPolicy());
} finally {
session.endOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
}
return domainObject;
}
use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.
the class ExpressionQueryMechanism method prepareSelectOneRow.
/**
* Pre-build the SQL statement from the expression.
*/
@Override
public void prepareSelectOneRow() {
// Check for multiple table inheritance which may require multiple queries.
if (!getDescriptor().hasInheritance() || !getDescriptor().getInheritancePolicy().requiresMultipleTableSubclassRead()) {
setSQLStatement(buildNormalSelectStatement());
super.prepareSelectOneRow();
} else {
InheritancePolicy policy = getDescriptor().getInheritancePolicy();
if (policy.hasView()) {
// CR#3158703 if the descriptor has a view, then it requires a single select,
// so can be prepared.
setSQLStatement(getDescriptor().getInheritancePolicy().buildViewSelectStatement((ObjectLevelReadQuery) getQuery()));
super.prepareSelectOneRow();
} else if (((ObjectLevelReadQuery) getQuery()).shouldOuterJoinSubclasses()) {
// outer join into a single select that can be built normally
setSQLStatement(buildNormalSelectStatement());
super.prepareSelectOneRow();
} else if (!getDescriptor().getInheritancePolicy().hasClassExtractor()) {
// CR#3158703 otherwise if using a type indicator at least the type select can be prepared.
setSQLStatement(getDescriptor().getInheritancePolicy().buildClassIndicatorSelectStatement((ObjectLevelReadQuery) getQuery()));
super.prepareSelectOneRow();
}
}
}
use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.
the class ExpressionQueryMechanism method prepareSelectAllRows.
/**
* Pre-build the SQL statement from the expression.
*/
@Override
public void prepareSelectAllRows() {
// Check for multiple table inheritance which may require multiple queries.
if (!getDescriptor().hasInheritance() || !getDescriptor().getInheritancePolicy().requiresMultipleTableSubclassRead()) {
setSQLStatement(buildNormalSelectStatement());
super.prepareSelectAllRows();
} else {
InheritancePolicy policy = getDescriptor().getInheritancePolicy();
if (policy.hasView()) {
// CR#3158703 if the descriptor has a view, then it requires a single select,
// so can be prepared.
setSQLStatement(getDescriptor().getInheritancePolicy().buildViewSelectStatement((ObjectLevelReadQuery) getQuery()));
super.prepareSelectAllRows();
} else if (((ObjectLevelReadQuery) getQuery()).shouldOuterJoinSubclasses()) {
// outer join into a single select that can be built normally
setSQLStatement(buildNormalSelectStatement());
super.prepareSelectAllRows();
} else if (!getDescriptor().getInheritancePolicy().hasClassExtractor()) {
// CR#3158703 otherwise if using a type indicator at least the type select can be prepared.
setSQLStatement(getDescriptor().getInheritancePolicy().buildClassIndicatorSelectStatement((ObjectLevelReadQuery) getQuery()));
super.prepareSelectAllRows();
}
}
}
Aggregations