Merge pull request #10103 from sampada07/JAVA-2432

JAVA-2432: Move articles out of spring-hibernate4
This commit is contained in:
Dhawal Kapil 2020-09-29 18:33:00 +05:30 committed by GitHub
commit fd8be53f0a
103 changed files with 230 additions and 514 deletions

View File

@ -9,4 +9,5 @@ This module contains articles about enterprise concerns such as Multitenancy, Er
- [Hibernate Aggregate Functions](https://www.baeldung.com/hibernate-aggregate-functions) - [Hibernate Aggregate Functions](https://www.baeldung.com/hibernate-aggregate-functions)
- [Common Hibernate Exceptions](https://www.baeldung.com/hibernate-exceptions) - [Common Hibernate Exceptions](https://www.baeldung.com/hibernate-exceptions)
- [Hibernate Error “Not all named parameters have been set”](https://www.baeldung.com/hibernate-error-named-parameters-not-set) - [Hibernate Error “Not all named parameters have been set”](https://www.baeldung.com/hibernate-error-named-parameters-not-set)
- [Various Logging Levels in Hibernate](https://www.baeldung.com/hibernate-logging-levels) - [Various Logging Levels in Hibernate](https://www.baeldung.com/hibernate-logging-levels)
- [Hibernate: save, persist, update, merge, saveOrUpdate](https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate)

View File

@ -61,13 +61,25 @@
<version>${byte-buddy.version}</version> <version>${byte-buddy.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsqldb.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<repositories> <repositories>
<repository> <repository>
<id>geodb-repo</id> <id>osgeo</id>
<name>GeoDB repository</name> <name>OSGeo Release Repository</name>
<url>http://repo.boundlessgeo.com/main/</url> <url>https://repo.osgeo.org/repository/release/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository> </repository>
</repositories> </repositories>
@ -77,6 +89,7 @@
<mariaDB4j.version>2.2.3</mariaDB4j.version> <mariaDB4j.version>2.2.3</mariaDB4j.version>
<assertj-core.version>3.8.0</assertj-core.version> <assertj-core.version>3.8.0</assertj-core.version>
<geodb.version>0.9</geodb.version> <geodb.version>0.9</geodb.version>
<hsqldb.version>2.3.4</hsqldb.version>
</properties> </properties>
</project> </project>

View File

@ -1,6 +1,14 @@
package com.baeldung.persistence.save; package com.baeldung.persistence.save;
import com.baeldung.persistence.model.Person; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import javax.persistence.PersistenceException;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
@ -8,9 +16,13 @@ import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.HSQLDialect; import org.hibernate.dialect.HSQLDialect;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.junit.*; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*; import com.baeldung.persistence.model.Person;
/** /**
* Testing specific implementation details for different methods: * Testing specific implementation details for different methods:
@ -21,12 +33,19 @@ public class SaveMethodsIntegrationTest {
private static SessionFactory sessionFactory; private static SessionFactory sessionFactory;
private Session session; private Session session;
private boolean doNotCommit = false;
@BeforeClass @BeforeClass
public static void beforeTests() { public static void beforeTests() {
Configuration configuration = new Configuration().addAnnotatedClass(Person.class).setProperty("hibernate.dialect", HSQLDialect.class.getName()).setProperty("hibernate.connection.driver_class", org.hsqldb.jdbcDriver.class.getName()) Configuration configuration = new Configuration().addAnnotatedClass(Person.class)
.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test").setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "").setProperty("hibernate.hbm2ddl.auto", "update"); .setProperty("hibernate.dialect", HSQLDialect.class.getName())
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); .setProperty("hibernate.connection.driver_class", org.hsqldb.jdbcDriver.class.getName())
.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test")
.setProperty("hibernate.connection.username", "sa")
.setProperty("hibernate.connection.password", "")
.setProperty("hibernate.hbm2ddl.auto", "update");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
.build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry); sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} }
@ -34,6 +53,7 @@ public class SaveMethodsIntegrationTest {
public void setUp() { public void setUp() {
session = sessionFactory.openSession(); session = sessionFactory.openSession();
session.beginTransaction(); session.beginTransaction();
doNotCommit = false;
} }
@Test @Test
@ -43,7 +63,8 @@ public class SaveMethodsIntegrationTest {
person.setName("John"); person.setName("John");
session.persist(person); session.persist(person);
session.getTransaction().commit(); session.getTransaction()
.commit();
session.close(); session.close();
session = sessionFactory.openSession(); session = sessionFactory.openSession();
@ -68,15 +89,33 @@ public class SaveMethodsIntegrationTest {
assertEquals(id1, id2); assertEquals(id1, id2);
} }
@Test(expected = HibernateException.class) @Test(expected = PersistenceException.class)
public void whenPersistDetached_thenThrowsException() { public void whenPersistDetached_thenThrowsException() {
doNotCommit = true;
Person person = new Person(); Person person = new Person();
person.setName("John"); person.setName("John");
session.persist(person); session.persist(person);
session.evict(person); session.evict(person);
session.persist(person); session.persist(person);
}
@Test
public void whenMergeDetached_thenEntityUpdatedFromDatabase() {
Person person = new Person();
person.setName("John");
session.save(person);
session.flush();
session.evict(person);
person.setName("Mary");
Person mergedPerson = (Person) session.merge(person);
assertNotSame(person, mergedPerson);
assertEquals("Mary", mergedPerson.getName());
} }
@ -92,7 +131,8 @@ public class SaveMethodsIntegrationTest {
assertNotNull(id); assertNotNull(id);
session.getTransaction().commit(); session.getTransaction()
.commit();
session.close(); session.close();
assertEquals(id, person.getId()); assertEquals(id, person.getId());
@ -128,22 +168,6 @@ public class SaveMethodsIntegrationTest {
} }
@Test
public void whenMergeDetached_thenEntityUpdatedFromDatabase() {
Person person = new Person();
person.setName("John");
session.save(person);
session.evict(person);
person.setName("Mary");
Person mergedPerson = (Person) session.merge(person);
assertNotSame(person, mergedPerson);
assertEquals("Mary", mergedPerson.getName());
}
@Test @Test
public void whenMergeTransient_thenNewEntitySavedToDatabase() { public void whenMergeTransient_thenNewEntitySavedToDatabase() {
@ -151,7 +175,8 @@ public class SaveMethodsIntegrationTest {
person.setName("John"); person.setName("John");
Person mergedPerson = (Person) session.merge(person); Person mergedPerson = (Person) session.merge(person);
session.getTransaction().commit(); session.getTransaction()
.commit();
session.beginTransaction(); session.beginTransaction();
assertNull(person.getId()); assertNull(person.getId());
@ -227,7 +252,8 @@ public class SaveMethodsIntegrationTest {
person.setName("John"); person.setName("John");
session.saveOrUpdate(person); session.saveOrUpdate(person);
session.getTransaction().commit(); session.getTransaction()
.commit();
session.close(); session.close();
session = sessionFactory.openSession(); session = sessionFactory.openSession();
@ -250,7 +276,10 @@ public class SaveMethodsIntegrationTest {
@After @After
public void tearDown() { public void tearDown() {
session.getTransaction().commit(); if (!doNotCommit) {
session.getTransaction()
.commit();
}
session.close(); session.close();
} }

View File

@ -81,8 +81,7 @@
<module>spring-data-redis</module> <module>spring-data-redis</module>
<module>spring-data-solr</module> <module>spring-data-solr</module>
<module>spring-hibernate-3</module> <module>spring-hibernate-3</module>
<module>spring-hibernate-5</module> <!-- long running --> <module>spring-hibernate-5</module> <!-- long running -->
<module>spring-hibernate4</module>
<module>spring-jpa</module> <module>spring-jpa</module>
<module>spring-jpa-2</module> <module>spring-jpa-2</module>
<module>spring-jdbc</module> <module>spring-jdbc</module>

View File

@ -6,6 +6,12 @@ This module contains articles about querying data using Spring Data JPA
- [Spring Data JPA @Query Annotation](https://www.baeldung.com/spring-data-jpa-query) - [Spring Data JPA @Query Annotation](https://www.baeldung.com/spring-data-jpa-query)
- [Use Criteria Queries in a Spring Data Application](https://www.baeldung.com/spring-data-criteria-queries) - [Use Criteria Queries in a Spring Data Application](https://www.baeldung.com/spring-data-criteria-queries)
- [Query Entities by Dates and Times with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-query-by-date) - [Query Entities by Dates and Times with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-query-by-date)
- [Hibernate Pagination](https://www.baeldung.com/hibernate-pagination)
- [Sorting with Hibernate](https://www.baeldung.com/hibernate-sort)
- [Stored Procedures with Hibernate](https://www.baeldung.com/stored-procedures-with-hibernate-tutorial)
- [Eager/Lazy Loading In Hibernate](https://www.baeldung.com/hibernate-lazy-eager-loading)
- [Auditing with JPA, Hibernate, and Spring Data JPA](https://www.baeldung.com/database-auditing-jpa)
- More articles: [[<-- prev]](../spring-data-jpa-query) - More articles: [[<-- prev]](../spring-data-jpa-query)
### Eclipse Config ### Eclipse Config

View File

@ -18,6 +18,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
@ -28,6 +32,55 @@
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>${jta.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>${tomcat-dbcp.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
@ -35,5 +88,10 @@
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version> <maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<junit-jupiter.version>5.6.2</junit-jupiter.version> <junit-jupiter.version>5.6.2</junit-jupiter.version>
<junit.version>4.13</junit.version> <junit.version>4.13</junit.version>
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
<jta.version>1.1</jta.version>
<guava.version>21.0</guava.version>
<!-- persistence -->
<hibernate.version>5.2.10.Final</hibernate.version>
</properties> </properties>
</project> </project>

View File

@ -1,4 +1,4 @@
package com.baeldung.spring; package com.baeldung.config;
import java.util.Properties; import java.util.Properties;
@ -15,8 +15,8 @@ import org.springframework.dao.annotation.PersistenceExceptionTranslationPostPro
import org.springframework.data.domain.AuditorAware; import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate4.HibernateTransactionManager; import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean; import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@ -47,7 +47,7 @@ import com.google.common.base.Preconditions;
@Configuration @Configuration
@EnableTransactionManagement @EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "com.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager") @EnableJpaRepositories(basePackages = { "com.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager", entityManagerFactoryRef = "jpaEntityManager")
@EnableJpaAuditing(auditorAwareRef = "auditorProvider") @EnableJpaAuditing(auditorAwareRef = "auditorProvider")
@PropertySource({ "classpath:persistence-h2.properties" }) @PropertySource({ "classpath:persistence-h2.properties" })
@ComponentScan({ "com.baeldung.persistence" }) @ComponentScan({ "com.baeldung.persistence" })
@ -75,7 +75,7 @@ public class PersistenceConfig {
return sessionFactory; return sessionFactory;
} }
@Bean @Bean("jpaEntityManager")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() { public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean(); final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(restDataSource()); emf.setDataSource(restDataSource());

View File

@ -1,4 +1,4 @@
package com.baeldung.spring; package com.baeldung.config;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;

View File

@ -9,11 +9,10 @@ import org.springframework.security.core.context.SecurityContextHolder;
public class AuditorAwareImpl implements AuditorAware<String> { public class AuditorAwareImpl implements AuditorAware<String> {
@Override @Override
public String getCurrentAuditor() { public Optional<String> getCurrentAuditor() {
return Optional.ofNullable(SecurityContextHolder.getContext()) return Optional.ofNullable(SecurityContextHolder.getContext())
.map(e -> e.getAuthentication()) .map(e -> e.getAuthentication())
.map(Authentication::getName) .map(Authentication::getName);
.orElse(null);
} }
} }

View File

@ -1,9 +0,0 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface BookRepository extends JpaRepository<Book, Long>, BookRepositoryCustom, JpaSpecificationExecutor<Book> {
}

View File

@ -1,11 +0,0 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Book;
import java.util.List;
public interface BookRepositoryCustom {
List<Book> findBooksByAuthorNameAndTitle(String authorName, String title);
}

View File

@ -1,44 +0,0 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Book;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
@Repository
public class BookRepositoryImpl implements BookRepositoryCustom {
private EntityManager em;
public BookRepositoryImpl(EntityManager em) {
this.em = em;
}
@Override
public List<Book> findBooksByAuthorNameAndTitle(String authorName, String title) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Book> cq = cb.createQuery(Book.class);
Root<Book> book = cq.from(Book.class);
List<Predicate> predicates = new ArrayList<>();
if (authorName != null) {
predicates.add(cb.equal(book.get("author"), authorName));
}
if (title != null) {
predicates.add(cb.like(book.get("title"), "%" + title + "%"));
}
cq.where(predicates.toArray(new Predicate[0]));
TypedQuery<Book> query = em.createQuery(cq);
return query.getResultList();
}
}

View File

@ -1,25 +0,0 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Book;
import org.springframework.stereotype.Service;
import java.util.List;
import static com.baeldung.persistence.dao.BookSpecifications.hasAuthor;
import static com.baeldung.persistence.dao.BookSpecifications.titleContains;
import static org.springframework.data.jpa.domain.Specification.where;
@Service
public class BookService {
private BookRepository bookRepository;
public BookService(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
public List<Book> query(String author, String title) {
return bookRepository.findAll(where(hasAuthor(author)).and(titleContains(title)));
}
}

View File

@ -1,16 +0,0 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Book;
import org.springframework.data.jpa.domain.Specification;
public class BookSpecifications {
public static Specification<Book> hasAuthor(String author) {
return (book, cq, cb) -> cb.equal(book.get("author"), author);
}
public static Specification<Book> titleContains(String title) {
return (book, cq, cb) -> cb.like(book.get("title"), "%" + title + "%");
}
}

View File

@ -12,7 +12,7 @@ import javax.persistence.criteria.Root;
public class AbstractJpaDao<T extends Serializable> extends AbstractDao<T> implements IOperations<T> { public class AbstractJpaDao<T extends Serializable> extends AbstractDao<T> implements IOperations<T> {
@PersistenceContext @PersistenceContext(unitName = "jpaEntityManager")
private EntityManager em; private EntityManager em;
// API // API

View File

@ -1,36 +0,0 @@
package com.baeldung.persistence.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Book {
@Id
private Long id;
private String title;
private String author;
public Long getId() {
return id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.persistence.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -2,6 +2,7 @@ package com.baeldung.persistence.service.common;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Optional;
import com.baeldung.persistence.dao.common.IOperations; import com.baeldung.persistence.dao.common.IOperations;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
@ -14,7 +15,8 @@ public abstract class AbstractSpringDataJpaService<T extends Serializable> imple
@Override @Override
public T findOne(final long id) { public T findOne(final long id) {
return getDao().findOne(Long.valueOf(id)); Optional<T> opt = getDao().findById(Long.valueOf(id));
return opt.get();
} }
@Override @Override
@ -39,7 +41,7 @@ public abstract class AbstractSpringDataJpaService<T extends Serializable> imple
@Override @Override
public void deleteById(final long entityId) { public void deleteById(final long entityId) {
getDao().delete(Long.valueOf(entityId)); getDao().deleteById(Long.valueOf(entityId));
} }
protected abstract CrudRepository<T, Serializable> getDao(); protected abstract CrudRepository<T, Serializable> getDao();

View File

@ -7,7 +7,7 @@
<context:property-placeholder location="classpath:persistence-mysql.properties"/> <context:property-placeholder location="classpath:persistence-mysql.properties"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/> <property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.baeldung.persistence.model"/> <property name="packagesToScan" value="com.baeldung.persistence.model"/>
<property name="hibernateProperties"> <property name="hibernateProperties">
@ -21,11 +21,11 @@
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource"> <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/> <property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/> <property name="username" value="${jdbc.eventGeneratedId}"/>
<property name="password" value="${jdbc.pass}"/> <property name="password" value="${jdbc.pass}"/>
</bean> </bean>
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/> <property name="sessionFactory" ref="sessionFactory"/>
</bean> </bean>

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:persistence-h2.properties"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.baeldung.persistence.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pass}"/>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>

View File

@ -47,6 +47,7 @@ public class JPABarAuditIntegrationTest {
private IBarService barService; private IBarService barService;
@Autowired @Autowired
@Qualifier("jpaEntityManager")
private EntityManagerFactory entityManagerFactory; private EntityManagerFactory entityManagerFactory;
private EntityManager em; private EntityManager em;

View File

@ -46,6 +46,7 @@ public class SpringDataJPABarAuditIntegrationTest {
private IBarService barService; private IBarService barService;
@Autowired @Autowired
@Qualifier("jpaEntityManager")
private EntityManagerFactory entityManagerFactory; private EntityManagerFactory entityManagerFactory;
private EntityManager em; private EntityManager em;

View File

@ -21,8 +21,8 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.config.PersistenceConfig;
import com.baeldung.persistence.model.Foo; import com.baeldung.persistence.model.Foo;
import com.baeldung.spring.PersistenceConfig;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)

View File

@ -14,8 +14,8 @@ import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate4.HibernateTransactionManager; import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean; import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@ -45,7 +45,7 @@ import com.google.common.base.Preconditions;
@Configuration @Configuration
@EnableTransactionManagement @EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "com.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager") @EnableJpaRepositories(basePackages = { "com.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager", entityManagerFactoryRef = "jpaEntityManager")
@EnableJpaAuditing @EnableJpaAuditing
@PropertySource({ "classpath:persistence-h2.properties" }) @PropertySource({ "classpath:persistence-h2.properties" })
@ComponentScan({ "com.baeldung.persistence" }) @ComponentScan({ "com.baeldung.persistence" })
@ -68,7 +68,7 @@ public class PersistenceTestConfig {
return sessionFactory; return sessionFactory;
} }
@Bean @Bean("jpaEntityManager")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() { public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean(); final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(restDataSource()); emf.setDataSource(restDataSource());

View File

@ -6,6 +6,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.spring.data.jpa.query.datetime.Article;
import com.baeldung.spring.data.jpa.query.datetime.ArticleRepository;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;

View File

@ -12,4 +12,5 @@ This module contains articles about Hibernate 5 with Spring.
- [Hibernate Second-Level Cache](http://www.baeldung.com/hibernate-second-level-cache) - [Hibernate Second-Level Cache](http://www.baeldung.com/hibernate-second-level-cache)
- [Deleting Objects with Hibernate](http://www.baeldung.com/delete-with-hibernate) - [Deleting Objects with Hibernate](http://www.baeldung.com/delete-with-hibernate)
- [Spring, Hibernate and a JNDI Datasource](http://www.baeldung.com/spring-persistence-jpa-jndi-datasource) - [Spring, Hibernate and a JNDI Datasource](http://www.baeldung.com/spring-persistence-jpa-jndi-datasource)
- [@Immutable in Hibernate](http://www.baeldung.com/hibernate-immutable) - [@Immutable in Hibernate](http://www.baeldung.com/hibernate-immutable)
- [Guide to Hibernate 5 with Spring](https://www.baeldung.com/hibernate-4-spring)

View File

@ -1,15 +0,0 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear
/target/
/target/

View File

@ -1,24 +0,0 @@
## Spring with Hibernate 4
This module contains articles about Spring with Hibernate 4
### Relevant Articles:
- [Guide to Hibernate 4 with Spring](https://www.baeldung.com/hibernate-4-spring)
- [Hibernate Pagination](https://www.baeldung.com/hibernate-pagination)
- [Sorting with Hibernate](https://www.baeldung.com/hibernate-sort)
- [Stored Procedures with Hibernate](https://www.baeldung.com/stored-procedures-with-hibernate-tutorial)
- [Hibernate: save, persist, update, merge, saveOrUpdate](https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate)
- [Eager/Lazy Loading In Hibernate](https://www.baeldung.com/hibernate-lazy-eager-loading)
- [Auditing with JPA, Hibernate, and Spring Data JPA](https://www.baeldung.com/database-auditing-jpa)
### Quick Start
```
git clone git://github.com/eugenp/REST.git
cd REST
mvn install
mvn cargo:run
```
- **note**: starts on port `8082`

View File

@ -1,166 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>spring-hibernate4</artifactId>
<version>0.1-SNAPSHOT</version>
<name>spring-hibernate4</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-spring-4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-spring-4</relativePath>
</parent>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${org.springframework.security.version}</version>
</dependency>
<!-- persistence -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${org.springframework.data.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>${hibernate-envers.version}</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>${jta.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>${tomcat-dbcp.version}</version>
</dependency>
<!-- validation -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>${javax.el-api.version}</version>
</dependency>
<!-- utils -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>${org.springframework.security.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsqldb.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<!-- Spring -->
<org.springframework.version>4.3.4.RELEASE</org.springframework.version>
<org.springframework.security.version>4.2.0.RELEASE</org.springframework.security.version>
<org.springframework.data.version>1.10.5.RELEASE</org.springframework.data.version>
<!-- persistence -->
<hibernate.version>4.3.11.Final</hibernate.version>
<hibernate-envers.version>${hibernate.version}</hibernate-envers.version>
<mysql-connector-java.version>5.1.40</mysql-connector-java.version>
<tomcat-dbcp.version>8.5.8</tomcat-dbcp.version>
<jta.version>1.1</jta.version>
<hsqldb.version>2.3.4</hsqldb.version>
<!-- various -->
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
<javax.el-api.version>2.2.5</javax.el-api.version>
<!-- util -->
<guava.version>19.0</guava.version>
<!-- testing -->
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<junit-jupiter.version>5.6.2</junit-jupiter.version>
<junit.version>4.13</junit.version>
</properties>
</project>

Some files were not shown because too many files have changed in this diff Show More