org.springframework.orm.jpa.domain.Person Java Examples

The following examples show how to use org.springframework.orm.jpa.domain.Person. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContainerManagedEntityManagerIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testReuseInNewTransaction() {
	EntityManager em = createContainerManagedEntityManager();
	doInstantiateAndSave(em);
	endTransaction();

	//assertFalse(em.getTransaction().isActive());

	startNewTransaction();
	// Call any method: should cause automatic tx invocation
	assertFalse(em.contains(new Person()));
	//assertTrue(em.getTransaction().isActive());

	doInstantiateAndSave(em);
	setComplete();
	endTransaction();	// Should rollback
	assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));

	// Now clean up the database
	deleteFromTables("person");
}
 
Example #2
Source File: ContainerManagedEntityManagerIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testEntityManagerProxyIsProxy() {
	EntityManager em = createContainerManagedEntityManager();
	assertTrue(Proxy.isProxyClass(em.getClass()));
	Query q = em.createQuery("select p from Person as p");
	List<Person> people = q.getResultList();
	assertTrue(people.isEmpty());

	assertTrue("Should be open to start with", em.isOpen());
	try {
		em.close();
		fail("Close should not work on container managed EM");
	}
	catch (IllegalStateException ex) {
		// OK
	}
	assertTrue(em.isOpen());
}
 
Example #3
Source File: AbstractContainerEntityManagerFactoryIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersonsNotTransactional() {
	endTransaction();

	EntityManager em = entityManagerFactory.createEntityManager();
	Query q = em.createQuery("select p from Person as p");
	List<Person> people = q.getResultList();
	assertEquals(0, people.size());
	try {
		assertNull(q.getSingleResult());
		fail("Should have thrown NoResultException");
	}
	catch (NoResultException ex) {
		// expected
	}
}
 
Example #4
Source File: ContainerManagedEntityManagerIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testReuseInNewTransaction() {
	EntityManager em = createContainerManagedEntityManager();
	doInstantiateAndSave(em);
	endTransaction();

	//assertFalse(em.getTransaction().isActive());

	startNewTransaction();
	// Call any method: should cause automatic tx invocation
	assertFalse(em.contains(new Person()));
	//assertTrue(em.getTransaction().isActive());

	doInstantiateAndSave(em);
	setComplete();
	endTransaction();	// Should rollback
	assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));

	// Now clean up the database
	deleteFromTables("person");
}
 
Example #5
Source File: AbstractContainerEntityManagerFactoryIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testLazyLoading() {
	try {
		Person tony = new Person();
		tony.setFirstName("Tony");
		tony.setLastName("Blair");
		tony.setDriversLicense(new DriversLicense("8439DK"));
		sharedEntityManager.persist(tony);
		setComplete();
		endTransaction();

		startNewTransaction();
		sharedEntityManager.clear();
		Person newTony = entityManagerFactory.createEntityManager().getReference(Person.class, tony.getId());
		assertNotSame(newTony, tony);
		endTransaction();

		assertNotNull(newTony.getDriversLicense());

		newTony.getDriversLicense().getSerialNumber();
	}
	finally {
		deleteFromTables("person", "drivers_license");
	}
}
 
Example #6
Source File: AbstractContainerEntityManagerFactoryIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public void testLazyLoading() {
	try {
		Person tony = new Person();
		tony.setFirstName("Tony");
		tony.setLastName("Blair");
		tony.setDriversLicense(new DriversLicense("8439DK"));
		sharedEntityManager.persist(tony);
		setComplete();
		endTransaction();

		startNewTransaction();
		sharedEntityManager.clear();
		Person newTony = entityManagerFactory.createEntityManager().getReference(Person.class, tony.getId());
		assertNotSame(newTony, tony);
		endTransaction();

		assertNotNull(newTony.getDriversLicense());

		newTony.getDriversLicense().getSerialNumber();
	}
	finally {
		deleteFromTables("person", "drivers_license");
	}
}
 
