use of org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer 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.BeerConsumer in project eclipselink by eclipse-ee4j.
the class InheritedModelJunitTest method testVersionUpdateOnElementCollectionChange.
public void testVersionUpdateOnElementCollectionChange() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
BeerConsumer beerConsumer = (BeerConsumer) em.createQuery("select b from BeerConsumer b join b.redStripes r").getResultList().get(0);
int currentVersion = beerConsumer.getVersion();
beerConsumer.getRedStripes().put("version", new RedStripe(Double.valueOf("343")));
commitTransaction(em);
assertTrue("Did not increment version for change to element collection", beerConsumer.getVersion() == ++currentVersion);
} catch (RuntimeException e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
// Re-throw exception to ensure stacktrace appears in test result.
throw e;
}
closeEntityManager(em);
}
use of org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer in project eclipselink by eclipse-ee4j.
the class InheritedModelJunitTest method testBecksBeerConsumer.
public void testBecksBeerConsumer() {
EntityManager em = createEntityManager();
beginTransaction(em);
BeerConsumer initialBC = new BeerConsumer();
int beerConsumerId = 0;
try {
Becks becks1 = new Becks();
becks1.setAlcoholContent(5.1);
BecksTag becksTag1 = new BecksTag();
becksTag1.setCallNumber("0A.789");
em.persist(becksTag1);
Becks becks2 = new Becks();
becks2.setAlcoholContent(5.1);
BecksTag becksTag2 = new BecksTag();
becksTag2.setCallNumber("BX.521");
em.persist(becksTag2);
Becks becks3 = new Becks();
becks3.setAlcoholContent(5.1);
BecksTag becksTag3 = new BecksTag();
becksTag3.setCallNumber("UY.429");
em.persist(becksTag3);
initialBC.setName("Becks Consumer");
initialBC.addBecksBeerToConsume(becks1, becksTag1);
initialBC.addBecksBeerToConsume(becks2, becksTag2);
initialBC.addBecksBeerToConsume(becks3, becksTag3);
em.persist(initialBC);
beerConsumerId = initialBC.getId();
commitTransaction(em);
} catch (RuntimeException e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
fail("An exception was caught during create operation: [" + e.getMessage() + "]");
}
closeEntityManager(em);
clearCache();
em = createEntityManager();
BeerConsumer refreshedBC = em.find(BeerConsumer.class, beerConsumerId);
assertTrue("The beer consumer read back did not match the original", getServerSession().compareObjects(initialBC, refreshedBC));
}
use of org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer in project eclipselink by eclipse-ee4j.
the class InheritedModelJunitTest method testEmbeddableAggregateCollectionAndAggregate.
// Bug 334175
public void testEmbeddableAggregateCollectionAndAggregate() {
EntityManager em = createEntityManager();
beginTransaction(em);
BeerConsumer consumer = new BeerConsumer();
consumer.setName("Lionel");
RedStripe rs = new RedStripe();
rs.setAlcoholContent(4.5);
consumer.addRedStripeByAlcoholContent(rs);
em.persist(consumer);
Official official = new Official();
official.setName("George");
rs = new RedStripe();
rs.setAlcoholContent(4.6);
official.setLastRedStripeConsumed(rs);
em.persist(official);
em.flush();
em.clear();
clearCache();
consumer = em.find(BeerConsumer.class, consumer.getId());
assertNotNull("BeerConsumer had null red stripes.", consumer.getRedStripes());
assertTrue("BeerConsumer had wrong number of red stripes.", consumer.getRedStripesByAlcoholContent().size() == 1);
assertTrue("BeerConsumer had wrong red stripe.", consumer.getRedStripesByAlcoholContent().get(4.5) != null);
official = em.find(Official.class, official.getId());
assertNotNull("Official had null red stripe.", official.getLastRedStripeConsumed());
assertTrue("Official had the wrong red stripe", official.getLastRedStripeConsumed().getAlcoholContent() == 4.6);
rollbackTransaction(em);
}
use of org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer in project eclipselink by eclipse-ee4j.
the class InheritedModelJunitTest method testMapOrphanRemoval.
public void testMapOrphanRemoval() {
EntityManager em = createEntityManager();
beginTransaction(em);
BeerConsumer initialBC = new BeerConsumer();
int beerConsumerId = 0;
try {
Becks becks1 = new Becks();
becks1.setAlcoholContent(5.1);
BecksTag becksTag1 = new BecksTag();
becksTag1.setCallNumber("0A.789");
em.persist(becksTag1);
initialBC.setName("Becks Consumer");
initialBC.addBecksBeerToConsume(becks1, becksTag1);
em.persist(initialBC);
beerConsumerId = initialBC.getId();
em.flush();
clearCache();
BeerConsumer refreshedBC = em.find(BeerConsumer.class, beerConsumerId);
refreshedBC.getBecksBeersToConsume().remove(becksTag1);
em.flush();
clearCache();
becksTag1 = em.find(BecksTag.class, becksTag1.getId());
assertTrue("Key was deleted when it should not be.", becksTag1 != null);
becks1 = em.find(Becks.class, becks1.getId());
assertTrue("Orphan removal did not remove the orphan", becks1 == null);
rollbackTransaction(em);
} catch (RuntimeException e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
fail("An exception was caught during create operation: [" + e.getMessage() + "]");
}
}
Aggregations