use of org.eclipse.persistence.internal.queries.MapContainerPolicy in project eclipselink by eclipse-ee4j.
the class UseTransparentMapOnCollectionMapping method setup.
@Override
protected void setup() {
getSession().getIdentityMapAccessor().initializeAllIdentityMaps();
descriptorToModify = project.getDescriptors().get(Employee.class);
policy = new TransparentIndirectionPolicy();
mapPolicy = new MapContainerPolicy();
for (Enumeration<DatabaseMapping> mappingsEnum = (descriptorToModify.getMappings()).elements(); mappingsEnum.hasMoreElements(); ) {
mappingToModify = mappingsEnum.nextElement();
if (mappingToModify.isForeignReferenceMapping()) {
if (mappingToModify.isCollectionMapping()) {
CollectionMapping collectionMapping = (CollectionMapping) mappingToModify;
collectionMapping.setContainerPolicy(mapPolicy);
mapPolicy.setKeyName("testMethod");
collectionMapping.getContainerPolicy().setContainerClass(Vector.class);
((ForeignReferenceMapping) mappingToModify).setIndirectionPolicy(policy);
}
}
}
}
use of org.eclipse.persistence.internal.queries.MapContainerPolicy in project eclipselink by eclipse-ee4j.
the class CollectionMappingIsMapPolicyTest method setup.
@Override
protected void setup() {
getSession().getIdentityMapAccessor().initializeAllIdentityMaps();
descriptorToModify = project.getDescriptors().get(Employee.class);
policy = new MapContainerPolicy();
for (Enumeration<DatabaseMapping> mappingsEnum = (descriptorToModify.getMappings()).elements(); mappingsEnum.hasMoreElements(); ) {
mappingToModify = mappingsEnum.nextElement();
if (mappingToModify.isForeignReferenceMapping()) {
if (mappingToModify.isCollectionMapping()) {
CollectionMapping collectionMapping = (CollectionMapping) mappingToModify;
collectionMapping.setContainerPolicy(policy);
policy.setKeyName("testMethod");
collectionMapping.getContainerPolicy().setContainerClass(Vector.class);
}
}
}
}
use of org.eclipse.persistence.internal.queries.MapContainerPolicy in project eclipselink by eclipse-ee4j.
the class XMLCompositeCollectionMapping method initialize.
/**
* INTERNAL:
* The mapping is initialized with the given session. This mapping is fully initialized
* after this.
*/
@Override
public void initialize(AbstractSession session) throws DescriptorException {
// modified so that reference class on composite mappings is no longer mandatory
String referenceClassName = getReferenceClassName();
if (this.referenceClass == null && referenceClassName != null) {
if (!referenceClassName.equals(XMLConstants.UNKNOWN_OR_TRANSIENT_CLASS)) {
setReferenceClass(session.getDatasourcePlatform().getConversionManager().convertClassNameToClass(referenceClassName));
}
}
initializeReferenceDescriptorAndField(session);
ContainerPolicy cp = getContainerPolicy();
if (cp != null) {
if (cp.getContainerClass() == null) {
Class<Object> cls = session.getDatasourcePlatform().getConversionManager().convertClassNameToClass(cp.getContainerClassName());
cp.setContainerClass(cls);
}
if (cp instanceof MapContainerPolicy) {
initializeMapContainerPolicy(session, (MapContainerPolicy) cp);
}
}
if (null != getContainerAccessor()) {
getContainerAccessor().initializeAttributes(this.referenceClass);
}
}
use of org.eclipse.persistence.internal.queries.MapContainerPolicy in project eclipselink by eclipse-ee4j.
the class XMLVariableXPathCollectionMapping method useMapClassName.
@Override
public void useMapClassName(String concreteContainerClassName, String methodName) {
// the reference class has to be specified before coming here
if (this.getReferenceClassName() == null) {
throw DescriptorException.referenceClassNotSpecified(this);
}
MapContainerPolicy policy = new MapContainerPolicy(concreteContainerClassName);
policy.setKeyName(methodName, getReferenceClass().getName());
this.setContainerPolicy(policy);
}
use of org.eclipse.persistence.internal.queries.MapContainerPolicy in project eclipselink by eclipse-ee4j.
the class RelationshipModelJUnitTestSuite method testGetResultListTest.
/*
* Tests using the 'getSingleResult' api on a Query object obtained from the
* EntityManager Also tests bugs 4300879 - check non Collection container
* policy error and 4297903 - check ReadObjectQuery fails
*/
public void testGetResultListTest() {
Collection returnedCustomers1, returnedCustomers2;
QueryException expectedException1 = null;
String ejbql1 = "SELECT OBJECT(thecust) FROM FieldAccessCustomer thecust WHERE thecust.customerId = :id";
Integer[] cusIDs = new Integer[3];
Customer cusClone1 = RelationshipsExamples.customerExample1();
Customer cusClone2 = RelationshipsExamples.customerExample2();
EntityManager em = createEntityManager("fieldaccess");
beginTransaction(em);
em.persist(cusClone1);
em.persist(cusClone2);
commitTransaction(em);
em.clear();
clearCache("fieldaccess");
cusIDs[0] = cusClone1.getCustomerId();
cusIDs[1] = cusClone2.getCustomerId();
try {
beginTransaction(em);
EntityManagerImpl entityManagerImpl = (EntityManagerImpl) em.getDelegate();
Query query1 = em.createNamedQuery("findAllCustomersFieldAccess");
returnedCustomers1 = query1.getResultList();
Query query2 = em.createQuery(ejbql1);
query2.setParameter("id", -10);
returnedCustomers2 = query2.getResultList();
// bug:4297903, check container policy failure
EJBQueryImpl query3 = (EJBQueryImpl) entityManagerImpl.createQuery(ejbql1);
ReadAllQuery readAllQuery = new ReadAllQuery(Customer.class);
MapContainerPolicy mapContainerPolicy = new MapContainerPolicy();
mapContainerPolicy.setContainerClass(HashMap.class);
mapContainerPolicy.setKeyName("hashCode");
readAllQuery.setContainerPolicy(mapContainerPolicy);
query3.setDatabaseQuery(readAllQuery);
try {
query3.getResultList();
} catch (PersistenceException exc) {
// QueryException.INVALID_CONTAINER_CLASS
expectedException1 = (QueryException) exc.getCause();
rollbackTransaction(em);
beginTransaction(em);
}
entityManagerImpl = (EntityManagerImpl) em.getDelegate();
// bug:4300879, check ReadObjectQuery fails
EJBQueryImpl query4 = (EJBQueryImpl) entityManagerImpl.createQuery(ejbql1);
query4.setParameter("id", -10);
ReadObjectQuery readObjectQuery2 = new ReadObjectQuery(Customer.class);
readObjectQuery2.setEJBQLString(ejbql1);
query4.setDatabaseQuery(readObjectQuery2);
query4.getResultList();
commitTransaction(em);
if (returnedCustomers1 == null || (returnedCustomers1.size() < 2)) {
fail("Not all customers were returned from findAllCustomers query ");
}
if (returnedCustomers2 == null || (returnedCustomers2.size() != 0)) {
fail("Customer from ReadObjectQuery was not returned using getResultCollection");
}
if (expectedException1 == null || (expectedException1.getErrorCode() != QueryException.INVALID_CONTAINER_CLASS)) {
fail("getResultCollection on query returning a hashtable did not throw expected INVALID_CONTAINER_CLASS QueryException");
}
beginTransaction(em);
Customer cus1 = em.find(Customer.class, cusIDs[0]);
em.remove(cus1);
Customer cus2 = em.find(Customer.class, cusIDs[1]);
em.remove(cus2);
commitTransaction(em);
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
}
}
Aggregations