JAVA-10081 :Create new module hibernate-mapping from spring-hibernate-5 module
This commit is contained in:
parent
26a0093154
commit
d003088364
13
persistence-modules/hibernate-many-to-many/.gitignore
vendored
Normal file
13
persistence-modules/hibernate-many-to-many/.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
*.class
|
||||||
|
|
||||||
|
#folders#
|
||||||
|
/target
|
||||||
|
/neoDb*
|
||||||
|
/data
|
||||||
|
/src/main/webapp/WEB-INF/classes
|
||||||
|
*/META-INF/*
|
||||||
|
|
||||||
|
# Packaged files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
7
persistence-modules/hibernate-many-to-many/README.md
Normal file
7
persistence-modules/hibernate-many-to-many/README.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
## Hibernate 5 with Spring
|
||||||
|
|
||||||
|
This module contains articles about Hibernate 5 with Spring.
|
||||||
|
|
||||||
|
### Relevant articles
|
||||||
|
|
||||||
|
- [Hibernate Many to Many Annotation Tutorial](https://www.baeldung.com/hibernate-many-to-many)
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
136
persistence-modules/hibernate-many-to-many/pom.xml
Normal file
136
persistence-modules/hibernate-many-to-many/pom.xml
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
<?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>hibernate-many-to-many</artifactId>
|
||||||
|
<version>0.1-SNAPSHOT</version>
|
||||||
|
<name>hibernate-many-to-many</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>persistence-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</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>
|
||||||
|
<!-- 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>javax.transaction</groupId>
|
||||||
|
<artifactId>jta</artifactId>
|
||||||
|
<version>${jta.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate</groupId>
|
||||||
|
<artifactId>hibernate-search-orm</artifactId>
|
||||||
|
<version>${hibernatesearch.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.tomcat</groupId>
|
||||||
|
<artifactId>tomcat-dbcp</artifactId>
|
||||||
|
<version>${tomcat-dbcp.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- validation -->
|
||||||
|
<!-- 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>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<version>${h2.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jakarta.xml.bind</groupId>
|
||||||
|
<artifactId>jakarta.xml.bind-api</artifactId>
|
||||||
|
<version>2.3.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.xml.bind</groupId>
|
||||||
|
<artifactId>jaxb-core</artifactId>
|
||||||
|
<version>2.3.0.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.xml.bind</groupId>
|
||||||
|
<artifactId>jaxb-api</artifactId>
|
||||||
|
<version>2.3.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.xml.bind</groupId>
|
||||||
|
<artifactId>jaxb-impl</artifactId>
|
||||||
|
<version>2.3.1</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<!-- Spring -->
|
||||||
|
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
|
||||||
|
<org.springframework.data.version>1.10.6.RELEASE</org.springframework.data.version>
|
||||||
|
<org.springframework.security.version>4.2.1.RELEASE</org.springframework.security.version>
|
||||||
|
<!-- persistence -->
|
||||||
|
<hibernate.version>5.2.10.Final</hibernate.version>
|
||||||
|
<hibernatesearch.version>5.8.2.Final</hibernatesearch.version>
|
||||||
|
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
|
||||||
|
<jta.version>1.1</jta.version>
|
||||||
|
<hsqldb.version>2.3.4</hsqldb.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -1,88 +1,88 @@
|
|||||||
package com.baeldung.hibernate.manytomany.model;
|
package com.baeldung.hibernate.manytomany.model;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.persistence.CascadeType;
|
import javax.persistence.CascadeType;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.GenerationType;
|
import javax.persistence.GenerationType;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.JoinColumn;
|
import javax.persistence.JoinColumn;
|
||||||
import javax.persistence.JoinTable;
|
import javax.persistence.JoinTable;
|
||||||
import javax.persistence.ManyToMany;
|
import javax.persistence.ManyToMany;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "Employee")
|
@Table(name = "Employee")
|
||||||
public class Employee implements Serializable {
|
public class Employee implements Serializable {
|
||||||
@Id
|
@Id
|
||||||
@Column(name = "employee_id")
|
@Column(name = "employee_id")
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long employeeId;
|
private Long employeeId;
|
||||||
|
|
||||||
@Column(name = "first_name")
|
@Column(name = "first_name")
|
||||||
private String firstName;
|
private String firstName;
|
||||||
|
|
||||||
@Column(name = "last_name")
|
@Column(name = "last_name")
|
||||||
private String lastName;
|
private String lastName;
|
||||||
|
|
||||||
@ManyToMany(cascade = { CascadeType.ALL })
|
@ManyToMany(cascade = { CascadeType.ALL })
|
||||||
@JoinTable(
|
@JoinTable(
|
||||||
name = "Employee_Project",
|
name = "Employee_Project",
|
||||||
joinColumns = { @JoinColumn(name = "employee_id") },
|
joinColumns = { @JoinColumn(name = "employee_id") },
|
||||||
inverseJoinColumns = { @JoinColumn(name = "project_id") }
|
inverseJoinColumns = { @JoinColumn(name = "project_id") }
|
||||||
)
|
)
|
||||||
Set<Project> projects = new HashSet<Project>();
|
Set<Project> projects = new HashSet<Project>();
|
||||||
|
|
||||||
|
|
||||||
public Employee() {
|
public Employee() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Employee(String firstName, String lastName) {
|
public Employee(String firstName, String lastName) {
|
||||||
this.firstName = firstName;
|
this.firstName = firstName;
|
||||||
this.lastName = lastName;
|
this.lastName = lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Employee(String firstName, String lastName, Set<Project> projects) {
|
public Employee(String firstName, String lastName, Set<Project> projects) {
|
||||||
this.firstName = firstName;
|
this.firstName = firstName;
|
||||||
this.lastName = lastName;
|
this.lastName = lastName;
|
||||||
this.projects = projects;
|
this.projects = projects;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Long getEmployeeId() {
|
public Long getEmployeeId() {
|
||||||
return employeeId;
|
return employeeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEmployeeId(Long employeeId) {
|
public void setEmployeeId(Long employeeId) {
|
||||||
this.employeeId = employeeId;
|
this.employeeId = employeeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFirstName() {
|
public String getFirstName() {
|
||||||
return firstName;
|
return firstName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFirstName(String firstName) {
|
public void setFirstName(String firstName) {
|
||||||
this.firstName = firstName;
|
this.firstName = firstName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLastName() {
|
public String getLastName() {
|
||||||
return lastName;
|
return lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLastName(String lastName) {
|
public void setLastName(String lastName) {
|
||||||
this.lastName = lastName;
|
this.lastName = lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Project> getProjects() {
|
public Set<Project> getProjects() {
|
||||||
return projects;
|
return projects;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setProjects(Set<Project> projects) {
|
public void setProjects(Set<Project> projects) {
|
||||||
this.projects = projects;
|
this.projects = projects;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,61 +1,61 @@
|
|||||||
package com.baeldung.hibernate.manytomany.model;
|
package com.baeldung.hibernate.manytomany.model;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.ManyToMany;
|
import javax.persistence.ManyToMany;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "Project")
|
@Table(name = "Project")
|
||||||
public class Project implements Serializable {
|
public class Project implements Serializable {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@Column(name = "project_id")
|
@Column(name = "project_id")
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private Long projectId;
|
private Long projectId;
|
||||||
|
|
||||||
@Column(name = "title")
|
@Column(name = "title")
|
||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
@ManyToMany(mappedBy = "projects")
|
@ManyToMany(mappedBy = "projects")
|
||||||
private Set<Employee> employees = new HashSet<Employee>();
|
private Set<Employee> employees = new HashSet<Employee>();
|
||||||
|
|
||||||
public Project() {
|
public Project() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Project(String title) {
|
public Project(String title) {
|
||||||
this.title = title;
|
this.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getProjectId() {
|
public Long getProjectId() {
|
||||||
return projectId;
|
return projectId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setProjectId(Long projectId) {
|
public void setProjectId(Long projectId) {
|
||||||
this.projectId = projectId;
|
this.projectId = projectId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTitle() {
|
public String getTitle() {
|
||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTitle(String title) {
|
public void setTitle(String title) {
|
||||||
this.title = title;
|
this.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Employee> getEmployees() {
|
public Set<Employee> getEmployees() {
|
||||||
return employees;
|
return employees;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEmployees(Set<Employee> employees) {
|
public void setEmployees(Set<Employee> employees) {
|
||||||
this.employees = employees;
|
this.employees = employees;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -1,45 +1,45 @@
|
|||||||
package com.baeldung.hibernate.manytomany.util;
|
package com.baeldung.hibernate.manytomany.util;
|
||||||
|
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.service.ServiceRegistry;
|
import org.hibernate.service.ServiceRegistry;
|
||||||
import com.baeldung.hibernate.manytomany.model.Employee;
|
import com.baeldung.hibernate.manytomany.model.Employee;
|
||||||
import com.baeldung.hibernate.manytomany.model.Project;
|
import com.baeldung.hibernate.manytomany.model.Project;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class HibernateUtil {
|
public class HibernateUtil {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(HibernateUtil.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(HibernateUtil.class);
|
||||||
private static SessionFactory sessionFactory;
|
private static SessionFactory sessionFactory;
|
||||||
|
|
||||||
private static SessionFactory buildSessionFactory() {
|
private static SessionFactory buildSessionFactory() {
|
||||||
try {
|
try {
|
||||||
// Create the SessionFactory from hibernate-annotation.cfg.xml
|
// Create the SessionFactory from hibernate-annotation.cfg.xml
|
||||||
Configuration configuration = new Configuration();
|
Configuration configuration = new Configuration();
|
||||||
configuration.addAnnotatedClass(Employee.class);
|
configuration.addAnnotatedClass(Employee.class);
|
||||||
configuration.addAnnotatedClass(Project.class);
|
configuration.addAnnotatedClass(Project.class);
|
||||||
configuration.configure("manytomany.cfg.xml");
|
configuration.configure("manytomany.cfg.xml");
|
||||||
LOGGER.debug("Hibernate Annotation Configuration loaded");
|
LOGGER.debug("Hibernate Annotation Configuration loaded");
|
||||||
|
|
||||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
|
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
|
||||||
.build();
|
.build();
|
||||||
LOGGER.debug("Hibernate Annotation serviceRegistry created");
|
LOGGER.debug("Hibernate Annotation serviceRegistry created");
|
||||||
|
|
||||||
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||||
|
|
||||||
return sessionFactory;
|
return sessionFactory;
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
LOGGER.error("Initial SessionFactory creation failed.", ex);
|
LOGGER.error("Initial SessionFactory creation failed.", ex);
|
||||||
throw new ExceptionInInitializerError(ex);
|
throw new ExceptionInInitializerError(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SessionFactory getSessionFactory() {
|
public static SessionFactory getSessionFactory() {
|
||||||
if (sessionFactory == null) {
|
if (sessionFactory == null) {
|
||||||
sessionFactory = buildSessionFactory();
|
sessionFactory = buildSessionFactory();
|
||||||
}
|
}
|
||||||
return sessionFactory;
|
return sessionFactory;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,70 +1,70 @@
|
|||||||
package com.baeldung.manytomany.spring;
|
package com.baeldung.manytomany.spring;
|
||||||
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
|
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||||
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
||||||
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
|
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
|
||||||
import org.springframework.transaction.PlatformTransactionManager;
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
@PropertySource({ "classpath:persistence-h2.properties" })
|
@PropertySource({ "classpath:persistence-h2.properties" })
|
||||||
@ComponentScan({ "com.baeldung.hibernate.manytomany" })
|
@ComponentScan({ "com.baeldung.hibernate.manytomany" })
|
||||||
public class PersistenceConfig {
|
public class PersistenceConfig {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Environment env;
|
private Environment env;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public LocalSessionFactoryBean sessionFactory() {
|
public LocalSessionFactoryBean sessionFactory() {
|
||||||
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
||||||
sessionFactory.setDataSource(restDataSource());
|
sessionFactory.setDataSource(restDataSource());
|
||||||
sessionFactory.setPackagesToScan(new String[] { "com.baeldung.hibernate.manytomany" });
|
sessionFactory.setPackagesToScan(new String[] { "com.baeldung.hibernate.manytomany" });
|
||||||
sessionFactory.setHibernateProperties(hibernateProperties());
|
sessionFactory.setHibernateProperties(hibernateProperties());
|
||||||
|
|
||||||
return sessionFactory;
|
return sessionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DataSource restDataSource() {
|
public DataSource restDataSource() {
|
||||||
final BasicDataSource dataSource = new BasicDataSource();
|
final BasicDataSource dataSource = new BasicDataSource();
|
||||||
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
|
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
|
||||||
dataSource.setUrl(env.getProperty("jdbc.url"));
|
dataSource.setUrl(env.getProperty("jdbc.url"));
|
||||||
dataSource.setUsername(env.getProperty("jdbc.user"));
|
dataSource.setUsername(env.getProperty("jdbc.user"));
|
||||||
dataSource.setPassword(env.getProperty("jdbc.pass"));
|
dataSource.setPassword(env.getProperty("jdbc.pass"));
|
||||||
|
|
||||||
return dataSource;
|
return dataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public PlatformTransactionManager hibernateTransactionManager() {
|
public PlatformTransactionManager hibernateTransactionManager() {
|
||||||
final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
|
final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
|
||||||
transactionManager.setSessionFactory(sessionFactory().getObject());
|
transactionManager.setSessionFactory(sessionFactory().getObject());
|
||||||
return transactionManager;
|
return transactionManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||||
return new PersistenceExceptionTranslationPostProcessor();
|
return new PersistenceExceptionTranslationPostProcessor();
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Properties hibernateProperties() {
|
private final Properties hibernateProperties() {
|
||||||
final Properties hibernateProperties = new Properties();
|
final Properties hibernateProperties = new Properties();
|
||||||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||||
hibernateProperties.setProperty("hibernate.show_sql", "false");
|
hibernateProperties.setProperty("hibernate.show_sql", "false");
|
||||||
|
|
||||||
return hibernateProperties;
|
return hibernateProperties;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
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()).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
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);
|
||||||
|
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
package com.baeldung.persistence.manytomany.dao;
|
package com.baeldung.persistence.manytomany.dao;
|
||||||
|
|
||||||
import com.baeldung.hibernate.manytomany.model.Employee;
|
import com.baeldung.hibernate.manytomany.model.Employee;
|
||||||
import com.baeldung.persistence.dao.common.IOperations;
|
import com.baeldung.persistence.dao.common.IOperations;
|
||||||
|
|
||||||
public interface IEmployeeDao extends IOperations<Employee>{
|
public interface IEmployeeDao extends IOperations<Employee>{
|
||||||
|
|
||||||
}
|
}
|
@ -1,8 +1,8 @@
|
|||||||
package com.baeldung.persistence.manytomany.dao;
|
package com.baeldung.persistence.manytomany.dao;
|
||||||
|
|
||||||
import com.baeldung.hibernate.manytomany.model.Project;
|
import com.baeldung.hibernate.manytomany.model.Project;
|
||||||
import com.baeldung.persistence.dao.common.IOperations;
|
import com.baeldung.persistence.dao.common.IOperations;
|
||||||
|
|
||||||
public interface IProjectDao extends IOperations<Project>{
|
public interface IProjectDao extends IOperations<Project>{
|
||||||
|
|
||||||
}
|
}
|
@ -1,16 +1,16 @@
|
|||||||
package com.baeldung.persistence.manytomany.dao.impl;
|
package com.baeldung.persistence.manytomany.dao.impl;
|
||||||
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
import com.baeldung.hibernate.manytomany.model.Employee;
|
import com.baeldung.hibernate.manytomany.model.Employee;
|
||||||
import com.baeldung.persistence.dao.common.AbstractHibernateDao;
|
import com.baeldung.persistence.dao.common.AbstractHibernateDao;
|
||||||
import com.baeldung.persistence.manytomany.dao.IEmployeeDao;
|
import com.baeldung.persistence.manytomany.dao.IEmployeeDao;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public class EmployeeDao extends AbstractHibernateDao<Employee> implements IEmployeeDao {
|
public class EmployeeDao extends AbstractHibernateDao<Employee> implements IEmployeeDao {
|
||||||
|
|
||||||
public EmployeeDao() {
|
public EmployeeDao() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
setClazz(Employee.class);
|
setClazz(Employee.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,17 +1,17 @@
|
|||||||
package com.baeldung.persistence.manytomany.dao.impl;
|
package com.baeldung.persistence.manytomany.dao.impl;
|
||||||
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
import com.baeldung.hibernate.manytomany.model.Project;
|
import com.baeldung.hibernate.manytomany.model.Project;
|
||||||
import com.baeldung.persistence.dao.common.AbstractHibernateDao;
|
import com.baeldung.persistence.dao.common.AbstractHibernateDao;
|
||||||
import com.baeldung.persistence.manytomany.dao.IProjectDao;
|
import com.baeldung.persistence.manytomany.dao.IProjectDao;
|
||||||
|
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public class ProjectDao extends AbstractHibernateDao<Project> implements IProjectDao {
|
public class ProjectDao extends AbstractHibernateDao<Project> implements IProjectDao {
|
||||||
|
|
||||||
public ProjectDao() {
|
public ProjectDao() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
setClazz(Project.class);
|
setClazz(Project.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
package com.baeldung.spring;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
|
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||||
|
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
||||||
|
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableTransactionManagement
|
||||||
|
@PropertySource({ "classpath:persistence-h2.properties" })
|
||||||
|
@ComponentScan({ "com.baeldung.persistence" })
|
||||||
|
public class PersistenceConfig {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Environment env;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocalSessionFactoryBean sessionFactory() {
|
||||||
|
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
||||||
|
sessionFactory.setDataSource(dataSource());
|
||||||
|
sessionFactory.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
|
||||||
|
sessionFactory.setHibernateProperties(hibernateProperties());
|
||||||
|
|
||||||
|
return sessionFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DataSource dataSource() {
|
||||||
|
final BasicDataSource dataSource = new BasicDataSource();
|
||||||
|
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
||||||
|
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
||||||
|
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
|
||||||
|
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
|
||||||
|
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PlatformTransactionManager hibernateTransactionManager() {
|
||||||
|
final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
|
||||||
|
transactionManager.setSessionFactory(sessionFactory().getObject());
|
||||||
|
return transactionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||||
|
return new PersistenceExceptionTranslationPostProcessor();
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Properties hibernateProperties() {
|
||||||
|
final Properties hibernateProperties = new Properties();
|
||||||
|
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||||
|
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||||
|
|
||||||
|
hibernateProperties.setProperty("hibernate.show_sql", "false");
|
||||||
|
|
||||||
|
// Envers properties
|
||||||
|
hibernateProperties.setProperty("org.hibernate.envers.audit_table_suffix", env.getProperty("envers.audit_table_suffix"));
|
||||||
|
|
||||||
|
return hibernateProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
insert into item (item_id, item_name, item_desc, item_price)
|
||||||
|
values(1,'item One', 'test 1', 35.12);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price)
|
||||||
|
values(2,'Pogo stick', 'Pogo stick', 466.12);
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price)
|
||||||
|
values(3,'Raft', 'Raft', 345.12);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price)
|
||||||
|
values(4,'Skate Board', 'Skating', 135.71);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price)
|
||||||
|
values(5,'Umbrella', 'Umbrella for Rain', 619.25);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price)
|
||||||
|
values(6,'Glue', 'Glue for home', 432.73);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price)
|
||||||
|
values(7,'Paint', 'Paint for Room', 1311.40);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price)
|
||||||
|
values(8,'Red paint', 'Red paint for room', 1135.71);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price)
|
||||||
|
values(9,'Household Chairs', 'Chairs for house', 25.71);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price)
|
||||||
|
values(10,'Office Chairs', 'Chairs for office', 395.98);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price)
|
||||||
|
values(11,'Outdoor Chairs', 'Chairs for outdoor activities', 1234.36);
|
@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<logger name="org.springframework" level="WARN" />
|
||||||
|
<logger name="org.springframework.transaction" level="WARN" />
|
||||||
|
|
||||||
|
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||||
|
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
@ -1,15 +1,15 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE hibernate-configuration PUBLIC
|
<!DOCTYPE hibernate-configuration PUBLIC
|
||||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||||
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||||
<hibernate-configuration>
|
<hibernate-configuration>
|
||||||
<session-factory>
|
<session-factory>
|
||||||
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
|
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
|
||||||
<property name="hibernate.connection.password">tutorialmy5ql</property>
|
<property name="hibernate.connection.password">tutorialmy5ql</property>
|
||||||
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/spring_hibernate_many_to_many</property>
|
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/spring_hibernate_many_to_many</property>
|
||||||
<property name="hibernate.connection.username">tutorialuser</property>
|
<property name="hibernate.connection.username">tutorialuser</property>
|
||||||
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
|
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
|
||||||
<property name="hibernate.current_session_context_class">thread</property>
|
<property name="hibernate.current_session_context_class">thread</property>
|
||||||
<property name="hibernate.show_sql">false</property>
|
<property name="hibernate.show_sql">false</property>
|
||||||
</session-factory>
|
</session-factory>
|
||||||
</hibernate-configuration>
|
</hibernate-configuration>
|
@ -0,0 +1,21 @@
|
|||||||
|
# jdbc.X
|
||||||
|
jdbc.driverClassName=org.h2.Driver
|
||||||
|
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
||||||
|
jdbc.eventGeneratedId=sa
|
||||||
|
jdbc.user=sa
|
||||||
|
jdbc.pass=
|
||||||
|
|
||||||
|
# hibernate.X
|
||||||
|
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||||
|
hibernate.show_sql=false
|
||||||
|
hibernate.hbm2ddl.auto=create-drop
|
||||||
|
hibernate.cache.use_second_level_cache=true
|
||||||
|
hibernate.cache.use_query_cache=true
|
||||||
|
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
|
||||||
|
|
||||||
|
# hibernate.search.X
|
||||||
|
hibernate.search.default.directory_provider = filesystem
|
||||||
|
hibernate.search.default.indexBase = /data/index/default
|
||||||
|
|
||||||
|
# envers.X
|
||||||
|
envers.audit_table_suffix=_audit_log
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baeldung.spring.PersistenceConfig;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
|
public class SpringContextTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||||
|
}
|
||||||
|
}
|
@ -1,49 +1,49 @@
|
|||||||
package com.baeldung.hibernate.manytomany;
|
package com.baeldung.hibernate.manytomany;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
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.hibernate.manytomany.model.Employee;
|
import com.baeldung.hibernate.manytomany.model.Employee;
|
||||||
import com.baeldung.hibernate.manytomany.model.Project;
|
import com.baeldung.hibernate.manytomany.model.Project;
|
||||||
import com.baeldung.manytomany.spring.PersistenceConfig;
|
import com.baeldung.manytomany.spring.PersistenceConfig;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
public class HibernateManyToManyAnnotationJavaConfigMainIntegrationTest {
|
public class HibernateManyToManyAnnotationJavaConfigMainIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private SessionFactory sessionFactory;
|
private SessionFactory sessionFactory;
|
||||||
|
|
||||||
private Session session;
|
private Session session;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public final void before() {
|
public final void before() {
|
||||||
session = sessionFactory.openSession();
|
session = sessionFactory.openSession();
|
||||||
session.beginTransaction();
|
session.beginTransaction();
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public final void after() {
|
public final void after() {
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
session.close();
|
session.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenEntitiesAreCreated_thenNoExceptions() {
|
public final void whenEntitiesAreCreated_thenNoExceptions() {
|
||||||
Set<Project> projects = new HashSet<Project>();
|
Set<Project> projects = new HashSet<Project>();
|
||||||
projects.add(new Project("IT Project"));
|
projects.add(new Project("IT Project"));
|
||||||
projects.add(new Project("Networking Project"));
|
projects.add(new Project("Networking Project"));
|
||||||
session.persist(new Employee("Peter", "Oven", projects));
|
session.persist(new Employee("Peter", "Oven", projects));
|
||||||
session.persist(new Employee("Allan", "Norman", projects));
|
session.persist(new Employee("Allan", "Norman", projects));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,80 +1,80 @@
|
|||||||
package com.baeldung.hibernate.manytomany;
|
package com.baeldung.hibernate.manytomany;
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static junit.framework.TestCase.assertEquals;
|
import static junit.framework.TestCase.assertEquals;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.hibernate.manytomany.util.HibernateUtil;
|
import com.baeldung.hibernate.manytomany.util.HibernateUtil;
|
||||||
import com.baeldung.hibernate.manytomany.model.Employee;
|
import com.baeldung.hibernate.manytomany.model.Employee;
|
||||||
import com.baeldung.hibernate.manytomany.model.Project;
|
import com.baeldung.hibernate.manytomany.model.Project;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configured in: manytomany.cfg.xml
|
* Configured in: manytomany.cfg.xml
|
||||||
*/
|
*/
|
||||||
public class HibernateManyToManyAnnotationMainIntegrationTest {
|
public class HibernateManyToManyAnnotationMainIntegrationTest {
|
||||||
private static SessionFactory sessionFactory;
|
private static SessionFactory sessionFactory;
|
||||||
|
|
||||||
private Session session;
|
private Session session;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void beforeTests() {
|
public static void beforeTests() {
|
||||||
sessionFactory = HibernateUtil.getSessionFactory();
|
sessionFactory = HibernateUtil.getSessionFactory();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
session = sessionFactory.openSession();
|
session = sessionFactory.openSession();
|
||||||
session.beginTransaction();
|
session.beginTransaction();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenData_whenInsert_thenCreatesMtoMrelationship() {
|
public void givenData_whenInsert_thenCreatesMtoMrelationship() {
|
||||||
String[] employeeData = { "Peter Oven", "Allan Norman" };
|
String[] employeeData = { "Peter Oven", "Allan Norman" };
|
||||||
String[] projectData = { "IT Project", "Networking Project" };
|
String[] projectData = { "IT Project", "Networking Project" };
|
||||||
Set<Project> projects = new HashSet<Project>();
|
Set<Project> projects = new HashSet<Project>();
|
||||||
|
|
||||||
for (String proj : projectData) {
|
for (String proj : projectData) {
|
||||||
projects.add(new Project(proj));
|
projects.add(new Project(proj));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (String emp : employeeData) {
|
for (String emp : employeeData) {
|
||||||
Employee employee = new Employee(emp.split(" ")[0], emp.split(" ")[1]);
|
Employee employee = new Employee(emp.split(" ")[0], emp.split(" ")[1]);
|
||||||
assertEquals(0, employee.getProjects().size());
|
assertEquals(0, employee.getProjects().size());
|
||||||
employee.setProjects(projects);
|
employee.setProjects(projects);
|
||||||
session.persist(employee);
|
session.persist(employee);
|
||||||
assertNotNull(employee);
|
assertNotNull(employee);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSession_whenRead_thenReturnsMtoMdata() {
|
public void givenSession_whenRead_thenReturnsMtoMdata() {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
List<Employee> employeeList = session.createQuery("FROM Employee").list();
|
List<Employee> employeeList = session.createQuery("FROM Employee").list();
|
||||||
assertNotNull(employeeList);
|
assertNotNull(employeeList);
|
||||||
for(Employee employee : employeeList) {
|
for(Employee employee : employeeList) {
|
||||||
assertNotNull(employee.getProjects());
|
assertNotNull(employee.getProjects());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
session.getTransaction()
|
session.getTransaction()
|
||||||
.commit();
|
.commit();
|
||||||
session.close();
|
session.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
public static void afterTests() {
|
public static void afterTests() {
|
||||||
sessionFactory.close();
|
sessionFactory.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
13
persistence-modules/hibernate-many-to-many/src/test/resources/.gitignore
vendored
Normal file
13
persistence-modules/hibernate-many-to-many/src/test/resources/.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
*.class
|
||||||
|
|
||||||
|
#folders#
|
||||||
|
/target
|
||||||
|
/neoDb*
|
||||||
|
/data
|
||||||
|
/src/main/webapp/WEB-INF/classes
|
||||||
|
*/META-INF/*
|
||||||
|
|
||||||
|
# Packaged files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
@ -0,0 +1,22 @@
|
|||||||
|
<?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>
|
||||||
|
<class name="com.baeldung.hibernate.criteria.model.Item" table="ITEM">
|
||||||
|
<id name="itemId" type="java.lang.Integer">
|
||||||
|
<column name="ITEM_ID" />
|
||||||
|
<generator class="identity" />
|
||||||
|
</id>
|
||||||
|
<property name="itemDescription" type="string">
|
||||||
|
<column name="ITEM_DESC" length="100" />
|
||||||
|
</property>
|
||||||
|
<property name="itemPrice" type="java.lang.Integer">
|
||||||
|
<column name="ITEM_PRICE" not-null="true" />
|
||||||
|
</property>
|
||||||
|
<property name="itemName" type="string">
|
||||||
|
<column name="ITEM_NAME" not-null="true" />
|
||||||
|
</property>
|
||||||
|
</class>
|
||||||
|
|
||||||
|
</hibernate-mapping>
|
@ -0,0 +1,21 @@
|
|||||||
|
insert into item (item_id, item_name, item_desc, item_price) values(1,'item One', 'test 1', 35.12);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price) values(2,'Pogo stick', 'Pogo stick', 466.12);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price) values(3,'Raft', 'Raft', 345.12);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price) values(4,'Skate Board', 'Skating', 135.71);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price) values(5,'Umbrella', 'Umbrella for Rain', 619.25);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price) values(6,'Glue', 'Glue for home', 432.73);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price) values(7,'Paint', 'Paint for Room', 1311.40);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price) values(8,'Red paint', 'Red paint for room', 1135.71);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price) values(9,'Household Chairs', 'Chairs for house', 25.71);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price) values(10,'Office Chairs', 'Chairs for office', 395.98);
|
||||||
|
|
||||||
|
insert into item (item_id, item_name, item_desc, item_price) values(11,'Outdoor Chairs', 'Chairs for outdoor activities', 1234.36);
|
@ -4,7 +4,6 @@ This module contains articles about Hibernate 5 with Spring.
|
|||||||
|
|
||||||
### Relevant articles
|
### Relevant articles
|
||||||
|
|
||||||
- [Hibernate Many to Many Annotation Tutorial](https://www.baeldung.com/hibernate-many-to-many)
|
|
||||||
- [Programmatic Transactions in the Spring TestContext Framework](https://www.baeldung.com/spring-test-programmatic-transactions)
|
- [Programmatic Transactions in the Spring TestContext Framework](https://www.baeldung.com/spring-test-programmatic-transactions)
|
||||||
- [JPA Criteria Queries](https://www.baeldung.com/hibernate-criteria-queries)
|
- [JPA Criteria Queries](https://www.baeldung.com/hibernate-criteria-queries)
|
||||||
- [Introduction to Hibernate Search](https://www.baeldung.com/hibernate-search)
|
- [Introduction to Hibernate Search](https://www.baeldung.com/hibernate-search)
|
||||||
|
@ -108,6 +108,26 @@
|
|||||||
<artifactId>h2</artifactId>
|
<artifactId>h2</artifactId>
|
||||||
<version>${h2.version}</version>
|
<version>${h2.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jakarta.xml.bind</groupId>
|
||||||
|
<artifactId>jakarta.xml.bind-api</artifactId>
|
||||||
|
<version>2.3.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.xml.bind</groupId>
|
||||||
|
<artifactId>jaxb-core</artifactId>
|
||||||
|
<version>2.3.0.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.xml.bind</groupId>
|
||||||
|
<artifactId>jaxb-api</artifactId>
|
||||||
|
<version>2.3.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.xml.bind</groupId>
|
||||||
|
<artifactId>jaxb-impl</artifactId>
|
||||||
|
<version>2.3.1</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
insert into item (item_id, item_name, item_desc, item_price) values(1,'item One', 'test 1', 35.12);
|
insert into item (item_id, item_name, item_desc, item_price) values(1,'item One', 'test 1', 35.12);
|
||||||
|
|
||||||
insert into item (item_id, item_name, item_desc, item_price) values(2,'Pogo stick', 'Pogo stick', 466.12);
|
insert into item (item_id, item_name, item_desc, item_price) values(2,'Pogo stick', 'Pogo stick', 466.12);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user