use of org.datanucleus.samples.annotations.versioned.VersionedPerson 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.versioned.VersionedPerson in project tests by datanucleus.
the class EntityManagerTest method testLockOptimisticForceIncrement.
/**
* Test of lock type OPTIMISTIC_FORCE_INCREMENT
*/
public void testLockOptimisticForceIncrement() {
try {
// Persist an object
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
VersionedPerson p = new VersionedPerson(1, 1);
em.persist(p);
tx.commit();
assertEquals(1, p.getVersion());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
emf.getCache().evictAll();
// Retrieve the object and lock it with OPTIMISTIC_FORCE_INCREMENT
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
VersionedPerson p = em.find(VersionedPerson.class, 1);
em.lock(p, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
tx.commit();
assertEquals(2, p.getVersion());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(VersionedPerson.class);
}
}
use of org.datanucleus.samples.annotations.versioned.VersionedPerson in project tests by datanucleus.
the class EntityManagerTest method testVersionedObject.
/**
* Test of persistence/retrieval of object with version.
*/
public void testVersionedObject() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
VersionedPerson p = new VersionedPerson(1, 1);
em.persist(p);
tx.commit();
assertEquals(1, p.getVersion());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
emf.getCache().evictAll();
// Merge the same object, but transient
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
VersionedPerson p = new VersionedPerson(1, 1);
p = em.merge(p);
tx.commit();
assertEquals(2, p.getVersion());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(VersionedEmployee.class);
clean(VersionedPerson.class);
}
}
Aggregations