[JAVA-2306] Moved articles from spring-persistence-simple
* https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa went into spring-jpa-2 * https://www.baeldung.com/hibernate-5-spring went to spring-jpa-2 * https://www.baeldung.com/transaction-configuration-with-jpa-and-spring went to spring-jpa-2 * https://www.baeldung.com/persistence-layer-with-spring-and-hibernate went to spring-jpa-2 * https://www.baeldung.com/simplifying-the-data-access-layer-with-spring-and-java-generics went to spring-jpa-2 * https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa went to spring-data-jpa-repo-2 * https://www.baeldung.com/spring-data-jpa-query went to spring-data-jpa-query-2 * https://www.baeldung.com/spring-jdbc-jdbctemplate moved to spring-jdbc * Removed spring-persistence-simple module as all articles have been moved
This commit is contained in:
+185
@@ -0,0 +1,185 @@
|
||||
package com.baeldung.hibernate.bootstrap;
|
||||
|
||||
import com.baeldung.hibernate.bootstrap.model.TestEntity;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.annotation.Commit;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
import org.springframework.test.context.transaction.TestTransaction;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { HibernateConf.class })
|
||||
@Transactional
|
||||
public class HibernateBootstrapIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.hibernate.bootstrap;
|
||||
|
||||
import com.baeldung.hibernate.bootstrap.model.TestEntity;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { HibernateXMLConf.class })
|
||||
@Transactional
|
||||
public class HibernateXMLBootstrapIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user