public class EntityBeanTest
{
private PersistenceUnitUtil persistenceUnitUtil;
private EntityManagerFactory entityManagerFactory;
private EntityManager entityManager;
private EntityTransaction entityTransaction;
private EntityBean parent;
@Test
public void checkEntityIdFromProxy()
{
EntityBean child = parent.getChild();
assertFalse(persistenceUnitUtil.isLoaded(child));
assertEquals(child.getId(), persistenceUnitUtil.getIdentifier(child));
}
@Before
public void setUp()
{
entityManagerFactory = Persistence.createEntityManagerFactory("manager1");
persistenceUnitUtil = entityManagerFactory.getPersistenceUnitUtil();
entityManager = entityManagerFactory.createEntityManager();
entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
EntityBean entityBean = new EntityBean();
entityBean.setChild(new EntityBean());
entityManager.persist(entityBean);
entityManager.flush();
Long id = entityBean.getId();
entityManager.clear();
parent = entityManager.find(EntityBean.class, id);
}
@After
public void tearDown()
{
entityTransaction.commit();
entityManager.close();
entityManagerFactory.close();
}
}