Example #7
Source File: ContainerManagedEntityManagerIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testEntityManagerProxyIsProxy() {
	EntityManager em = createContainerManagedEntityManager();
	assertTrue(Proxy.isProxyClass(em.getClass()));
	Query q = em.createQuery("select p from Person as p");
	List<Person> people = q.getResultList();
	assertTrue(people.isEmpty());

	assertTrue("Should be open to start with", em.isOpen());
	try {
		em.close();
		fail("Close should not work on container managed EM");
	}
	catch (IllegalStateException ex) {
		// OK
	}
	assertTrue(em.isOpen());
}
 
Example #8
Source File: AbstractContainerEntityManagerFactoryIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersonsNotTransactional() {
	endTransaction();

	EntityManager em = entityManagerFactory.createEntityManager();
	Query q = em.createQuery("select p from Person as p");
	List<Person> people = q.getResultList();
	assertEquals(0, people.size());
	try {
		assertNull(q.getSingleResult());
		fail("Should have thrown NoResultException");
	}
	catch (NoResultException ex) {
		// expected
	}
}
 
Example #9
Source File: ContainerManagedEntityManagerIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public void testReuseInNewTransaction() {
	EntityManager em = createContainerManagedEntityManager();
	doInstantiateAndSave(em);
	endTransaction();

	//assertFalse(em.getTransaction().isActive());

	startNewTransaction();
	// Call any method: should cause automatic tx invocation
	assertFalse(em.contains(new Person()));
	//assertTrue(em.getTransaction().isActive());

	doInstantiateAndSave(em);
	setComplete();
	endTransaction();	// Should rollback
	assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));

	// Now clean up the database
	deleteFromTables("person");
}
 
Example #10
Source File: ContainerManagedEntityManagerIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public void testEntityManagerProxyIsProxy() {
	EntityManager em = createContainerManagedEntityManager();
	assertTrue(Proxy.isProxyClass(em.getClass()));
	Query q = em.createQuery("select p from Person as p");
	List<Person> people = q.getResultList();
	assertTrue(people.isEmpty());

	assertTrue("Should be open to start with", em.isOpen());
	try {
		em.close();
		fail("Close should not work on container managed EM");
	}
	catch (IllegalStateException ex) {
		// Ok
	}
	assertTrue(em.isOpen());
}
 
Example #11
Source File: AbstractContainerEntityManagerFactoryIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testLazyLoading() {
	try {
		Person tony = new Person();
		tony.setFirstName("Tony");
		tony.setLastName("Blair");
		tony.setDriversLicense(new DriversLicense("8439DK"));
		sharedEntityManager.persist(tony);
		setComplete();
		endTransaction();

		startNewTransaction();
		sharedEntityManager.clear();
		Person newTony = entityManagerFactory.createEntityManager().getReference(Person.class, tony.getId());
		assertNotSame(newTony, tony);
		endTransaction();

		assertNotNull(newTony.getDriversLicense());

		newTony.getDriversLicense().getSerialNumber();
	}
	finally {
		deleteFromTables("person", "drivers_license");
	}
}
 
Example #12
Source File: ApplicationManagedEntityManagerIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public void doInstantiateAndSave(EntityManager em) {
	testStateClean();
	Person p = new Person();

	p.setFirstName("Tony");
	p.setLastName("Blair");
	em.persist(p);

	em.flush();
	assertEquals("1 row must have been inserted", 1, countRowsInTable(em, "person"));
}
 
Example #13
Source File: ContainerManagedEntityManagerIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public void doInstantiateAndSave(EntityManager em) {
	assertEquals("Should be no people from previous transactions", 0, countRowsInTable(em, "person"));
	Person p = new Person();

	p.setFirstName("Tony");
	p.setLastName("Blair");
	em.persist(p);

	em.flush();
	assertEquals("1 row must have been inserted", 1, countRowsInTable(em, "person"));
}
 
Example #14
Source File: ContainerManagedEntityManagerIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected void doInstantiateAndSave(EntityManager em) {
	assertEquals("Should be no people from previous transactions", 0, countRowsInTable(em, "person"));
	Person p = new Person();

	p.setFirstName("Tony");
	p.setLastName("Blair");
	em.persist(p);

	em.flush();
	assertEquals("1 row must have been inserted", 1, countRowsInTable(em, "person"));
}
 
