use of org.eclipse.persistence.internal.queries.MapContainerPolicy in project eclipselink by eclipse-ee4j.
the class XMLVariableXPathCollectionMapping method useMapClass.
public void useMapClass(String concreteContainerClassName) {
MapContainerPolicy policy = new MapContainerPolicy(concreteContainerClassName);
this.setContainerPolicy(policy);
}
use of org.eclipse.persistence.internal.queries.MapContainerPolicy in project eclipselink by eclipse-ee4j.
the class XMLCollectionReferenceMapping method useMapClassName.
/**
* PUBLIC:
* Configure the mapping to use an instance of the specified container class
* to hold the target objects. The key used to index the value in the Map
* is the value returned by a call to the specified zero-argument method.
* The method must be implemented by the class (or a superclass) of the
* value to be inserted into the Map.
* <p>jdk1.2.x: The container class must implement (directly or indirectly) the Map interface.
* <p>jdk1.1.x: The container class must be a subclass of Hashtable.
* <p>The referenceClass must be set before calling this method.
*/
@Override
public void useMapClassName(String concreteContainerClass, String methodName) {
// the reference class has to be specified before coming here
if (this.getReferenceClass() == null) {
throw DescriptorException.referenceClassNotSpecified(this);
}
MapContainerPolicy policy = new MapContainerPolicy(concreteContainerClass);
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 testGetSingleResultTest.
/*
* Tests using the 'getSingleResult' api on a Query object obtained from the
* EntityManager Tests fixes for bugs 4202835 and 4301674
*
* modified for changes in bug:4628215 (EntityNotFoundException)
* EntityNotFoundException changed to NoResultException as per new spec
*/
public void testGetSingleResultTest() {
// used for verification
Customer returnedCustomer1, returnedCustomer2 = null;
NonUniqueResultException expectedException1 = null;
NoResultException expectedException2 = null;
String searchString = "notAnItemName";
Integer[] cusIDs = new Integer[3];
Customer cusClone1 = RelationshipsExamples.customerExample1();
Customer cusClone2 = RelationshipsExamples.customerExample2();
EntityManager em = createEntityManager("fieldaccess");
try {
beginTransaction(em);
em.persist(cusClone1);
em.persist(cusClone2);
commitTransaction(em);
clearCache("fieldaccess");
cusIDs[0] = cusClone1.getCustomerId();
cusIDs[1] = cusClone2.getCustomerId();
beginTransaction(em);
try {
returnedCustomer1 = (Customer) em.createNamedQuery("findAllCustomersFieldAccess").getSingleResult();
} catch (NonUniqueResultException exceptionExpected1) {
expectedException1 = exceptionExpected1;
}
try {
// should be no Items to find, which should cause an
// NoResultException
Query query1 = em.createNamedQuery("findAllFieldAccessItemsByName");
Item item = (Item) query1.setParameter(1, searchString).getSingleResult();
item.toString();
} catch (NoResultException exceptionExpected2) {
expectedException2 = exceptionExpected2;
}
// bug 4301674 test
EJBQueryImpl query2 = (EJBQueryImpl) em.createNamedQuery("findAllCustomersFieldAccess");
ReadAllQuery readAllQuery = new ReadAllQuery(Customer.class);
MapContainerPolicy mapContainerPolicy = new MapContainerPolicy();
mapContainerPolicy.setContainerClass(HashMap.class);
mapContainerPolicy.setKeyName("hashCode");
readAllQuery.setContainerPolicy(mapContainerPolicy);
query2.setDatabaseQuery(readAllQuery);
Map result = (Map) query2.getSingleResult();
result.toString();
// check for single result found.
Query query3 = em.createQuery("SELECT OBJECT(thecust) FROM FieldAccessCustomer thecust WHERE thecust.customerId = :id");
returnedCustomer1 = (Customer) query3.setParameter("id", cusIDs[0]).getSingleResult();
// check for single result using a ReadObjectQuery (tests previous
// fix for 4202835)
EJBQueryImpl query4 = (EJBQueryImpl) em.createQuery("SELECT OBJECT(thecust) FROM FieldAccessCustomer thecust WHERE thecust.customerId = :id");
query4.setParameter("id", cusIDs[0]);
ReadObjectQuery readObjectQuery = new ReadObjectQuery(Customer.class);
readObjectQuery.setEJBQLString("SELECT OBJECT(thecust) FROM FieldAccessCustomer thecust WHERE thecust.customerId = :id");
query4.setDatabaseQuery(readObjectQuery);
returnedCustomer2 = (Customer) query4.getSingleResult();
commitTransaction(em);
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);
if (expectedException1 == null) {
fail("getSingelResult on query returning multiple values did not throw a NonUniqueResultException");
}
if (expectedException2 == null) {
fail("getSingelResult on query returning multiple values did not throw an NoResultException");
}
if (returnedCustomer1 == null || (!returnedCustomer1.getCustomerId().equals(cusIDs[0]))) {
fail("Incorrect Single Customer returned, found: " + returnedCustomer1);
}
if (returnedCustomer2 == null || (!returnedCustomer2.getCustomerId().equals(cusIDs[0]))) {
fail("Incorrect Single Customer returned, found: " + returnedCustomer2);
}
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
}
}
use of org.eclipse.persistence.internal.queries.MapContainerPolicy in project eclipselink by eclipse-ee4j.
the class ObjectPersistenceRuntimeXMLProject method buildDatasourceLoginDescriptor.
public ClassDescriptor buildDatasourceLoginDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(DatasourceLogin.class);
descriptor.setDefaultRootElement("login");
descriptor.getInheritancePolicy().setClassIndicatorField(new XMLField("@xsi:type"));
descriptor.getInheritancePolicy().addClassIndicator(DatabaseLogin.class, getPrimaryNamespaceXPath() + "database-login");
descriptor.getInheritancePolicy().addClassIndicator(EISLogin.class, getPrimaryNamespaceXPath() + "eis-login");
descriptor.getInheritancePolicy().addClassIndicator(XMLLogin.class, getPrimaryNamespaceXPath() + "xml-login");
XMLDirectMapping platformMapping = new XMLDirectMapping();
platformMapping.setAttributeName("platform");
platformMapping.setGetMethodName("getDatasourcePlatform");
platformMapping.setSetMethodName("usePlatform");
platformMapping.setConverter(new Converter() {
private Map<String, String> platformList;
private String oldPrefix = "oracle.toplink.";
private String newPrefix = "org.eclipse.persistence.";
private String oldOxmPrefix = oldPrefix + "ox.";
private String newOxmPrefix = newPrefix + "oxm.";
@Override
public Object convertObjectValueToDataValue(Object objectValue, Session session) {
if (objectValue == null) {
return null;
}
return objectValue.getClass().getName();
}
@Override
public Object convertDataValueToObjectValue(Object fieldValue, Session session) {
if (fieldValue == null) {
return null;
}
if (((String) fieldValue).startsWith(oldPrefix)) {
if (((String) fieldValue).startsWith(oldOxmPrefix)) {
fieldValue = ((String) fieldValue).replaceFirst(oldOxmPrefix, newOxmPrefix);
} else {
fieldValue = ((String) fieldValue).replaceFirst(oldPrefix, newPrefix);
}
}
// convert deprecated platforms to new platforms
String result = platformList.get(fieldValue);
if (result != null) {
fieldValue = result;
}
Object attributeValue;
Class<?> attributeClass = session.getDatasourcePlatform().convertObject(fieldValue, ClassConstants.CLASS);
try {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
attributeValue = AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(attributeClass));
} catch (PrivilegedActionException exception) {
throw ConversionException.couldNotBeConverted(fieldValue, attributeClass, exception.getException());
}
} else {
attributeValue = PrivilegedAccessHelper.newInstanceFromClass(attributeClass);
}
} catch (Exception exception) {
throw ConversionException.couldNotBeConverted(fieldValue, attributeClass, exception);
}
return attributeValue;
}
@Override
public boolean isMutable() {
return false;
}
@Override
public void initialize(DatabaseMapping mapping, Session session) {
this.platformList = new HashMap<>();
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.AccessPlatform", "org.eclipse.persistence.platform.database.AccessPlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.AttunityPlatform", "org.eclipse.persistence.platform.database.AttunityPlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.CloudscapePlatform", "org.eclipse.persistence.platform.database.CloudscapePlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.DatabasePlatform", "org.eclipse.persistence.platform.database.DatabasePlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.DB2MainframePlatform", "org.eclipse.persistence.platform.database.DB2MainframePlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.DB2Platform", "org.eclipse.persistence.platform.database.DB2Platform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.DBasePlatform", "org.eclipse.persistence.platform.database.DBasePlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.HSQLPlatform", "org.eclipse.persistence.platform.database.HSQLPlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.InformixPlatform", "org.eclipse.persistence.platform.database.InformixPlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.OraclePlatform", "org.eclipse.persistence.platform.database.OraclePlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.PointBasePlatform", "org.eclipse.persistence.platform.database.PointBasePlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.SQLAnyWherePlatform", "org.eclipse.persistence.platform.database.SQLAnywherePlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.SQLServerPlatform", "org.eclipse.persistence.platform.database.SQLServerPlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.SybasePlatform", "org.eclipse.persistence.platform.database.SybasePlatform");
this.platformList.put("org.eclipse.persistence.oraclespecific.Oracle8Platform", "org.eclipse.persistence.platform.database.oracle.Oracle8Platform");
this.platformList.put("org.eclipse.persistence.oraclespecific.Oracle9Platform", "org.eclipse.persistence.platform.database.oracle.Oracle9Platform");
this.platformList.put("org.eclipse.persistence.platform.database.SQLAnyWherePlatform", "org.eclipse.persistence.platform.database.SQLAnywherePlatform");
// CR#... Mapping must also have the field classification.
if (mapping.isDirectToFieldMapping()) {
AbstractDirectMapping directMapping = (AbstractDirectMapping) mapping;
// Allow user to specify field type to override computed value. (i.e. blob, nchar)
if (directMapping.getFieldClassification() == null) {
directMapping.setFieldClassification(ClassConstants.STRING);
}
}
}
});
platformMapping.setXPath(getPrimaryNamespaceXPath() + "platform-class/text()");
descriptor.addMapping(platformMapping);
XMLDirectMapping userNameMapping = new XMLDirectMapping();
userNameMapping.setAttributeName("userName");
userNameMapping.setGetMethodName("getUserName");
userNameMapping.setSetMethodName("setUserName");
userNameMapping.setXPath(getPrimaryNamespaceXPath() + "user-name/text()");
descriptor.addMapping(userNameMapping);
XMLDirectMapping passwordMapping = new XMLDirectMapping();
passwordMapping.setAttributeName("password");
passwordMapping.setGetMethodName("getPassword");
passwordMapping.setSetMethodName("setEncryptedPassword");
passwordMapping.setXPath(getPrimaryNamespaceXPath() + "password/text()");
descriptor.addMapping(passwordMapping);
XMLDirectMapping usesExternalConnectionPoolingMapping = new XMLDirectMapping();
usesExternalConnectionPoolingMapping.setAttributeName("usesExternalConnectionPooling");
usesExternalConnectionPoolingMapping.setGetMethodName("shouldUseExternalConnectionPooling");
usesExternalConnectionPoolingMapping.setSetMethodName("setUsesExternalConnectionPooling");
usesExternalConnectionPoolingMapping.setXPath(getPrimaryNamespaceXPath() + "external-connection-pooling/text()");
usesExternalConnectionPoolingMapping.setNullValue(Boolean.FALSE);
descriptor.addMapping(usesExternalConnectionPoolingMapping);
XMLDirectMapping usesExternalTransactionControllerMapping = new XMLDirectMapping();
usesExternalTransactionControllerMapping.setAttributeName("usesExternalTransactionController");
usesExternalTransactionControllerMapping.setGetMethodName("shouldUseExternalTransactionController");
usesExternalTransactionControllerMapping.setSetMethodName("setUsesExternalTransactionController");
usesExternalTransactionControllerMapping.setXPath(getPrimaryNamespaceXPath() + "external-transaction-controller/text()");
usesExternalTransactionControllerMapping.setNullValue(Boolean.FALSE);
descriptor.addMapping(usesExternalTransactionControllerMapping);
XMLCompositeObjectMapping defaultSequenceMapping = new XMLCompositeObjectMapping();
defaultSequenceMapping.setAttributeName("defaultSequence");
defaultSequenceMapping.setSetMethodName("setDefaultSequence");
defaultSequenceMapping.setGetMethodName("getDefaultSequenceToWrite");
defaultSequenceMapping.setReferenceClass(Sequence.class);
defaultSequenceMapping.setXPath(getPrimaryNamespaceXPath() + "sequencing/" + getPrimaryNamespaceXPath() + "default-sequence");
descriptor.addMapping(defaultSequenceMapping);
XMLCompositeCollectionMapping sequencesMapping = new XMLCompositeCollectionMapping();
MapContainerPolicy containerPolicy = new MapContainerPolicy(HashMap.class);
containerPolicy.setKeyName("name", Sequence.class.getName());
sequencesMapping.setContainerPolicy(containerPolicy);
sequencesMapping.setAttributeName("sequences");
sequencesMapping.setSetMethodName("setSequences");
sequencesMapping.setGetMethodName("getSequencesToWrite");
sequencesMapping.setReferenceClass(Sequence.class);
sequencesMapping.setXPath(getPrimaryNamespaceXPath() + "sequencing/" + getPrimaryNamespaceXPath() + "sequences/" + getPrimaryNamespaceXPath() + "sequence");
descriptor.addMapping(sequencesMapping);
return descriptor;
}
use of org.eclipse.persistence.internal.queries.MapContainerPolicy in project eclipselink by eclipse-ee4j.
the class RelationshipModelJUnitTestSuite method testGetResultCollectionTest.
/*
* Tests using the 'getResultCollection' 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 testGetResultCollectionTest() {
Collection returnedCustomers1, returnedCustomers2;
QueryException expectedException1 = null;
String ejbql1 = "SELECT OBJECT(thecust) FROM Customer thecust WHERE thecust.customerId = :id";
Integer[] cusIDs = new Integer[3];
Customer cusClone1 = RelationshipsExamples.customerExample1();
Customer cusClone2 = RelationshipsExamples.customerExample2();
EntityManager em = createEntityManager();
beginTransaction(em);
em.persist(cusClone1);
em.persist(cusClone2);
commitTransaction(em);
em.clear();
clearCache();
cusIDs[0] = cusClone1.getCustomerId();
cusIDs[1] = cusClone2.getCustomerId();
try {
beginTransaction(em);
EntityManagerImpl entityManagerImpl = (EntityManagerImpl) em.getDelegate();
EJBQueryImpl query1 = (EJBQueryImpl) entityManagerImpl.createNamedQuery("findAllCustomers");
returnedCustomers1 = query1.getResultCollection();
EJBQueryImpl query2 = (EJBQueryImpl) entityManagerImpl.createQuery(ejbql1);
query2.setParameter("id", -10);
returnedCustomers2 = query2.getResultCollection();
// 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.getResultCollection();
} 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.getResultCollection();
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