use of org.eclipse.persistence.testing.models.jpa.inherited.BlueLight in project eclipselink by eclipse-ee4j.
the class CriteriaQueryCastTestSuite method testDowncastSingleTableQueryKey.
public void testDowncastSingleTableQueryKey() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
BeerConsumer consumer = new BeerConsumer();
consumer.setName("John");
em.persist(consumer);
Blue blue = new Blue();
blue.setAlcoholContent(5f);
blue.setUniqueKey(new BigInteger("4531"));
em.persist(blue);
BlueLight blueLight = new BlueLight();
blueLight.setDiscount(10);
blueLight.setUniqueKey(new BigInteger("4533"));
em.persist(blueLight);
consumer.addBlueBeerToConsume(blueLight);
blueLight.setBeerConsumer(consumer);
consumer.addBlueBeerToConsume(blue);
consumer.addBlueBeerToConsume(blueLight);
consumer = new BeerConsumer();
consumer.setName("Frank");
em.persist(consumer);
blueLight = new BlueLight();
blueLight.setDiscount(5);
blueLight.setUniqueKey(new BigInteger("4532"));
em.persist(blueLight);
consumer.addBlueBeerToConsume(blueLight);
blueLight.setBeerConsumer(consumer);
em.flush();
clearCache();
em.clear();
// Query query = em.createQuery("Select b from BeerConsumer b join treat(b.blueBeersToConsume as BlueLight) bl where bl.discount = 10");
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<BeerConsumer> cq = qb.createQuery(BeerConsumer.class);
Root<BeerConsumer> root = cq.from(BeerConsumer.class);
Join<Object, BlueLight> blueLightJoin = qb.treat(root.join("blueBeersToConsume"), BlueLight.class);
cq.where(qb.equal(blueLightJoin.get("discount"), 10));
List<BeerConsumer> resultList = em.createQuery(cq).getResultList();
assertTrue("Incorrect results returned", resultList.size() == 1);
} finally {
if (this.isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.inherited.BlueLight in project eclipselink by eclipse-ee4j.
the class CriteriaQueryCastTestSuite method testDowncastSingleTableQueryKey.
public void testDowncastSingleTableQueryKey() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
BeerConsumer consumer = new BeerConsumer();
consumer.setName("John");
em.persist(consumer);
Blue blue = new Blue();
blue.setAlcoholContent(5f);
blue.setUniqueKey(new BigInteger("4531"));
em.persist(blue);
BlueLight blueLight = new BlueLight();
blueLight.setDiscount(10);
blueLight.setUniqueKey(new BigInteger("4533"));
em.persist(blueLight);
consumer.addBlueBeerToConsume(blueLight);
blueLight.setBeerConsumer(consumer);
consumer.addBlueBeerToConsume(blue);
consumer.addBlueBeerToConsume(blueLight);
consumer = new BeerConsumer();
consumer.setName("Frank");
em.persist(consumer);
blueLight = new BlueLight();
blueLight.setDiscount(5);
blueLight.setUniqueKey(new BigInteger("4532"));
em.persist(blueLight);
consumer.addBlueBeerToConsume(blueLight);
blueLight.setBeerConsumer(consumer);
em.flush();
clearCache();
em.clear();
// Query query = em.createQuery("Select b from BeerConsumer b join treat(b.blueBeersToConsume as BlueLight) bl where bl.discount = 10");
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<BeerConsumer> cq = qb.createQuery(BeerConsumer.class);
Root<BeerConsumer> root = cq.from(BeerConsumer.class);
Join blueLightJoin = qb.treat(root.join("blueBeersToConsume"), BlueLight.class);
cq.where(qb.equal(blueLightJoin.get("discount"), 10));
List resultList = em.createQuery(cq).getResultList();
assertTrue("Incorrect results returned", resultList.size() == 1);
} finally {
if (this.isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.inherited.BlueLight in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaSimpleTestSuiteBase method mapCastTest.
public void mapCastTest() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
BeerConsumer bc1 = new BeerConsumer();
bc1.setName("George");
em.persist(bc1);
Blue blue = new Blue();
blue.setUniqueKey(new BigInteger("1"));
em.persist(blue);
bc1.addBlueBeerToConsume(blue);
blue.setBeerConsumer(bc1);
BeerConsumer bc2 = new BeerConsumer();
bc2.setName("Scott");
em.persist(bc2);
BlueLight blueLight = new BlueLight();
blueLight.setDiscount(10);
blueLight.setUniqueKey(new BigInteger("2"));
em.persist(blueLight);
blueLight.setBeerConsumer(bc2);
bc2.addBlueBeerToConsume(blueLight);
em.flush();
em.clear();
clearCache();
ReadAllQuery query = new ReadAllQuery();
Expression selectionCriteria = new ExpressionBuilder().anyOf("blueBeersToConsume").treat(BlueLight.class).get("discount").equal(10);
query.setSelectionCriteria(selectionCriteria);
query.setReferenceClass(BeerConsumer.class);
query.dontUseDistinct();
Query jpaQuery = ((org.eclipse.persistence.internal.jpa.EntityManagerImpl) em.getDelegate()).createQuery(query);
List expectedResult = jpaQuery.getResultList();
clearCache();
em.clear();
// "SELECT e from Employee e join cast(e.project, LargeProject) p where p.budget = 1000
CriteriaBuilder qb1 = em.getCriteriaBuilder();
CriteriaQuery<BeerConsumer> cq1 = qb1.createQuery(BeerConsumer.class);
Root<BeerConsumer> root = wrapper.from(cq1, BeerConsumer.class);
Join<BeerConsumer, Blue> join = wrapper.join(root, BeerConsumer_blueBeersToConsume);
jakarta.persistence.criteria.Expression exp = wrapper.get((Path) join.as(BlueLight.class), BlueLight_discount);
cq1.where(qb1.equal(exp, 10));
List<BeerConsumer> result = em.createQuery(cq1).getResultList();
assertTrue("LargeProject cast failed.", comparer.compareObjects(result, expectedResult));
} finally {
if (isTransactionActive(em)) {
this.rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.inherited.BlueLight in project eclipselink by eclipse-ee4j.
the class InheritedModelJunitTest method testInheritedClone.
// Test the clone method works with LAZY attributes at multiple levels of an inheritance hierarchy
public void testInheritedClone() {
EntityManager em = createEntityManager();
beginTransaction(em);
SerialNumber serialNumber = new SerialNumber();
em.persist(serialNumber);
Alpine alpine = new Alpine(serialNumber);
alpine.setBestBeforeDate(Helper.dateFromYearMonthDate(2005, 8, 21));
alpine.setAlcoholContent(5.0);
alpine.setClassification(Alpine.Classification.BITTER);
alpine.addInspectionDate(new Date(System.currentTimeMillis()));
alpine.getBeerConsumer();
Alpine clone = null;
try {
clone = alpine.clone();
} catch (CloneNotSupportedException ex) {
fail("Caught CloneNotSupportedException " + ex);
}
BeerConsumer consumer = new BeerConsumer();
consumer.setName("Keith Alexander");
BlueLight blueLight = new BlueLight();
blueLight.setAlcoholContent(4.0f);
blueLight.setUniqueKey(new BigInteger((Long.valueOf(System.currentTimeMillis()).toString())));
em.persist(blueLight);
consumer.addBlueLightBeerToConsume(blueLight);
clone.setBeerConsumer(consumer);
consumer.addAlpineBeerToConsume(clone);
em.persist(clone);
em.persist(consumer);
if (alpine.getBeerConsumer() == clone.getBeerConsumer()) {
fail("Changing clone beerConsumer changed original.");
}
commitTransaction(em);
Integer alpineId = clone.getId();
Integer consumerId = consumer.getId();
BigDecimal blueLightId = blueLight.getId();
clearCache();
closeEntityManager(em);
em = createEntityManager();
beginTransaction(em);
alpine = em.find(Alpine.class, alpineId);
try {
clone = alpine.clone();
} catch (CloneNotSupportedException ex) {
fail("Caught CloneNotSupportedException " + ex);
}
consumer = new BeerConsumer();
consumer.setName("Frank Keith");
clone.setBeerConsumer(consumer);
consumer.addAlpineBeerToConsume(clone);
clone.getBeerConsumer();
if (alpine.getBeerConsumer() == clone.getBeerConsumer()) {
fail("Changing clone beerConsumer changed original.");
}
if (alpine.getBeerConsumer() == null) {
fail("Changing clone address reset original to null.");
}
if (clone.getBeerConsumer() != consumer) {
fail("Changing clone did not work.");
}
try {
consumer = em.find(BeerConsumer.class, consumerId);
BeerConsumer consumerClone = (BeerConsumer) consumer.clone();
Iterator<Alpine> alpineIterator = consumer.getAlpineBeersToConsume().iterator();
while (alpineIterator.hasNext()) {
alpine = alpineIterator.next();
assertTrue("The original beerConsumer has an alpine beer with an incorrect back pointer.", consumer == alpine.getBeerConsumer());
}
alpineIterator = consumerClone.getAlpineBeersToConsume().iterator();
while (alpineIterator.hasNext()) {
alpine = alpineIterator.next();
assertTrue("The cloned beerConsumer has an alpine beer with an incorrect back pointer.", consumerClone == alpine.getBeerConsumer());
}
Iterator<BlueLight> blueLightIterator = consumer.getBlueLightBeersToConsume().iterator();
while (blueLightIterator.hasNext()) {
blueLight = blueLightIterator.next();
assertTrue("The original beerConsumer has an BlueLight beer with an incorrect back pointer.", consumer == blueLight.getBeerConsumer());
}
blueLightIterator = consumerClone.getBlueLightBeersToConsume().iterator();
while (blueLightIterator.hasNext()) {
blueLight = blueLightIterator.next();
assertTrue("The cloned beerConsumer has an BlueLight beer with an incorrect back pointer.", consumerClone == blueLight.getBeerConsumer());
}
} catch (CloneNotSupportedException e) {
fail("Call to clone threw CloneNotSupportedException");
}
alpine = em.find(Alpine.class, clone.getId());
em.remove(alpine);
blueLight = em.find(BlueLight.class, blueLightId);
em.remove(blueLight);
consumer = em.find(BeerConsumer.class, consumerId);
em.remove(consumer);
commitTransaction(em);
closeEntityManager(em);
}
use of org.eclipse.persistence.testing.models.jpa.inherited.BlueLight in project eclipselink by eclipse-ee4j.
the class InheritedModelJunitTest method testCascadeRemove.
// BUG 227345
public void testCascadeRemove() {
BeerConsumer beerConsumer = null;
BlueLight blueLightPersisted = null;
BlueLight blueLightDetached = null;
EntityManager em = createEntityManager();
// Create and persist the beerConsumer
try {
beginTransaction(em);
beerConsumer = new BeerConsumer();
beerConsumer.setName("Beer Man");
blueLightPersisted = new BlueLight();
beerConsumer.getBlueLightBeersToConsume().add(blueLightPersisted);
blueLightPersisted.setBeerConsumer(beerConsumer);
em.persist(beerConsumer);
// Unique key must be set before commit.
blueLightPersisted.setUniqueKey(blueLightPersisted.getId().toBigInteger());
// They should be known by the EM
assertTrue(em.contains(beerConsumer));
assertTrue(em.contains(blueLightPersisted));
commitTransaction(em);
} catch (RuntimeException e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw e;
}
// Create BlueLightDetached and manage the relations
try {
beginTransaction(em);
blueLightDetached = new BlueLight();
blueLightDetached.setUniqueKey(new BigDecimal(blueLightPersisted.getUniqueKey().intValue() + 1).toBigInteger());
// Set the pointers
beerConsumer.getBlueLightBeersToConsume().add(blueLightDetached);
blueLightDetached.setBeerConsumer(beerConsumer);
// And now remove the beer consumer. The remove-operation should cascade
if (isOnServer()) {
beerConsumer = em.find(BeerConsumer.class, m_beerConsumerId);
}
em.remove(beerConsumer);
// It's o.k. should be detached
assertFalse(em.contains(blueLightDetached));
} catch (RuntimeException e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw e;
}
try {
commitTransaction(em);
} catch (RuntimeException e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
fail("An exception was caught during the remove of the BeerConsumer: [" + e.getMessage() + "]");
}
// Ensure neither the beer consumer nor the blue light
// persisted/detached are available.
assertFalse("The beer consumer was not removed", em.contains(beerConsumer));
assertFalse("The blue light persisted was not removed even though the its owning beer comsumer was removed", em.contains(blueLightPersisted));
assertFalse("The blue light detached was persisted even though the its owning beer comsumer was removed", em.contains(blueLightDetached));
closeEntityManager(em);
}
Aggregations