use of org.eclipse.persistence.mappings.CollectionMapping in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestCase method testLazySetInstantiationLazy.
// bug 325035
public void testLazySetInstantiationLazy() {
if (!isWeavingEnabled()) {
return;
}
EntityManager em = createEntityManager();
CollectionMapping mapping = ((CollectionMapping) getServerSession().getProject().getClassDescriptor(AAA.class).getMappingForAttributeName("ddds"));
Boolean lazyIndirection = mapping.shouldUseLazyInstantiationForIndirectCollection();
mapping.setUseLazyInstantiationForIndirectCollection(true);
beginTransaction(em);
AAA a = new AAA();
em.persist(a);
DDD d = new DDD();
em.persist(d);
Set<DDD> ds = new HashSet<DDD>();
ds.add(d);
a.setDdds(ds);
d.setAaa(a);
commitTransaction(em);
clearCache();
try {
em = createEntityManager();
a = em.find(AAA.class, a.getId());
a.getDdds().add(new DDD());
assertTrue("Lazy instantiation was not enabled for IndirectSet.", ((IndirectSet) a.getDdds()).getAddedElements().size() == 1);
} finally {
em = createEntityManager();
beginTransaction(em);
a = em.find(AAA.class, a.getId());
d = em.find(DDD.class, d.getId());
em.remove(a);
em.remove(d);
commitTransaction(em);
mapping.setUseLazyInstantiationForIndirectCollection(lazyIndirection);
}
}
use of org.eclipse.persistence.mappings.CollectionMapping in project eclipselink by eclipse-ee4j.
the class PartitionedTestSuite method testSetup.
/**
* The setup is done as a test, both to record its failure, and to allow execution in the server.
*/
public void testSetup() {
Map properties = new HashMap(JUnitTestCaseHelper.getDatabaseProperties());
if (getServerSession().getPlatform().isDerby()) {
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node2." + PersistenceUnitProperties.CONNECTION_POOL_MIN, "2");
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node2." + PersistenceUnitProperties.CONNECTION_POOL_URL, "jdbc:derby:node2;create=true");
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node3." + PersistenceUnitProperties.CONNECTION_POOL_URL, "jdbc:derby:node3;create=true");
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node3." + PersistenceUnitProperties.CONNECTION_POOL_MAX, "8");
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node3." + PersistenceUnitProperties.CONNECTION_POOL_FAILOVER, "node2, node1");
} else if (getServerSession().getPlatform().isH2()) {
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node2." + PersistenceUnitProperties.CONNECTION_POOL_MIN, "2");
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node2." + PersistenceUnitProperties.CONNECTION_POOL_URL, "jdbc:h2:test2");
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node3." + PersistenceUnitProperties.CONNECTION_POOL_URL, "jdbc:h2:test3");
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node3." + PersistenceUnitProperties.CONNECTION_POOL_MAX, "8");
} else if (getServerSession().getPlatform().isHSQL()) {
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node2." + PersistenceUnitProperties.CONNECTION_POOL_MIN, "2");
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node2." + PersistenceUnitProperties.CONNECTION_POOL_URL, "jdbc:hsqldb:file:test2");
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node3." + PersistenceUnitProperties.CONNECTION_POOL_URL, "jdbc:hsqldb:file:test3");
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node3." + PersistenceUnitProperties.CONNECTION_POOL_MAX, "8");
} else if (!isOnServer() && getServerSession().getPlatform().isOracle() && (getServerSession().getLogin().getURL().indexOf("ems56442") != -1)) {
isRAC = true;
// RAC testing (direct node).
String url = getServerSession().getLogin().getURL();
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node2." + PersistenceUnitProperties.CONNECTION_POOL_MIN, "2");
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node2." + PersistenceUnitProperties.CONNECTION_POOL_URL, (url.substring(0, url.length() - 1)) + "3");
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node3." + PersistenceUnitProperties.CONNECTION_POOL_URL, url);
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node3." + PersistenceUnitProperties.CONNECTION_POOL_MAX, "8");
} else if (!isOnServer() && getServerSession().getPlatform().isOracle() && (getServerSession().getLogin().getURL().indexOf("@(DESCRIPTION") != -1)) {
isRAC = true;
// UCP RAC callback testing.
properties.put(PersistenceUnitProperties.PARTITIONING_CALLBACK, "org.eclipse.persistence.platform.database.oracle.ucp.UCPDataPartitioningCallback");
} else if (isOnServer()) {
isRAC = true;
try {
Class.forName("weblogic.jdbc.common.internal.DataSourceManager");
} catch (Exception notWebLogic) {
warning("Partitioning tests only run on WebLogic with GridLink.");
return;
}
} else {
// Do not run on Sybase as may hang.
if (getServerSession().getPlatform().isSybase()) {
validDatabase = false;
return;
}
isRAC = true;
// Simulate a RAC using multiple connection pools to the same database.
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node2." + PersistenceUnitProperties.CONNECTION_POOL_MIN, "2");
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node2." + PersistenceUnitProperties.CONNECTION_POOL_URL, getServerSession().getLogin().getURL());
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node3." + PersistenceUnitProperties.CONNECTION_POOL_URL, getServerSession().getLogin().getURL());
properties.put(PersistenceUnitProperties.CONNECTION_POOL + "node3." + PersistenceUnitProperties.CONNECTION_POOL_MAX, "8");
}
getEntityManagerFactory(getPersistenceUnitName(), properties);
if (isRAC) {
// Disable replication and unioning in RAC.
for (PartitioningPolicy policy : getDatabaseSession().getProject().getPartitioningPolicies().values()) {
if (policy instanceof RoundRobinPartitioningPolicy) {
((RoundRobinPartitioningPolicy) policy).setReplicateWrites(false);
} else if (policy instanceof UnionPartitioningPolicy) {
((UnionPartitioningPolicy) policy).setReplicateWrites(false);
} else if (policy instanceof CustomPartitioningPolicy) {
((EmployeePartitioningPolicy) ((CustomPartitioningPolicy) policy).getPolicy()).setReplicate(false);
} else if (policy instanceof RangePartitioningPolicy) {
((RangePartitioningPolicy) policy).setUnionUnpartitionableQueries(false);
} else if (policy instanceof ValuePartitioningPolicy) {
((ValuePartitioningPolicy) policy).setUnionUnpartitionableQueries(false);
}
}
CollectionMapping mapping = (CollectionMapping) getDatabaseSession().getDescriptor(Employee.class).getMappingForAttributeName("projects");
PartitioningPolicy policy = getDatabaseSession().getProject().getPartitioningPolicy("defaut");
mapping.setPartitioningPolicy(policy);
mapping.getSelectionQuery().setPartitioningPolicy(policy);
mapping = (CollectionMapping) getDatabaseSession().getDescriptor(Employee.class).getMappingForAttributeName("managedEmployees");
mapping.setPartitioningPolicy(policy);
mapping.getSelectionQuery().setPartitioningPolicy(policy);
}
new PartitionedTableCreator().replaceTables(getDatabaseSession());
EntityManager em = createEntityManager();
try {
PopulationManager.resetDefaultManager();
beginTransaction(em);
new EmployeePopulator().persistExample(em);
commitTransaction(em);
} finally {
closeEntityManagerAndTransaction(em);
}
clearCache();
}
use of org.eclipse.persistence.mappings.CollectionMapping in project eclipselink by eclipse-ee4j.
the class QueryKeyExpression method validateNode.
/**
* Do any required validation for this node. Throw an exception if it's incorrect.
*/
@Override
public void validateNode() {
QueryKey queryKey = getQueryKeyOrNull();
DatabaseMapping mapping = getMapping();
if ((queryKey == null) && (mapping == null)) {
throw QueryException.invalidQueryKeyInExpression(getName());
}
Object theOneThatsNotNull = null;
boolean qkIsToMany = false;
if (queryKey != null) {
theOneThatsNotNull = queryKey;
qkIsToMany = queryKey.isManyToManyQueryKey() || queryKey.isOneToManyQueryKey();
}
boolean isNestedMapping = false;
if (mapping != null) {
// Bug 2847621 - Add Aggregate Collection to the list of valid items for outer join.
if (this.shouldUseOuterJoin && (!(mapping.isOneToOneMapping() || mapping.isOneToManyMapping() || mapping.isManyToManyMapping() || mapping.isAggregateCollectionMapping() || mapping.isDirectCollectionMapping()))) {
throw QueryException.outerJoinIsOnlyValidForOneToOneMappings(mapping);
}
qkIsToMany = mapping.isCollectionMapping();
if (this.index != null) {
if (qkIsToMany) {
CollectionMapping collectionMapping = (CollectionMapping) mapping;
if (collectionMapping.getListOrderField() != null) {
this.index.setField(collectionMapping.getListOrderField());
addDerivedField(this.index);
} else {
throw QueryException.indexRequiresCollectionMappingWithListOrderField(this, collectionMapping);
}
} else {
throw QueryException.indexRequiresCollectionMappingWithListOrderField(this, mapping);
}
}
isNestedMapping = mapping.isNestedTableMapping();
theOneThatsNotNull = mapping;
} else {
if (this.index != null) {
throw QueryException.indexRequiresCollectionMappingWithListOrderField(this, null);
}
}
if ((!shouldQueryToManyRelationship()) && qkIsToMany && (!isNestedMapping)) {
throw QueryException.invalidUseOfToManyQueryKeyInExpression(theOneThatsNotNull);
}
if (shouldQueryToManyRelationship() && !qkIsToMany) {
throw QueryException.invalidUseOfAnyOfInExpression(theOneThatsNotNull);
}
}
use of org.eclipse.persistence.mappings.CollectionMapping in project eclipselink by eclipse-ee4j.
the class PrivateOwnedJUnitTestCase method testDeleteObjectOneByOne.
// Bug 395630
public void testDeleteObjectOneByOne() {
EntityManager em = createEntityManager();
CollectionMapping mapping = (CollectionMapping) getServerSession().getDescriptor(Engine.class).getMappingForAttributeName("sparkPlugs");
boolean deleteOneByOne = mapping.mustDeleteReferenceObjectsOneByOne();
mapping.setMustDeleteReferenceObjectsOneByOne(true);
try {
beginTransaction(em);
Engine eng = new Engine();
eng.setSerialNumber(2222);
em.persist(eng);
SparkPlug plug = new SparkPlug();
plug.setSerialNumber(1111);
eng.addSparkPlug(plug);
em.persist(plug);
em.flush();
plug = new SparkPlug();
plug.setSerialNumber(3333);
eng.addSparkPlug(plug);
em.remove(eng);
commitTransaction(em);
} finally {
mapping.setMustDeleteReferenceObjectsOneByOne(deleteOneByOne);
}
}
use of org.eclipse.persistence.mappings.CollectionMapping in project eclipselink by eclipse-ee4j.
the class ManagedTypeImpl method initialize.
/**
* INTERNAL:
* Initialize the members of this ManagedType based on the mappings defined on the descriptor.
* We process the appropriate Map, List, Set, Collection or Object/primitive types.<p>
* Initialization should occur after all types in the metamodel have been created already.
*/
protected void initialize() {
// See MappedSuperclassType.getMemberFromInheritingType()
if (null != this.members) {
// this is already initialized
return;
}
this.members = new HashMap<String, Attribute<X, ?>>();
// Get and process all mappings on the relationalDescriptor
for (DatabaseMapping mapping : getDescriptor().getMappings()) {
AttributeImpl<X, ?> member = null;
// Tie into the collection hierarchy at a lower level
if (mapping instanceof CollectionMapping) {
// Handle 1:m, n:m collection mappings
CollectionMapping colMapping = (CollectionMapping) mapping;
ContainerPolicy collectionContainerPolicy = colMapping.getContainerPolicy();
if (collectionContainerPolicy.isMapPolicy()) {
// Handle the 3 Map type mappings (policy.isMappedKeyMapPolicy()) is handled by isMapPolicy())
member = new MapAttributeImpl(this, colMapping, true);
// check mapping.attributeAcessor.attributeField.type=Collection
} else if (collectionContainerPolicy.isListPolicy()) {
// This seems very over complex...
/**
* Handle lazy Collections and Lists and the fact that both return an IndirectList policy.
* We check the type on the attributeField of the attributeAccessor on the mapping
*/
Class<?> aType = null;
// 325699: AttributeAccessor is subclassed by both IntanceVariableAttributeAccessor (JPA) and ValuesAccessor (Dynamic JPA)
if (colMapping.getAttributeAccessor() instanceof ValuesAccessor) {
member = new ListAttributeImpl(this, colMapping);
} else if (colMapping.getAttributeAccessor() instanceof InstanceVariableAttributeAccessor) {
Field aField = ((InstanceVariableAttributeAccessor) colMapping.getAttributeAccessor()).getAttributeField();
// MappedSuperclasses need special handling to get their type from an inheriting subclass
if (null == aField) {
// MappedSuperclass field will not be set
if (this.isMappedSuperclass()) {
// get inheriting subtype member (without handling @override annotations)
MappedSuperclassTypeImpl aMappedSuperclass = ((MappedSuperclassTypeImpl) this);
AttributeImpl inheritingTypeMember = aMappedSuperclass.getMemberFromInheritingType(colMapping.getAttributeName());
// 322166: If attribute is defined on this current ManagedType (and not on a superclass) - do not attempt a reflective call on a superclass
if (null != inheritingTypeMember) {
// Verify we have an attributeAccessor
aField = ((InstanceVariableAttributeAccessor) inheritingTypeMember.getMapping().getAttributeAccessor()).getAttributeField();
}
}
}
// 322166: The attribute may be defined on the current ManagedType - not inherited
if (null == aField) {
// Check attributeName when the field is null
aType = this.getTypeClassFromAttributeOrMethodLevelAccessor(mapping);
} else {
aType = aField.getType();
}
// This attribute is declared as List
if ((aType != null) && List.class.isAssignableFrom(aType)) {
member = new ListAttributeImpl(this, colMapping, true);
} else if ((aType != null) && Collection.class.isAssignableFrom(aType)) {
// This attribute is therefore declared as Collection
member = new CollectionAttributeImpl(this, colMapping, true);
} else {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
}
} else {
// handle variations of missing get/set methods - only for Collection vs List
if (colMapping.getAttributeAccessor() instanceof MethodAttributeAccessor) {
/**
* The following call will perform a getMethod call for us.
* If no getMethod exists, we will secondarily check the getMethodName below.
*/
aType = colMapping.getAttributeAccessor().getAttributeClass();
if ((aType != null) && List.class.isAssignableFrom(aType)) {
member = new ListAttributeImpl(this, colMapping, true);
} else if ((aType != null) && Collection.class.isAssignableFrom(aType)) {
member = new CollectionAttributeImpl(this, colMapping, true);
} else {
/**
* In this block we have the following scenario:
* 1) The access type is "field"
* 2) The get method is not set on the entity
* 3) The get method is named differently than the attribute
*/
// Type may be null when no getMethod exists for the class for a ManyToMany mapping
// Here we check the returnType on the declared method on the class directly
String getMethodName = ((MethodAttributeAccessor) colMapping.getAttributeAccessor()).getGetMethodName();
if (null == getMethodName) {
// Check declaredFields in the case where we have no getMethod or getMethodName
try {
Field field = null;
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
field = AccessController.doPrivileged(new PrivilegedGetDeclaredField(this.getJavaType(), colMapping.getAttributeName(), false));
} catch (PrivilegedActionException exception) {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
}
} else {
field = PrivilegedAccessHelper.getDeclaredField(this.getJavaType(), colMapping.getAttributeName(), false);
}
if (null == field) {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
} else {
aType = field.getType();
if ((aType != null) && List.class.isAssignableFrom(aType)) {
member = new ListAttributeImpl(this, colMapping, true);
} else if ((aType != null) && Collection.class.isAssignableFrom(aType)) {
member = new CollectionAttributeImpl(this, colMapping, true);
} else {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
}
}
} catch (Exception e) {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
}
} else {
/**
* Field access Handling:
* If a get method name exists, we check the return type on the method directly
* using reflection.
* In all failure cases we default to the List type.
*/
try {
Method aMethod = null;
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
aMethod = AccessController.doPrivileged(new PrivilegedGetDeclaredMethod(this.getJavaType(), getMethodName, null));
} else {
aMethod = PrivilegedAccessHelper.getDeclaredMethod(this.getJavaType(), getMethodName, null);
}
if (null == aMethod) {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
} else {
aType = aMethod.getReturnType();
if ((aType != null) && List.class.isAssignableFrom(aType)) {
member = new ListAttributeImpl(this, colMapping, true);
} else if ((aType != null) && Collection.class.isAssignableFrom(aType)) {
member = new CollectionAttributeImpl(this, colMapping, true);
} else {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
}
}
} catch (Exception e) {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
}
}
}
}
}
} else {
// Handle non-lazy Collection or Set type mappings (IndirectSet.isAssignableFrom(Set.class) == false)
if ((collectionContainerPolicy.getContainerClass() != null) && Set.class.isAssignableFrom(collectionContainerPolicy.getContainerClass())) {
member = new SetAttributeImpl(this, colMapping, true);
} else {
// Check for non-lazy Collection policy possibly instantiated to a Set or List (both of which is ignored)
if (collectionContainerPolicy.isCollectionPolicy()) {
member = new CollectionAttributeImpl(this, colMapping, true);
} else {
// Handle Collection type mappings as a default (we should never get here)
// TODO: System.out.println("_Warning: defaulting to non-Set specific Collection type on " + colMapping);
member = new CollectionAttributeImpl(this, colMapping);
}
}
}
} else {
// Handle 1:1 single object and direct mappings including EnumSet
member = new SingularAttributeImpl(this, mapping, true);
}
// 303063: secondary check for a null value put - should never happen but this will show on code coverage
if (null == member) {
AbstractSessionLog.getLog().log(SessionLog.FINEST, "metamodel_attribute_getmember_is_null", mapping.getAttributeName(), this, descriptor);
}
this.members.put(mapping.getAttributeName(), member);
}
}
Aggregations