JAVA-2432: Moved articles from here to other modules
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
/target/
|
||||
/target/
|
||||
@@ -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`
|
||||
|
||||
@@ -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>
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.hibernate.audit;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.domain.AuditorAware;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
|
||||
public class AuditorAwareImpl implements AuditorAware<String> {
|
||||
|
||||
@Override
|
||||
public String getCurrentAuditor() {
|
||||
return Optional.ofNullable(SecurityContextHolder.getContext())
|
||||
.map(e -> e.getAuthentication())
|
||||
.map(Authentication::getName)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
}
|
||||
-81
@@ -1,81 +0,0 @@
|
||||
package com.baeldung.hibernate.criteria.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Item implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private Integer itemId;
|
||||
private String itemName;
|
||||
private String itemDescription;
|
||||
private Integer itemPrice;
|
||||
|
||||
// constructors
|
||||
public Item() {
|
||||
|
||||
}
|
||||
|
||||
public Item(final Integer itemId, final String itemName, final String itemDescription) {
|
||||
super();
|
||||
this.itemId = itemId;
|
||||
this.itemName = itemName;
|
||||
this.itemDescription = itemDescription;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((itemId == null) ? 0 : itemId.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
final Item other = (Item) obj;
|
||||
if (itemId == null) {
|
||||
if (other.itemId != null)
|
||||
return false;
|
||||
} else if (!itemId.equals(other.itemId))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public Integer getItemId() {
|
||||
return itemId;
|
||||
}
|
||||
|
||||
public void setItemId(final Integer itemId) {
|
||||
this.itemId = itemId;
|
||||
}
|
||||
|
||||
public String getItemName() {
|
||||
return itemName;
|
||||
}
|
||||
|
||||
public void setItemName(final String itemName) {
|
||||
this.itemName = itemName;
|
||||
}
|
||||
|
||||
public String getItemDescription() {
|
||||
return itemDescription;
|
||||
}
|
||||
|
||||
public Integer getItemPrice() {
|
||||
return itemPrice;
|
||||
}
|
||||
|
||||
public void setItemPrice(final Integer itemPrice) {
|
||||
this.itemPrice = itemPrice;
|
||||
}
|
||||
|
||||
public void setItemDescription(final String itemDescription) {
|
||||
this.itemDescription = itemDescription;
|
||||
}
|
||||
}
|
||||
-58
@@ -1,58 +0,0 @@
|
||||
package com.baeldung.hibernate.fetching.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Date;
|
||||
|
||||
@Entity
|
||||
@Table(name = "USER_ORDER")
|
||||
public class OrderDetail implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "ORDER_ID")
|
||||
private Long orderId;
|
||||
|
||||
public OrderDetail() {
|
||||
}
|
||||
|
||||
public OrderDetail(Date orderDate, String orderDesc) {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((orderId == null) ? 0 : orderId.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
OrderDetail other = (OrderDetail) obj;
|
||||
if (orderId == null) {
|
||||
if (other.orderId != null)
|
||||
return false;
|
||||
} else if (!orderId.equals(other.orderId))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Long getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public void setOrderId(Long orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
}
|
||||
-71
@@ -1,71 +0,0 @@
|
||||
package com.baeldung.hibernate.fetching.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
@Table(name = "USER")
|
||||
public class UserEager implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "USER_ID")
|
||||
private Long userId;
|
||||
|
||||
@OneToMany(fetch = FetchType.EAGER)
|
||||
private Set<OrderDetail> orderDetail = new HashSet();
|
||||
|
||||
public UserEager() {
|
||||
}
|
||||
|
||||
public UserEager(final Long userId) {
|
||||
super();
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
final UserEager other = (UserEager) obj;
|
||||
if (userId == null) {
|
||||
if (other.userId != null)
|
||||
return false;
|
||||
} else if (!userId.equals(other.userId))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(final Long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public Set<OrderDetail> getOrderDetail() {
|
||||
return orderDetail;
|
||||
}
|
||||
|
||||
public void setOrderDetail(Set<OrderDetail> orderDetail) {
|
||||
this.orderDetail = orderDetail;
|
||||
}
|
||||
|
||||
}
|
||||
-71
@@ -1,71 +0,0 @@
|
||||
package com.baeldung.hibernate.fetching.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
@Table(name = "USER")
|
||||
public class UserLazy implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "USER_ID")
|
||||
private Long userId;
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY)
|
||||
private Set<OrderDetail> orderDetail = new HashSet();
|
||||
|
||||
public UserLazy() {
|
||||
}
|
||||
|
||||
public UserLazy(final Long userId) {
|
||||
super();
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
final UserLazy other = (UserLazy) obj;
|
||||
if (userId == null) {
|
||||
if (other.userId != null)
|
||||
return false;
|
||||
} else if (!userId.equals(other.userId))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(final Long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public Set<OrderDetail> getOrderDetail() {
|
||||
return orderDetail;
|
||||
}
|
||||
|
||||
public void setOrderDetail(Set<OrderDetail> orderDetail) {
|
||||
this.orderDetail = orderDetail;
|
||||
}
|
||||
|
||||
}
|
||||
-29
@@ -1,29 +0,0 @@
|
||||
package com.baeldung.hibernate.fetching.util;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
|
||||
public class HibernateUtil {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static Session getHibernateSession(String fetchMethod) {
|
||||
// two config files are there
|
||||
// one with lazy loading enabled
|
||||
// another lazy = false
|
||||
SessionFactory sf;
|
||||
if ("lazy".equals(fetchMethod)) {
|
||||
sf = new Configuration().configure("fetchingLazy.cfg.xml").buildSessionFactory();
|
||||
} else {
|
||||
sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory();
|
||||
}
|
||||
|
||||
// fetching.cfg.xml is used for this example
|
||||
return sf.openSession();
|
||||
}
|
||||
|
||||
public static Session getHibernateSession() {
|
||||
return new Configuration().configure("fetching.cfg.xml").buildSessionFactory().openSession();
|
||||
}
|
||||
|
||||
}
|
||||
-68
@@ -1,68 +0,0 @@
|
||||
package com.baeldung.hibernate.fetching.view;
|
||||
|
||||
import com.baeldung.hibernate.fetching.model.OrderDetail;
|
||||
import com.baeldung.hibernate.fetching.model.UserEager;
|
||||
import com.baeldung.hibernate.fetching.model.UserLazy;
|
||||
import com.baeldung.hibernate.fetching.util.HibernateUtil;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class FetchingAppView {
|
||||
|
||||
public FetchingAppView() {
|
||||
|
||||
}
|
||||
|
||||
// lazily loaded
|
||||
public Set<OrderDetail> lazyLoaded() {
|
||||
final Session sessionLazy = HibernateUtil.getHibernateSession("lazy");
|
||||
List<UserLazy> users = sessionLazy.createQuery("From UserLazy").list();
|
||||
UserLazy userLazyLoaded = users.get(0);
|
||||
// since data is lazyloaded so data won't be initialized
|
||||
return (userLazyLoaded.getOrderDetail());
|
||||
}
|
||||
|
||||
// eagerly loaded
|
||||
public Set<OrderDetail> eagerLoaded() {
|
||||
final Session sessionEager = HibernateUtil.getHibernateSession();
|
||||
// data should be loaded in the following line
|
||||
// also note the queries generated
|
||||
List<UserEager> user = sessionEager.createQuery("From UserEager").list();
|
||||
UserEager userEagerLoaded = user.get(0);
|
||||
return userEagerLoaded.getOrderDetail();
|
||||
}
|
||||
|
||||
// creates test data
|
||||
// call this method to create the data in the database
|
||||
public void createTestData() {
|
||||
|
||||
final Session session = HibernateUtil.getHibernateSession("lazy");
|
||||
Transaction tx = session.beginTransaction();
|
||||
final UserLazy user1 = new UserLazy();
|
||||
final UserLazy user2 = new UserLazy();
|
||||
final UserLazy user3 = new UserLazy();
|
||||
|
||||
session.save(user1);
|
||||
session.save(user2);
|
||||
session.save(user3);
|
||||
|
||||
final OrderDetail order1 = new OrderDetail();
|
||||
final OrderDetail order2 = new OrderDetail();
|
||||
final OrderDetail order3 = new OrderDetail();
|
||||
final OrderDetail order4 = new OrderDetail();
|
||||
final OrderDetail order5 = new OrderDetail();
|
||||
|
||||
session.saveOrUpdate(order1);
|
||||
session.saveOrUpdate(order2);
|
||||
session.saveOrUpdate(order3);
|
||||
session.saveOrUpdate(order4);
|
||||
session.saveOrUpdate(order5);
|
||||
|
||||
tx.commit();
|
||||
session.close();
|
||||
|
||||
}
|
||||
}
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import com.baeldung.persistence.dao.common.IAuditOperations;
|
||||
import com.baeldung.persistence.model.Bar;
|
||||
|
||||
public interface IBarAuditableDao extends IBarDao, IAuditOperations<Bar> {
|
||||
//
|
||||
}
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.baeldung.persistence.model.Bar;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface IBarCrudRepository extends CrudRepository<Bar, Serializable> {
|
||||
//
|
||||
}
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import com.baeldung.persistence.dao.common.IOperations;
|
||||
import com.baeldung.persistence.model.Bar;
|
||||
|
||||
public interface IBarDao extends IOperations<Bar> {
|
||||
//
|
||||
}
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import com.baeldung.persistence.model.Child;
|
||||
import com.baeldung.persistence.dao.common.IOperations;
|
||||
|
||||
public interface IChildDao extends IOperations<Child> {
|
||||
//
|
||||
}
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import com.baeldung.persistence.dao.common.IAuditOperations;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
|
||||
public interface IFooAuditableDao extends IFooDao, IAuditOperations<Foo> {
|
||||
//
|
||||
}
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.persistence.dao.common.IOperations;
|
||||
|
||||
public interface IFooDao extends IOperations<Foo> {
|
||||
//
|
||||
}
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import com.baeldung.persistence.model.Parent;
|
||||
import com.baeldung.persistence.dao.common.IOperations;
|
||||
|
||||
public interface IParentDao extends IOperations<Parent> {
|
||||
//
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.persistence.dao.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
public abstract class AbstractDao<T extends Serializable> implements IOperations<T> {
|
||||
|
||||
protected Class<T> clazz;
|
||||
|
||||
protected final void setClazz(final Class<T> clazzToSet) {
|
||||
clazz = Preconditions.checkNotNull(clazzToSet);
|
||||
}
|
||||
}
|
||||
-37
@@ -1,37 +0,0 @@
|
||||
package com.baeldung.persistence.dao.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.envers.AuditReader;
|
||||
import org.hibernate.envers.AuditReaderFactory;
|
||||
import org.hibernate.envers.query.AuditQuery;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class AbstractHibernateAuditableDao<T extends Serializable> extends AbstractHibernateDao<T> implements IAuditOperations<T> {
|
||||
|
||||
@Override
|
||||
public List<T> getEntitiesAtRevision(final Number revision) {
|
||||
final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession());
|
||||
final AuditQuery query = auditReader.createQuery().forEntitiesAtRevision(clazz, revision);
|
||||
final List<T> resultList = query.getResultList();
|
||||
return resultList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> getEntitiesModifiedAtRevision(final Number revision) {
|
||||
final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession());
|
||||
final AuditQuery query = auditReader.createQuery().forEntitiesModifiedAtRevision(clazz, revision);
|
||||
final List<T> resultList = query.getResultList();
|
||||
return resultList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> getRevisions() {
|
||||
final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession());
|
||||
final AuditQuery query = auditReader.createQuery().forRevisionsOfEntity(clazz, true, true);
|
||||
final List<T> resultList = query.getResultList();
|
||||
return resultList;
|
||||
}
|
||||
|
||||
}
|
||||
-59
@@ -1,59 +0,0 @@
|
||||
package com.baeldung.persistence.dao.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public abstract class AbstractHibernateDao<T extends Serializable> extends AbstractDao<T> implements IOperations<T> {
|
||||
|
||||
@Autowired
|
||||
protected SessionFactory sessionFactory;
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public T findOne(final long id) {
|
||||
return (T) getCurrentSession().get(clazz, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> findAll() {
|
||||
return getCurrentSession().createQuery("from " + clazz.getName()).list();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(final T entity) {
|
||||
Preconditions.checkNotNull(entity);
|
||||
getCurrentSession().saveOrUpdate(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T update(final T entity) {
|
||||
Preconditions.checkNotNull(entity);
|
||||
return (T) getCurrentSession().merge(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(final T entity) {
|
||||
Preconditions.checkNotNull(entity);
|
||||
getCurrentSession().delete(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(final long entityId) {
|
||||
final T entity = findOne(entityId);
|
||||
Preconditions.checkState(entity != null);
|
||||
delete(entity);
|
||||
}
|
||||
|
||||
protected Session getCurrentSession() {
|
||||
return sessionFactory.getCurrentSession();
|
||||
}
|
||||
|
||||
}
|
||||
-56
@@ -1,56 +0,0 @@
|
||||
package com.baeldung.persistence.dao.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
public class AbstractJpaDao<T extends Serializable> extends AbstractDao<T> implements IOperations<T> {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public T findOne(final long id) {
|
||||
return em.find(clazz, Long.valueOf(id).intValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> findAll() {
|
||||
final CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
final CriteriaQuery<T> cq = cb.createQuery(clazz);
|
||||
final Root<T> rootEntry = cq.from(clazz);
|
||||
final CriteriaQuery<T> all = cq.select(rootEntry);
|
||||
final TypedQuery<T> allQuery = em.createQuery(all);
|
||||
return allQuery.getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(final T entity) {
|
||||
em.persist(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T update(final T entity) {
|
||||
em.merge(entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(final T entity) {
|
||||
em.remove(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(final long entityId) {
|
||||
delete(findOne(entityId));
|
||||
}
|
||||
|
||||
}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.persistence.dao.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
|
||||
public class GenericHibernateDao<T extends Serializable> extends AbstractHibernateDao<T> implements IGenericDao<T> {
|
||||
//
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.persistence.dao.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
public interface IAuditOperations<T extends Serializable> {
|
||||
|
||||
List<T> getEntitiesAtRevision(Number revision);
|
||||
|
||||
List<T> getEntitiesModifiedAtRevision(Number revision);
|
||||
|
||||
List<T> getRevisions();
|
||||
|
||||
}
|
||||
-7
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.persistence.dao.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public interface IGenericDao<T extends Serializable> extends IOperations<T> {
|
||||
//
|
||||
}
|
||||
-20
@@ -1,20 +0,0 @@
|
||||
package com.baeldung.persistence.dao.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
public interface IOperations<T extends Serializable> {
|
||||
|
||||
T findOne(final long id);
|
||||
|
||||
List<T> findAll();
|
||||
|
||||
void create(final T entity);
|
||||
|
||||
T update(final T entity);
|
||||
|
||||
void delete(final T entity);
|
||||
|
||||
void deleteById(final long entityId);
|
||||
|
||||
}
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.persistence.dao.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.persistence.dao.IBarAuditableDao;
|
||||
import com.baeldung.persistence.dao.common.AbstractHibernateAuditableDao;
|
||||
import com.baeldung.persistence.model.Bar;
|
||||
|
||||
public class BarAuditableDao extends AbstractHibernateAuditableDao<Bar> implements IBarAuditableDao {
|
||||
|
||||
public BarAuditableDao() {
|
||||
super();
|
||||
|
||||
setClazz(Bar.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public List<Bar> getRevisions() {
|
||||
final List<Bar> resultList = super.getRevisions();
|
||||
for (final Bar bar : resultList) {
|
||||
bar.getFooSet().size(); // force FooSet initialization
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
|
||||
}
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.persistence.dao.impl;
|
||||
|
||||
import com.baeldung.persistence.dao.common.AbstractHibernateDao;
|
||||
import com.baeldung.persistence.dao.IBarDao;
|
||||
import com.baeldung.persistence.model.Bar;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class BarDao extends AbstractHibernateDao<Bar> implements IBarDao {
|
||||
|
||||
public BarDao() {
|
||||
super();
|
||||
|
||||
setClazz(Bar.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
}
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.persistence.dao.impl;
|
||||
|
||||
import com.baeldung.persistence.dao.IBarDao;
|
||||
import com.baeldung.persistence.dao.common.AbstractJpaDao;
|
||||
import com.baeldung.persistence.model.Bar;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class BarJpaDao extends AbstractJpaDao<Bar> implements IBarDao {
|
||||
|
||||
public BarJpaDao() {
|
||||
super();
|
||||
|
||||
setClazz(Bar.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
}
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.persistence.dao.impl;
|
||||
|
||||
import com.baeldung.persistence.dao.common.AbstractHibernateDao;
|
||||
import com.baeldung.persistence.model.Child;
|
||||
import com.baeldung.persistence.dao.IChildDao;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class ChildDao extends AbstractHibernateDao<Child> implements IChildDao {
|
||||
|
||||
public ChildDao() {
|
||||
super();
|
||||
|
||||
setClazz(Child.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.persistence.dao.impl;
|
||||
|
||||
import com.baeldung.persistence.dao.common.AbstractHibernateAuditableDao;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.persistence.dao.IFooAuditableDao;
|
||||
|
||||
public class FooAuditableDao extends AbstractHibernateAuditableDao<Foo> implements IFooAuditableDao {
|
||||
|
||||
public FooAuditableDao() {
|
||||
super();
|
||||
|
||||
setClazz(Foo.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user