Java Code Examples for org.hibernate.Session#find()
The following examples show how to use
org.hibernate.Session#find() .
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: HibernateModulesOnWildflyIT.java From hibernate-demos with Apache License 2.0 | 6 votes |
/** * Should use hibernate orm 52. */ @Test @Transactional(value=TransactionMode.ROLLBACK) public void shouldUseHibernateOrm52() { Session session = entityManager.unwrap( Session.class ); Kryptonite kryptonite1 = new Kryptonite(); kryptonite1.id = 1L; kryptonite1.description = "Some Kryptonite"; session.persist( kryptonite1 ); session.flush(); session.clear(); // EntityManager methods exposed through Session only as of 5.2 Kryptonite loaded = session.find( Kryptonite.class, 1L ); assertThat( loaded.description, equalTo( "Some Kryptonite" ) ); }
Example 2
Source File: HibernateNoSessionUnitTest.java From tutorials with MIT License | 4 votes |
@Test public void whenAccessUserRolesOutsideSession_thenThrownException() { User detachedUser = createUserWithRoles(); Session session = sessionFactory.openSession(); session.beginTransaction(); User persistentUser = session.find(User.class, detachedUser.getId()); session.getTransaction().commit(); session.close(); thrown.expect(LazyInitializationException.class); System.out.println(persistentUser.getRoles().size()); }
Example 3
Source File: HibernateNoSessionUnitTest.java From tutorials with MIT License | 3 votes |
@Test public void whenAccessUserRolesInsideSession_thenSuccess() { User detachedUser = createUserWithRoles(); Session session = sessionFactory.openSession(); session.beginTransaction(); User persistentUser = session.find(User.class, detachedUser.getId()); Assert.assertEquals(2, persistentUser.getRoles().size()); session.getTransaction().commit(); session.close(); }
Example 4
Source File: HibernateBootstrapIntegrationTest.java From tutorials with MIT License | 3 votes |
@Test public void whenBootstrapHibernateSession_thenNoException() { Session session = sessionFactory.getCurrentSession(); TestEntity newEntity = new TestEntity(); newEntity.setId(1); session.save(newEntity); TestEntity searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); }
Example 5
Source File: HibernateXMLBootstrapIntegrationTest.java From tutorials with MIT License | 3 votes |
@Test public void whenBootstrapHibernateSession_thenNoException() { Session session = sessionFactory.getCurrentSession(); TestEntity newEntity = new TestEntity(); newEntity.setId(1); session.save(newEntity); TestEntity searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); }
Example 6
Source File: HibernateBootstrapIntegrationTest.java From tutorials with MIT License | 3 votes |
@Test public void whenBootstrapHibernateSession_thenNoException() { Session session = sessionFactory.getCurrentSession(); TestEntity newEntity = new TestEntity(); newEntity.setId(1); session.save(newEntity); TestEntity searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); }
Example 7
Source File: HibernateXMLBootstrapIntegrationTest.java From tutorials with MIT License | 3 votes |
@Test public void whenBootstrapHibernateSession_thenNoException() { Session session = sessionFactory.getCurrentSession(); TestEntity newEntity = new TestEntity(); newEntity.setId(1); session.save(newEntity); TestEntity searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); }
Example 8
Source File: HibernateBootstrapIntegrationTest.java From tutorials with MIT License | 2 votes |
@Test public void whenProgrammaticTransactionCommit_thenEntityIsInDatabase() { assertTrue(TestTransaction.isActive()); //Save an entity and commit. Session session = sessionFactory.getCurrentSession(); TestEntity newEntity = new TestEntity(); newEntity.setId(1); session.save(newEntity); TestEntity searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); assertTrue(TestTransaction.isFlaggedForRollback()); TestTransaction.flagForCommit(); TestTransaction.end(); assertFalse(TestTransaction.isFlaggedForRollback()); assertFalse(TestTransaction.isActive()); //Check that the entity is still there in a new transaction, //then delete it, but don't commit. TestTransaction.start(); assertTrue(TestTransaction.isFlaggedForRollback()); assertTrue(TestTransaction.isActive()); session = sessionFactory.getCurrentSession(); searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); session.delete(searchEntity); session.flush(); TestTransaction.end(); assertFalse(TestTransaction.isActive()); //Check that the entity is still there in a new transaction, //then delete it and commit. TestTransaction.start(); session = sessionFactory.getCurrentSession(); searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); session.delete(searchEntity); session.flush(); assertTrue(TestTransaction.isActive()); TestTransaction.flagForCommit(); TestTransaction.end(); assertFalse(TestTransaction.isActive()); //Check that the entity is no longer there in a new transaction. TestTransaction.start(); assertTrue(TestTransaction.isActive()); session = sessionFactory.getCurrentSession(); searchEntity = session.find(TestEntity.class, 1); Assert.assertNull(searchEntity); }
Example 9
Source File: HibernateBootstrapIntegrationTest.java From tutorials with MIT License | 2 votes |
@Test @Commit public void givenTransactionCommitDefault_whenProgrammaticTransactionCommit_thenEntityIsInDatabase() { assertTrue(TestTransaction.isActive()); //Save an entity and commit. Session session = sessionFactory.getCurrentSession(); TestEntity newEntity = new TestEntity(); newEntity.setId(1); session.save(newEntity); TestEntity searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); assertFalse(TestTransaction.isFlaggedForRollback()); TestTransaction.end(); assertFalse(TestTransaction.isFlaggedForRollback()); assertFalse(TestTransaction.isActive()); //Check that the entity is still there in a new transaction, //then delete it, but don't commit. TestTransaction.start(); assertFalse(TestTransaction.isFlaggedForRollback()); assertTrue(TestTransaction.isActive()); session = sessionFactory.getCurrentSession(); searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); session.delete(searchEntity); session.flush(); TestTransaction.flagForRollback(); TestTransaction.end(); assertFalse(TestTransaction.isActive()); //Check that the entity is still there in a new transaction, //then delete it and commit. TestTransaction.start(); session = sessionFactory.getCurrentSession(); searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); session.delete(searchEntity); session.flush(); assertTrue(TestTransaction.isActive()); TestTransaction.end(); assertFalse(TestTransaction.isActive()); //Check that the entity is no longer there in a new transaction. TestTransaction.start(); assertTrue(TestTransaction.isActive()); session = sessionFactory.getCurrentSession(); searchEntity = session.find(TestEntity.class, 1); Assert.assertNull(searchEntity); }
Example 10
Source File: HibernateBootstrapIntegrationTest.java From tutorials with MIT License | 2 votes |
@Test public void whenProgrammaticTransactionCommit_thenEntityIsInDatabase() { assertTrue(TestTransaction.isActive()); //Save an entity and commit. Session session = sessionFactory.getCurrentSession(); TestEntity newEntity = new TestEntity(); newEntity.setId(1); session.save(newEntity); TestEntity searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); assertTrue(TestTransaction.isFlaggedForRollback()); TestTransaction.flagForCommit(); TestTransaction.end(); assertFalse(TestTransaction.isFlaggedForRollback()); assertFalse(TestTransaction.isActive()); //Check that the entity is still there in a new transaction, //then delete it, but don't commit. TestTransaction.start(); assertTrue(TestTransaction.isFlaggedForRollback()); assertTrue(TestTransaction.isActive()); session = sessionFactory.getCurrentSession(); searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); session.delete(searchEntity); session.flush(); TestTransaction.end(); assertFalse(TestTransaction.isActive()); //Check that the entity is still there in a new transaction, //then delete it and commit. TestTransaction.start(); session = sessionFactory.getCurrentSession(); searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); session.delete(searchEntity); session.flush(); assertTrue(TestTransaction.isActive()); TestTransaction.flagForCommit(); TestTransaction.end(); assertFalse(TestTransaction.isActive()); //Check that the entity is no longer there in a new transaction. TestTransaction.start(); assertTrue(TestTransaction.isActive()); session = sessionFactory.getCurrentSession(); searchEntity = session.find(TestEntity.class, 1); Assert.assertNull(searchEntity); }
Example 11
Source File: HibernateBootstrapIntegrationTest.java From tutorials with MIT License | 2 votes |
@Test @Commit public void givenTransactionCommitDefault_whenProgrammaticTransactionCommit_thenEntityIsInDatabase() { assertTrue(TestTransaction.isActive()); //Save an entity and commit. Session session = sessionFactory.getCurrentSession(); TestEntity newEntity = new TestEntity(); newEntity.setId(1); session.save(newEntity); TestEntity searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); assertFalse(TestTransaction.isFlaggedForRollback()); TestTransaction.end(); assertFalse(TestTransaction.isFlaggedForRollback()); assertFalse(TestTransaction.isActive()); //Check that the entity is still there in a new transaction, //then delete it, but don't commit. TestTransaction.start(); assertFalse(TestTransaction.isFlaggedForRollback()); assertTrue(TestTransaction.isActive()); session = sessionFactory.getCurrentSession(); searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); session.delete(searchEntity); session.flush(); TestTransaction.flagForRollback(); TestTransaction.end(); assertFalse(TestTransaction.isActive()); //Check that the entity is still there in a new transaction, //then delete it and commit. TestTransaction.start(); session = sessionFactory.getCurrentSession(); searchEntity = session.find(TestEntity.class, 1); Assert.assertNotNull(searchEntity); session.delete(searchEntity); session.flush(); assertTrue(TestTransaction.isActive()); TestTransaction.end(); assertFalse(TestTransaction.isActive()); //Check that the entity is no longer there in a new transaction. TestTransaction.start(); assertTrue(TestTransaction.isActive()); session = sessionFactory.getCurrentSession(); searchEntity = session.find(TestEntity.class, 1); Assert.assertNull(searchEntity); }