* can call persist/remove/merge/refresh without a transaction.
* @hierarchy javasdk
* @userStory xyz
*/
public void testExtendedBehavior() {
TestEntity t = new TestEntity();
JPATestUtils.initializeTestEntity(t);
//new/transient
ParentTestEntity p = JPATestUtils.setMasterDetailRelationship(t);
t.setParent(p);
Assert.assertFalse(emExt.contains(t), "Entity is not transient.");
emExt.persist(p);
emExt.persist(t);
// Assert.assertNull(t.getId(), "Entity was already committed.");//fails
EntityTransaction tx = emExt.getTransaction();
tx.begin();
//managed
tx.commit();
Assert.assertTrue((emExt.contains(t) && emExt.contains(p)), "Entities are not managed.");
Assert.assertNotNull(t.getId(), "ID was not generated.");
Assert.assertNotNull(emExt.find(TestEntity.class, t.getId()), "The entity was not stored to the database.");
String firstTE = t.getId();
//now remove first TE and replace it with another TE
emExt.remove(t);
//another EM should still see the entity
// Assert.assertNotNull(em.find(TestEntity.class, firstTE), "The entity is already deleted.");//fails
TestEntity second = new TestEntity();
JPATestUtils.initializeTestEntity(second);
second.setParentMasterDetail(p);
second.setParent(p);
emExt.persist(second);
Assert.assertTrue(emExt.contains(second), "Second entity is managed.");
// Assert.assertNull(second.getId(), "Second was already committed.");//fails
tx = emExt.getTransaction();
tx.begin();
Assert.assertTrue((emExt.contains(second) && emExt.contains(p)), "Entities are not managed.");
// Assert.assertFalse(emExt.contains(t), "Removed entity is still managed.");//fails
tx.commit();
Assert.assertFalse(firstTE.equals(second.getId()), "The first entity was not replaced by a new.");
//now make changes on TE, then refresh (reverts changes) and merge outside of a tx
String origName = second.getName();
second.setName("testExtendedBehavior");
emExt.refresh(second);
Assert.assertEquals(second.getName(), origName, "Refresh did not reset the name.");
second.setName("newname");
// emExt.merge(second);//should make the new name persistent upon next commit //FAILS due to tx required - this is very odd
Assert.assertEquals(em.find(TestEntity.class, second.getId()).getName(), origName,
"Merge instantly committed the changes."); //should still see the old name
tx = emExt.getTransaction();
tx.begin();
tx.rollback();
Assert.assertEquals(emExt.find(TestEntity.class, second.getId()).getName(), origName, "Name update was not rolled back.");
}