Example #15
Source File: HibernateNativeEntityManagerFactoryIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testEntityListener() {
	String firstName = "Tony";
	insertPerson(firstName);

	List<Person> people = sharedEntityManager.createQuery("select p from Person as p").getResultList();
	assertEquals(1, people.size());
	assertEquals(firstName, people.get(0).getFirstName());
	assertSame(applicationContext, people.get(0).postLoaded);
}
 
Example #16
Source File: HibernateNativeEntityManagerFactoryIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testCurrentSession() {
	String firstName = "Tony";
	insertPerson(firstName);

	Query q = sessionFactory.getCurrentSession().createQuery("select p from Person as p");
	List<Person> people = q.getResultList();
	assertEquals(1, people.size());
	assertEquals(firstName, people.get(0).getFirstName());
	assertSame(applicationContext, people.get(0).postLoaded);
}
 
Example #17
Source File: ApplicationManagedEntityManagerIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public void testEntityManagerProxyIsProxy() {
	EntityManager em = entityManagerFactory.createEntityManager();
	assertTrue(Proxy.isProxyClass(em.getClass()));
	Query q = em.createQuery("select p from Person as p");
	List<Person> people = q.getResultList();
	assertNotNull(people);

	assertTrue("Should be open to start with", em.isOpen());
	em.close();
	assertFalse("Close should work on application managed EM", em.isOpen());
}
 
Example #18
Source File: AbstractContainerEntityManagerFactoryIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void testQueryNoPersons() {
	EntityManager em = entityManagerFactory.createEntityManager();
	Query q = em.createQuery("select p from Person as p");
	List<Person> people = q.getResultList();
	assertEquals(0, people.size());
	try {
		assertNull(q.getSingleResult());
		fail("Should have thrown NoResultException");
	}
	catch (NoResultException ex) {
		// expected
	}
}
 
Example #19
Source File: ApplicationManagedEntityManagerIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public void testReuseInNewTransaction() {
	EntityManager em = entityManagerFactory.createEntityManager();
	em.joinTransaction();

	doInstantiateAndSave(em);
	endTransaction();

	assertFalse(em.getTransaction().isActive());

	startNewTransaction();
	// Call any method: should cause automatic tx invocation
	assertFalse(em.contains(new Person()));

	assertFalse(em.getTransaction().isActive());
	em.joinTransaction();

	assertTrue(em.getTransaction().isActive());

	doInstantiateAndSave(em);
	setComplete();
	endTransaction();	// Should rollback
	assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));

	// Now clean up the database
	startNewTransaction();
	em.joinTransaction();
	deleteAllPeopleUsingEntityManager(em);
	assertEquals("People have been killed", 0, countRowsInTable(em, "person"));
	setComplete();
}
 
Example #20
Source File: AbstractContainerEntityManagerFactoryIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unused", "unchecked" })
public void testEntityManagerProxyIsProxy() {
	assertTrue(Proxy.isProxyClass(sharedEntityManager.getClass()));
	Query q = sharedEntityManager.createQuery("select p from Person as p");
	List<Person> people = q.getResultList();

	assertTrue("Should be open to start with", sharedEntityManager.isOpen());
	sharedEntityManager.close();
	assertTrue("Close should have been silently ignored", sharedEntityManager.isOpen());
}
 
Example #21
Source File: AbstractContainerEntityManagerFactoryIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public void testGetReferenceWhenNoRow() {
	try {
		Person notThere = sharedEntityManager.getReference(Person.class, 666);

		// We may get here (as with Hibernate).
		// Either behaviour is valid: throw exception on first access
		// or on getReference itself.
		notThere.getFirstName();
		fail("Should have thrown an EntityNotFoundException");
	}
	catch (EntityNotFoundException e) {
		/* expected */
	}
}
 
Example #22
Source File: AbstractContainerEntityManagerFactoryIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersons() {
	EntityManager em = entityManagerFactory.createEntityManager();
	Query q = em.createQuery("select p from Person as p");
	List<Person> people = q.getResultList();
	assertEquals(0, people.size());
	try {
		assertNull(q.getSingleResult());
		fail("Should have thrown NoResultException");
	}
	catch (NoResultException ex) {
		// expected
	}
}
 
