use of org.datanucleus.samples.annotations.models.company.Person in project tests by datanucleus.
the class EntityManagerFactoryTest method testPersistenceUnitUtilGetIdentifier.
/**
* Test for emf.getPersistenceUnitUtil.getIdentifier() method
*/
public void testPersistenceUnitUtilGetIdentifier() {
try {
PersistenceUnitUtil util = emf.getPersistenceUnitUtil();
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
p.setGlobalNum("First");
em.persist(p);
assertTrue(util.getIdentifier(p) instanceof Person.PK);
VersionedPerson vp = new VersionedPerson(1, 1);
em.persist(vp);
Object vpId = util.getIdentifier(vp);
assertTrue(vpId instanceof Long && ((Long) vpId) == 1);
tx.rollback();
} catch (Exception e) {
LOG.error(">> Exception on persist before serialisation", e);
fail("Exception on persist : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
// No cleanup needed
}
}
use of org.datanucleus.samples.annotations.models.company.Person in project tests by datanucleus.
the class EntityManagerTest method testPersistThenDetach.
/**
* Test of EntityManager.persist() and detach().
*/
public void testPersistThenDetach() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
p.setGlobalNum("First");
em.persist(p);
em.detach(p);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
em.close();
} finally {
clean(Person.class);
}
}
use of org.datanucleus.samples.annotations.models.company.Person 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 org.datanucleus.samples.annotations.models.company.Person 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 org.datanucleus.samples.annotations.models.company.Person 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);
}
}
Aggregations