diff --git a/persistence-modules/hibernate5-2/README.md b/persistence-modules/hibernate5-2/README.md deleted file mode 100644 index 330708f6d3..0000000000 --- a/persistence-modules/hibernate5-2/README.md +++ /dev/null @@ -1,11 +0,0 @@ -## Hibernate 5 - -This module contains articles about Hibernate 5. - -### Relevant Articles: -- [Hibernate Error “Not all named parameters have been set”](https://www.baeldung.com/hibernate-error-named-parameters-not-set) -- [FetchMode in Spring Data JPA](https://www.baeldung.com/spring-data-jpa-fetchmode) -- [JPA/Hibernate Persistence Context](https://www.baeldung.com/jpa-hibernate-persistence-context) -- [FetchMode in Hibernate](https://www.baeldung.com/hibernate-fetchmode) -- [Various Logging Levels in Hibernate](https://www.baeldung.com/hibernate-logging-levels) -- [[<-- Prev]](/hibernate5) diff --git a/persistence-modules/hibernate5-2/pom.xml b/persistence-modules/hibernate5-2/pom.xml deleted file mode 100644 index 15d42b3244..0000000000 --- a/persistence-modules/hibernate5-2/pom.xml +++ /dev/null @@ -1,74 +0,0 @@ - - - 4.0.0 - hibernate5-2 - 0.1-SNAPSHOT - hibernate5-2 - jar - Hibernate tutorial illustrating the use of named parameters - - - com.baeldung - persistence-modules - 1.0.0-SNAPSHOT - - - - - org.hibernate - hibernate-core - ${hibernate-core.version} - - - org.springframework.boot - spring-boot-starter-web - ${spring-boot.version} - - - org.springframework.boot - spring-boot-starter-thymeleaf - ${spring-boot.version} - - - org.springframework.boot - spring-boot-starter-data-jpa - ${spring-boot.version} - - - com.zaxxer - HikariCP - - - - - org.springframework.boot - spring-boot-starter-test - ${spring-boot.version} - - - - com.h2database - h2 - ${h2.version} - - - - org.apache.commons - commons-lang3 - ${commons.lang3.version} - - - - - 5.4.7.Final - 1.4.200 - 3.8.1 - true - 2.1.7.RELEASE - 5.4.7.Final - 1.4.200 - 3.8.1 - - - diff --git a/persistence-modules/hibernate5-2/src/main/java/com/baeldung/hibernate/logging/Employee.java b/persistence-modules/hibernate5-2/src/main/java/com/baeldung/hibernate/logging/Employee.java deleted file mode 100644 index 9dcf4058a7..0000000000 --- a/persistence-modules/hibernate5-2/src/main/java/com/baeldung/hibernate/logging/Employee.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.baeldung.hibernate.logging; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; - -@Entity -public class Employee { - @Id - @GeneratedValue(strategy = GenerationType.SEQUENCE) - private long id; - - private String employeeNumber; - - private String title; - - private String name; - - public Employee() { - } - - public Employee(String name, String employeeNumber) { - this.name = name; - this.employeeNumber = employeeNumber; - } - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } - - public String getEmployeeNumber() { - return employeeNumber; - } - - public void setEmployeeNumber(String employeeNumber) { - this.employeeNumber = employeeNumber; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } -} diff --git a/persistence-modules/hibernate5-2/src/main/java/com/baeldung/hibernateparameters/Event.java b/persistence-modules/hibernate5-2/src/main/java/com/baeldung/hibernateparameters/Event.java deleted file mode 100644 index 7912659a09..0000000000 --- a/persistence-modules/hibernate5-2/src/main/java/com/baeldung/hibernateparameters/Event.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.hibernateparameters; - -public class Event { - private Long id; - - private String title; - - public Event() { - } - - public Event(String title) { - this.title = title; - } - - public Long getId() { - return id; - } - - private void setId(Long id) { - this.id = id; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } -} \ No newline at end of file diff --git a/persistence-modules/hibernate5-2/src/main/java/com/baeldung/persistencecontext/PersistenceContextDemoApplication.java b/persistence-modules/hibernate5-2/src/main/java/com/baeldung/persistencecontext/PersistenceContextDemoApplication.java deleted file mode 100644 index 17fa582e10..0000000000 --- a/persistence-modules/hibernate5-2/src/main/java/com/baeldung/persistencecontext/PersistenceContextDemoApplication.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.persistencecontext; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.ComponentScan; - -@SpringBootApplication -@ComponentScan(basePackages="com.baeldung.persistencecontext") -public class PersistenceContextDemoApplication { - public static void main(String[] args) { - SpringApplication.run(PersistenceContextDemoApplication.class, args); - } -} diff --git a/persistence-modules/hibernate5-2/src/main/java/com/baeldung/persistencecontext/entity/User.java b/persistence-modules/hibernate5-2/src/main/java/com/baeldung/persistencecontext/entity/User.java deleted file mode 100644 index 7252ac46f5..0000000000 --- a/persistence-modules/hibernate5-2/src/main/java/com/baeldung/persistencecontext/entity/User.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.baeldung.persistencecontext.entity; - -import javax.persistence.Entity; -import javax.persistence.Id; - -@Entity -public class User { - - @Id - private Long id; - private String name; - private String role; - - public User() { - - } - - public User(Long id, String name, String role) { - this.id = id; - this.name = name; - this.role = role; - } - - public Long getId() { - return id; - } - - public String getName() { - return name; - } - - public String getRole() { - return role; - } - -} diff --git a/persistence-modules/hibernate5-2/src/main/java/com/baeldung/persistencecontext/service/ExtendedPersistenceContextUserService.java b/persistence-modules/hibernate5-2/src/main/java/com/baeldung/persistencecontext/service/ExtendedPersistenceContextUserService.java deleted file mode 100644 index ef25aac69f..0000000000 --- a/persistence-modules/hibernate5-2/src/main/java/com/baeldung/persistencecontext/service/ExtendedPersistenceContextUserService.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.baeldung.persistencecontext.service; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.PersistenceContextType; -import javax.transaction.Transactional; - -import org.springframework.stereotype.Component; - -import com.baeldung.persistencecontext.entity.User; - -@Component -public class ExtendedPersistenceContextUserService { - - @PersistenceContext(type = PersistenceContextType.EXTENDED) - private EntityManager entityManager; - - @Transactional - public User insertWithTransaction(User user) { - entityManager.persist(user); - return user; - } - - public User insertWithoutTransaction(User user) { - entityManager.persist(user); - return user; - } - - public User find(long id) { - User user = entityManager.find(User.class, id); - return user; - } -} diff --git a/persistence-modules/hibernate5-2/src/main/java/com/baeldung/persistencecontext/service/TransctionPersistenceContextUserService.java b/persistence-modules/hibernate5-2/src/main/java/com/baeldung/persistencecontext/service/TransctionPersistenceContextUserService.java deleted file mode 100644 index 481defcf08..0000000000 --- a/persistence-modules/hibernate5-2/src/main/java/com/baeldung/persistencecontext/service/TransctionPersistenceContextUserService.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.persistencecontext.service; - - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.transaction.Transactional; - -import org.springframework.stereotype.Component; - -import com.baeldung.persistencecontext.entity.User; - -@Component -public class TransctionPersistenceContextUserService { - - @PersistenceContext - private EntityManager entityManager; - - @Transactional - public User insertWithTransaction(User user) { - entityManager.persist(user); - return user; - } - - public User insertWithoutTransaction(User user) { - entityManager.persist(user); - return user; - } - - public User find(long id) { - return entityManager.find(User.class, id); - } -} diff --git a/persistence-modules/hibernate5-2/src/main/resources/com/baeldung/hibernateparameters/Event.hbm.xml b/persistence-modules/hibernate5-2/src/main/resources/com/baeldung/hibernateparameters/Event.hbm.xml deleted file mode 100644 index c485080879..0000000000 --- a/persistence-modules/hibernate5-2/src/main/resources/com/baeldung/hibernateparameters/Event.hbm.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - diff --git a/persistence-modules/hibernate5-2/src/main/resources/hibernate-logging.cfg.xml b/persistence-modules/hibernate5-2/src/main/resources/hibernate-logging.cfg.xml deleted file mode 100644 index 52ef1ee685..0000000000 --- a/persistence-modules/hibernate5-2/src/main/resources/hibernate-logging.cfg.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - org.h2.Driver - jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1 - sa - - - 1 - - org.hibernate.dialect.H2Dialect - - org.hibernate.cache.internal.NoCacheProvider - - true - - create - - - - - \ No newline at end of file diff --git a/persistence-modules/hibernate5-2/src/main/resources/hibernate.cfg.xml b/persistence-modules/hibernate5-2/src/main/resources/hibernate.cfg.xml deleted file mode 100644 index be564aaf5a..0000000000 --- a/persistence-modules/hibernate5-2/src/main/resources/hibernate.cfg.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - org.h2.Driver - jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1 - sa - - - 1 - - org.hibernate.dialect.H2Dialect - - org.hibernate.cache.internal.NoCacheProvider - - true - - create - - - - - - \ No newline at end of file diff --git a/persistence-modules/hibernate5-2/src/test/java/com/baeldung/hibernate/logging/HibernateLoggingIntegrationTest.java b/persistence-modules/hibernate5-2/src/test/java/com/baeldung/hibernate/logging/HibernateLoggingIntegrationTest.java deleted file mode 100644 index f609c75834..0000000000 --- a/persistence-modules/hibernate5-2/src/test/java/com/baeldung/hibernate/logging/HibernateLoggingIntegrationTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.baeldung.hibernate.logging; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.List; - -import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistry; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.query.Query; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.hibernate.logging.Employee; - -public class HibernateLoggingIntegrationTest { - - private SessionFactory sessionFactory; - - @Before - public void setUp() throws IOException { - final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure("hibernate-logging.cfg.xml") - .build(); - try { - sessionFactory = new MetadataSources(registry).buildMetadata() - .buildSessionFactory(); - Session session = sessionFactory.openSession(); - session.beginTransaction(); - session.save(new Employee("John Smith", "001")); - session.getTransaction() - .commit(); - session.close(); - } catch (Exception e) { - fail(e); - StandardServiceRegistryBuilder.destroy(registry); - } - } - - @Test - public void whenAllEmployeesAreSelected_ThenSuccess() { - Query query = sessionFactory.openSession().createQuery("from com.baeldung.hibernate.logging.Employee", Employee.class); - List deptEmployees = query.list(); - Employee deptEmployee = deptEmployees.get(0); - assertEquals("John Smith", deptEmployee.getName()); - } -} diff --git a/persistence-modules/hibernate5-2/src/test/java/com/baeldung/hibernateparameters/NamedParameterUnitTest.java b/persistence-modules/hibernate5-2/src/test/java/com/baeldung/hibernateparameters/NamedParameterUnitTest.java deleted file mode 100644 index 4efa1e1f68..0000000000 --- a/persistence-modules/hibernate5-2/src/test/java/com/baeldung/hibernateparameters/NamedParameterUnitTest.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.baeldung.hibernateparameters; - -import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistry; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.query.Query; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; - -public class NamedParameterUnitTest { - private SessionFactory sessionFactory; - - @Before - public void setUp() throws Exception { - final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() - .configure() - .build(); - try { - sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); - Session session = sessionFactory.openSession(); - session.beginTransaction(); - session.save(new Event("Event 1")); - session.save(new Event("Event 2")); - session.getTransaction().commit(); - session.close(); - } catch (Exception e) { - fail(e); - StandardServiceRegistryBuilder.destroy(registry); - } - } - - @After - public void tearDown() throws Exception { - if (sessionFactory != null) { - sessionFactory.close(); - } - } - - @Test - public void whenNamedParameterProvided_thenCorrect() { - Session session = sessionFactory.openSession(); - session.beginTransaction(); - Query query = session.createQuery("from Event E WHERE E.title = :eventTitle", Event.class); - - // This binds the value "Event1" to the parameter :eventTitle - query.setParameter("eventTitle", "Event 1"); - - assertEquals(1, query.list().size()); - session.getTransaction().commit(); - session.close(); - } - - @Test(expected = org.hibernate.QueryException.class) - public void whenNamedParameterMissing_thenThrowsQueryException() { - Session session = sessionFactory.openSession(); - session.beginTransaction(); - Query query = session.createQuery("from Event E WHERE E.title = :eventTitle", Event.class); - - try { - query.list(); - fail("We are expecting an exception!"); - } finally { - session.getTransaction().commit(); - session.close(); - } - } -} diff --git a/persistence-modules/hibernate5-2/src/test/java/com/baeldung/persistencecontext/PersistenceContextIntegrationTest.java b/persistence-modules/hibernate5-2/src/test/java/com/baeldung/persistencecontext/PersistenceContextIntegrationTest.java deleted file mode 100644 index b299dd5834..0000000000 --- a/persistence-modules/hibernate5-2/src/test/java/com/baeldung/persistencecontext/PersistenceContextIntegrationTest.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.baeldung.persistencecontext; - -import com.baeldung.persistencecontext.entity.User; -import com.baeldung.persistencecontext.service.ExtendedPersistenceContextUserService; -import com.baeldung.persistencecontext.service.TransctionPersistenceContextUserService; - -import javax.persistence.EntityExistsException; -import javax.persistence.TransactionRequiredException; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = com.baeldung.persistencecontext.PersistenceContextDemoApplication.class) -public class PersistenceContextIntegrationTest { - - @Autowired - private TransctionPersistenceContextUserService transctionPersistenceContext; - @Autowired - private ExtendedPersistenceContextUserService extendedPersistenceContext; - - @Test - public void testThatWhenUserSavedWithTransctionPersistenceContextThenUserShouldGetSavedInDB() { - User user = new User(121L, "Devender", "admin"); - transctionPersistenceContext.insertWithTransaction(user); - - User userFromTransctionPersistenceContext = transctionPersistenceContext.find(user.getId()); - assertNotNull(userFromTransctionPersistenceContext); - - User userFromExtendedPersistenceContext = extendedPersistenceContext.find(user.getId()); - assertNotNull(userFromExtendedPersistenceContext); - } - - @Test(expected = TransactionRequiredException.class) - public void testThatUserSaveWithoutTransactionThrowException() { - User user = new User(122L, "Devender", "admin"); - transctionPersistenceContext.insertWithoutTransaction(user); - } - - @Test - public void testThatWhenUserSavedWithExtendedPersistenceContextWithoutTransactionThenUserShouldGetCached() { - User user = new User(123L, "Devender", "admin"); - extendedPersistenceContext.insertWithoutTransaction(user); - - User userFromExtendedPersistenceContext = extendedPersistenceContext.find(user.getId()); - assertNotNull(userFromExtendedPersistenceContext); - - User userFromTransctionPersistenceContext = transctionPersistenceContext.find(user.getId()); - assertNull(userFromTransctionPersistenceContext); - } - - @Test(expected = EntityExistsException.class) - public void testThatPersistUserWithSameIdentifierThrowException() { - User user1 = new User(126L, "Devender", "admin"); - User user2 = new User(126L, "Devender", "admin"); - extendedPersistenceContext.insertWithoutTransaction(user1); - extendedPersistenceContext.insertWithoutTransaction(user2); - } - - @Test - public void testThatWhenUserSavedWithExtendedPersistenceContextWithTransactionThenUserShouldSaveEntityIntoDB() { - User user = new User(127L, "Devender", "admin"); - extendedPersistenceContext.insertWithTransaction(user); - - User userFromDB = transctionPersistenceContext.find(user.getId()); - assertNotNull(userFromDB); - } - - @Test - public void testThatWhenUserSavedWithExtendedPersistenceContextWithTransactionThenUserShouldFlushCachedEntityIntoDB() { - User user1 = new User(124L, "Devender", "admin"); - extendedPersistenceContext.insertWithoutTransaction(user1); - - User user2 = new User(125L, "Devender", "admin"); - extendedPersistenceContext.insertWithTransaction(user2); - - User user1FromTransctionPersistenceContext = transctionPersistenceContext.find(user1.getId()); - assertNotNull(user1FromTransctionPersistenceContext); - - User user2FromTransctionPersistenceContext = transctionPersistenceContext.find(user2.getId()); - assertNotNull(user2FromTransctionPersistenceContext); - } - -} diff --git a/persistence-modules/hibernate5-2/src/test/resources/log4j.xml b/persistence-modules/hibernate5-2/src/test/resources/log4j.xml deleted file mode 100644 index 2d153af124..0000000000 --- a/persistence-modules/hibernate5-2/src/test/resources/log4j.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/persistence-modules/hibernate5-2/src/test/resources/log4j2.xml b/persistence-modules/hibernate5-2/src/test/resources/log4j2.xml deleted file mode 100644 index c5d0f12462..0000000000 --- a/persistence-modules/hibernate5-2/src/test/resources/log4j2.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/persistence-modules/hibernate5-2/src/test/resources/logback.xml b/persistence-modules/hibernate5-2/src/test/resources/logback.xml deleted file mode 100644 index 9e591977d7..0000000000 --- a/persistence-modules/hibernate5-2/src/test/resources/logback.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - %d{yyyy-MM-dd HH:mm:ss} | %-5p | [%thread] %logger{5}:%L - %msg%n - - - - - - - - - - - - \ No newline at end of file