Example #23
Source File: AbstractContainerEntityManagerFactoryIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unused", "unchecked" })
public void testQueryNoPersonsShared() {
	EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(entityManagerFactory);
	Query q = em.createQuery("select p from Person as p");
	q.setFlushMode(FlushModeType.AUTO);
	List<Person> people = q.getResultList();
	try {
		assertNull(q.getSingleResult());
		fail("Should have thrown NoResultException");
	}
	catch (NoResultException ex) {
		// expected
	}
}
 
Example #24
Source File: AbstractContainerEntityManagerFactoryIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Transactional(propagation = Propagation.NOT_SUPPORTED)
@SuppressWarnings("unchecked")
public void testQueryNoPersonsNotTransactional() {
	EntityManager em = entityManagerFactory.createEntityManager();
	Query q = em.createQuery("select p from Person as p");
	List<Person> people = q.getResultList();
	assertEquals(0, people.size());
	try {
		assertNull(q.getSingleResult());
		fail("Should have thrown NoResultException");
	}
	catch (NoResultException ex) {
		// expected
	}
}
 
Example #25
Source File: AbstractContainerEntityManagerFactoryIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void testMultipleResults() {
	// Add with JDBC
	String firstName = "Tony";
	insertPerson(firstName);

	assertTrue(Proxy.isProxyClass(sharedEntityManager.getClass()));
	Query q = sharedEntityManager.createQuery("select p from Person as p");
	List<Person> people = q.getResultList();

	assertEquals(1, people.size());
	assertEquals(firstName, people.get(0).getFirstName());
}
 
Example #26
Source File: AbstractContainerEntityManagerFactoryIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected void testInstantiateAndSave(EntityManager em) {
	assertEquals("Should be no people from previous transactions", 0, countRowsInTable("person"));
	Person p = new Person();
	p.setFirstName("Tony");
	p.setLastName("Blair");
	em.persist(p);

	em.flush();
	assertEquals("1 row must have been inserted", 1, countRowsInTable("person"));
}
 
Example #27
Source File: HibernateNativeEntityManagerFactoryIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testEntityListener() {
	String firstName = "Tony";
	insertPerson(firstName);

	List<Person> people = sharedEntityManager.createQuery("select p from Person as p").getResultList();
	assertEquals(1, people.size());
	assertEquals(firstName, people.get(0).getFirstName());
	assertSame(applicationContext, people.get(0).postLoaded);
}
 
Example #28
Source File: ApplicationManagedEntityManagerIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected void doInstantiateAndSave(EntityManager em) {
	testStateClean();
	Person p = new Person();

	p.setFirstName("Tony");
	p.setLastName("Blair");
	em.persist(p);

	em.flush();
	assertEquals("1 row must have been inserted", 1, countRowsInTable(em, "person"));
}
 
Example #29
Source File: ApplicationManagedEntityManagerIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testReuseInNewTransaction() {
	EntityManager em = entityManagerFactory.createEntityManager();
	em.joinTransaction();

	doInstantiateAndSave(em);
	endTransaction();

	assertFalse(em.getTransaction().isActive());

	startNewTransaction();
	// Call any method: should cause automatic tx invocation
	assertFalse(em.contains(new Person()));

	assertFalse(em.getTransaction().isActive());
	em.joinTransaction();

	assertTrue(em.getTransaction().isActive());

	doInstantiateAndSave(em);
	setComplete();
	endTransaction();	// Should rollback
	assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));

	// Now clean up the database
	startNewTransaction();
	em.joinTransaction();
	deleteAllPeopleUsingEntityManager(em);
	assertEquals("People have been killed", 0, countRowsInTable(em, "person"));
	setComplete();
}
 
Example #30
Source File: AbstractContainerEntityManagerFactoryIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testGetReferenceWhenNoRow() {
	try {
		Person notThere = sharedEntityManager.getReference(Person.class, 666);

		// We may get here (as with Hibernate). Either behaviour is valid:
		// throw exception on first access or on getReference itself.
		notThere.getFirstName();
		fail("Should have thrown an EntityNotFoundException or ObjectNotFoundException");
	}
	catch (Exception ex) {
		assertTrue(ex.getClass().getName().endsWith("NotFoundException"));
	}
}