JAVA-4 : Removed hibernate5-2 module, moved its artifacts to other

modules - hibernate-jpa and hibernate-enterprise
This commit is contained in:
sampadawagde 2020-03-11 18:17:43 +05:30
parent 7cbc45c95a
commit 25d015b3df
17 changed files with 0 additions and 632 deletions

View File

@ -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)

View File

@ -1,74 +0,0 @@
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>hibernate5-2</artifactId>
<version>0.1-SNAPSHOT</version>
<name>hibernate5-2</name>
<packaging>jar</packaging>
<description>Hibernate tutorial illustrating the use of named parameters</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>persistence-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-core.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring-boot.version}</version>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.lang3.version}</version>
</dependency>
</dependencies>
<properties>
<hibernate-core.version>5.4.7.Final</hibernate-core.version>
<h2.version>1.4.200</h2.version>
<commons-lang3.version>3.8.1</commons-lang3.version>
<maven.deploy.skip>true</maven.deploy.skip>
<spring-boot.version>2.1.7.RELEASE</spring-boot.version>
<hibernate.core.version>5.4.7.Final</hibernate.core.version>
<h2.version>1.4.200</h2.version>
<commons.lang3.version>3.8.1</commons.lang3.version>
</properties>
</project>

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -1,15 +0,0 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.baeldung.hibernateparameters">
<class name="Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="increment"/>
</id>
<property name="title"/>
</class>
</hibernate-mapping>

View File

@ -1,27 +0,0 @@
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1</property>
<property name="connection.username">sa</property>
<property name="connection.password"/>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.baeldung.hibernate.logging.Employee"/>
</session-factory>
</hibernate-configuration>

View File

@ -1,28 +0,0 @@
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1</property>
<property name="connection.username">sa</property>
<property name="connection.password"/>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/baeldung/hibernateparameters/Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>

View File

@ -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<Employee> query = sessionFactory.openSession().createQuery("from com.baeldung.hibernate.logging.Employee", Employee.class);
List<Employee> deptEmployees = query.list();
Employee deptEmployee = deptEmployees.get(0);
assertEquals("John Smith", deptEmployee.getName());
}
}

View File

@ -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<Event> 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<Event> 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();
}
}
}

View File

@ -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);
}
}

View File

@ -1,25 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</layout>
</appender>
<logger name="org.hibernate">
<level value="info" />
</logger>
<logger name="org.hibernate.SQL">
<level value="debug" />
</logger>
<logger name="org.hibernate.type.descriptor.sql">
<level value="trace" />
</logger>
<logger name="org.hibernate.stat">
<level value="debug" />
</logger>
<root>
<priority value ="info" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>

View File

@ -1,18 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout
pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="org.hibernate" level="info"/>
<Logger name="org.hibernate.SQL" level="debug"/>
<Logger name="org.hibernate.type.descriptor.sql" level="trace"/>
<Logger name="org.hibernate.stat" level="debug" />
<Root level="info" additivity="false">
<AppenderRef ref="console" />
</Root>
</Loggers>
</Configuration>

View File

@ -1,18 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} | %-5p | [%thread] %logger{5}:%L - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.hibernate" level="INFO" />
<logger name="org.hibernate.SQL" level="DEBUG" />
<logger name="org.hibernate.type.descriptor.sql" level="TRACE" />
<logger name="org.hibernate.stat" level="DEBUG" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>