use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class EntityManagerTest method testPersist.
/**
* Test of EntityManager.persist()
*/
public void testPersist() {
try {
EntityManager em = getEM();
if ("cassandra".equals(storeMgr.getStoreManagerKey())) {
em.setProperty("datanucleus.cassandra.enforceUniquenessInApplication", "true");
} else if ("hbase".equals(storeMgr.getStoreManagerKey())) {
em.setProperty("datanucleus.hbase.enforceUniquenessInApplication", "true");
}
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
p.setGlobalNum("First");
em.persist(p);
tx.commit();
try {
tx.begin();
Person p2 = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
p2.setGlobalNum("First");
em.persist(p2);
tx.commit();
fail("Allowed to persist same object twice!");
} catch (EntityExistsException eee) {
// Expected since the object had been persisted earlier
} catch (PersistenceException eee) {
// Expected since the object had been persisted earlier
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.begin();
Person p2 = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
p2.setGlobalNum("First");
em.persist(p2);
tx.commit();
fail("Allowed to persist object with same id twice");
} catch (EntityExistsException eee) {
// Either this or PersistenceException expected
} catch (PersistenceException pe) {
// Either this or EntityExistsException expected
}
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
em.close();
} finally {
clean(Person.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class EntityManagerTest method testDetachAccessUndetachedField.
/**
* Test of detaching and access of an undetached field.
*/
public void testDetachAccessUndetachedField() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
Person p = null;
Person.PK pk = null;
try {
tx.begin();
p = new Person(101, "Billy", "Nomates", "billy.nomates@nowhere.com");
p.getPhoneNumbers().put("Joey", new org.datanucleus.samples.annotations.models.company.PhoneNumber("Joey", "+44 123456789"));
em.persist(p);
tx.commit();
pk = p.getPK();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
em.close();
// Retrieve it
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
p = em.find(Person.class, pk);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
em.close();
em = getEM();
try {
p.getPhoneNumbers();
} catch (Throwable thr) {
assertTrue(thr instanceof IllegalAccessException);
}
} finally {
clean(Person.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class EntityManagerTest method testMerge.
/**
* Test of EntityManager.merge()
*/
public void testMerge() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
// Persist an object
tx.begin();
Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
em.persist(p1);
tx.commit();
// Merge the detached object
tx.begin();
em.merge(p1);
tx.commit();
// Merge a new object
tx.begin();
Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@jpox.com");
em.merge(p2);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception using merge " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
em.close();
} finally {
clean(Person.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class EntityManagerTest method testFind.
/**
* Test of EntityManager.find()
*/
public void testFind() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
em.persist(p1);
Account acct1 = new Account();
acct1.setUsername("fredf");
em.persist(acct1);
em.flush();
Person.PK pk = p1.getPK();
long acctId = acct1.getId();
tx.commit();
tx.begin();
// Find using IdClass instance
Person person = em.find(Person.class, pk);
assertEquals(p1.getFirstName(), person.getFirstName());
// Find using key value (but of slightly different type that needs conversion - DN extension)
Account acct = em.find(Account.class, Integer.valueOf("" + acctId));
assertEquals(acct1.getUsername(), acct.getUsername());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Account.class);
clean(Person.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class EntityManagerTest method testTransactionBeginTwice.
/**
* Test of calling tx.begin() twice in a row.
*/
public void testTransactionBeginTwice() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
try {
tx.begin();
fail("Expected IllegalStateException on tx.begin() twice but got nothing");
} catch (IllegalStateException ise) {
// Expected
} catch (Exception e) {
fail("Expected IllegalStateException but got " + e);
}
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
em.close();
} finally {
}
}
Aggregations