Commit source code for BAEL-698 (#2429)
* Commit source for BAEL-698 * Commit model classes for BAEL-698 * remove file BAEL-698 * Commit source for BAEL-698 * Commit util class for BAEL-698 * delete file * Commit source for BAEL-698 * Commit spring config class for BAEL-698 * delete file * Commit source for BAEL-698 * Commit DAO classes for BAEL-698 * delete file * Commit source for BAEL-698 * Commit DAO Impl classes for BAEL-698 * delete file * Add Hibernate config XML BAEL-698 * Commit source for BAEL-698 * Commit test classes for BAEL-698 * delete file * Upgrade versions Upgrade spring framework version & tomcat-dbcp.version version, add mysql-connector-java dependency * Fix typo in class name * Fix type in PersistenceConfig class name * Remove class with typo error * Create tst.txt * Commit the corrected class * Delete tst.txt * Fix minor formatting issues * Fix minor formatting issues * Fix minor formatting issues * Fix minor formatting issues * Fix minor formatting issues * Fix minor formatting issues * Fix minor formatting issues * Fix minor formatting issues * Fix minor formatting issues * Fix minor formatting issues
This commit is contained in:
parent
00e3483531
commit
1a9c33f76f
|
@ -120,6 +120,12 @@
|
|||
<artifactId>hsqldb</artifactId>
|
||||
<version>${hsqldb.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${mysql-connector-java.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
|
@ -167,7 +173,7 @@
|
|||
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<org.springframework.version>4.3.5.RELEASE</org.springframework.version>
|
||||
<org.springframework.version>4.3.10.RELEASE</org.springframework.version>
|
||||
|
||||
<org.springframework.data.version>1.10.6.RELEASE</org.springframework.data.version>
|
||||
|
||||
|
@ -177,7 +183,8 @@
|
|||
|
||||
<!-- persistence -->
|
||||
<hibernate.version>5.2.10.Final</hibernate.version>
|
||||
<tomcat-dbcp.version>8.5.15</tomcat-dbcp.version>
|
||||
<mysql-connector-java.version>8.0.7-dmr</mysql-connector-java.version>
|
||||
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
|
||||
<jta.version>1.1</jta.version>
|
||||
<hsqldb.version>2.3.4</hsqldb.version>
|
||||
<h2.version>1.4.195</h2.version>
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
package com.baeldung.hibernate.manytomany.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Employee")
|
||||
public class Employee implements Serializable {
|
||||
@Id
|
||||
@Column(name = "employee_id")
|
||||
@GeneratedValue
|
||||
private Long employeeId;
|
||||
|
||||
@Column(name = "first_name")
|
||||
private String firstName;
|
||||
|
||||
@Column(name = "last_name")
|
||||
private String lastName;
|
||||
|
||||
@ManyToMany(cascade = { CascadeType.ALL })
|
||||
@JoinTable(
|
||||
name = "Employee_Project",
|
||||
joinColumns = { @JoinColumn(name = "employee_id") },
|
||||
inverseJoinColumns = { @JoinColumn(name = "project_id") }
|
||||
)
|
||||
Set<Project> projects = new HashSet<Project>();
|
||||
|
||||
|
||||
public Employee() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Employee(String firstName, String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Employee(String firstName, String lastName, Set<Project> projects) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.projects = projects;
|
||||
}
|
||||
|
||||
|
||||
public Long getEmployeeId() {
|
||||
return employeeId;
|
||||
}
|
||||
|
||||
public void setEmployeeId(Long employeeId) {
|
||||
this.employeeId = employeeId;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Set<Project> getProjects() {
|
||||
return projects;
|
||||
}
|
||||
|
||||
public void setProjects(Set<Project> projects) {
|
||||
this.projects = projects;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.hibernate.manytomany.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Project")
|
||||
public class Project implements Serializable {
|
||||
|
||||
@Id
|
||||
@Column(name = "project_id")
|
||||
@GeneratedValue
|
||||
private Long projectId;
|
||||
|
||||
@Column(name = "title")
|
||||
private String title;
|
||||
|
||||
@ManyToMany(mappedBy = "projects")
|
||||
private Set<Employee> employees = new HashSet<Employee>();
|
||||
|
||||
public Project() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Project(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Long getProjectId() {
|
||||
return projectId;
|
||||
}
|
||||
|
||||
public void setProjectId(Long projectId) {
|
||||
this.projectId = projectId;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Set<Employee> getEmployees() {
|
||||
return employees;
|
||||
}
|
||||
|
||||
public void setEmployees(Set<Employee> employees) {
|
||||
this.employees = employees;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.hibernate.manytomany.util;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
import com.baeldung.hibernate.manytomany.model.Employee;
|
||||
import com.baeldung.hibernate.manytomany.model.Project;
|
||||
|
||||
public class HibernateUtil {
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private static SessionFactory buildSessionFactory() {
|
||||
try {
|
||||
// Create the SessionFactory from hibernate-annotation.cfg.xml
|
||||
Configuration configuration = new Configuration();
|
||||
configuration.addAnnotatedClass(Employee.class);
|
||||
configuration.addAnnotatedClass(Project.class);
|
||||
configuration.configure("manytomany.cfg.xml");
|
||||
System.out.println("Hibernate Annotation Configuration loaded");
|
||||
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
|
||||
.build();
|
||||
System.out.println("Hibernate Annotation serviceRegistry created");
|
||||
|
||||
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||
|
||||
return sessionFactory;
|
||||
} catch (Throwable ex) {
|
||||
System.err.println("Initial SessionFactory creation failed." + ex);
|
||||
ex.printStackTrace();
|
||||
throw new ExceptionInInitializerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static SessionFactory getSessionFactory() {
|
||||
if (sessionFactory == null)
|
||||
sessionFactory = buildSessionFactory();
|
||||
return sessionFactory;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package com.baeldung.manytomany.spring;
|
||||
|
||||
import java.util.Properties;
|
||||
import javax.sql.DataSource;
|
||||
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.hibernate4.HibernateTransactionManager;
|
||||
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-mysql.properties" })
|
||||
@ComponentScan({ "com.baeldung.hibernate.manytomany" })
|
||||
public class PersistenceConfig {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Bean
|
||||
public LocalSessionFactoryBean sessionFactory() {
|
||||
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
||||
sessionFactory.setDataSource(restDataSource());
|
||||
sessionFactory.setPackagesToScan(new String[] { "com.baeldung.hibernate.manytomany" });
|
||||
sessionFactory.setHibernateProperties(hibernateProperties());
|
||||
|
||||
return sessionFactory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource restDataSource() {
|
||||
final BasicDataSource dataSource = new BasicDataSource();
|
||||
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
|
||||
dataSource.setUrl(env.getProperty("jdbc.url"));
|
||||
dataSource.setUsername(env.getProperty("jdbc.user"));
|
||||
dataSource.setPassword(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", "true");
|
||||
|
||||
return hibernateProperties;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.persistence.manytomany.dao;
|
||||
|
||||
import com.baeldung.hibernate.manytomany.model.Employee;
|
||||
import com.baeldung.persistence.dao.common.IOperations;
|
||||
|
||||
public interface IEmployeeDao extends IOperations<Employee>{
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.persistence.manytomany.dao;
|
||||
|
||||
import com.baeldung.hibernate.manytomany.model.Project;
|
||||
import com.baeldung.persistence.dao.common.IOperations;
|
||||
|
||||
public interface IProjectDao extends IOperations<Project>{
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.persistence.manytomany.dao.impl;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import com.baeldung.hibernate.manytomany.model.Employee;
|
||||
import com.baeldung.persistence.dao.common.AbstractHibernateDao;
|
||||
import com.baeldung.persistence.manytomany.dao.IEmployeeDao;
|
||||
|
||||
@Repository
|
||||
public class EmployeeDao extends AbstractHibernateDao<Employee> implements IEmployeeDao {
|
||||
|
||||
public EmployeeDao() {
|
||||
super();
|
||||
|
||||
setClazz(Employee.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.persistence.manytomany.dao.impl;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import com.baeldung.hibernate.manytomany.model.Project;
|
||||
import com.baeldung.persistence.dao.common.AbstractHibernateDao;
|
||||
import com.baeldung.persistence.manytomany.dao.IProjectDao;
|
||||
|
||||
|
||||
@Repository
|
||||
public class ProjectDao extends AbstractHibernateDao<Project> implements IProjectDao {
|
||||
|
||||
public ProjectDao() {
|
||||
super();
|
||||
|
||||
setClazz(Project.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<property name="hibernate.connection.driver_class">
|
||||
com.mysql.jdbc.Driver
|
||||
</property>
|
||||
<property name="hibernate.connection.password">
|
||||
buddhinisam123
|
||||
</property>
|
||||
<property name="hibernate.connection.url">
|
||||
jdbc:mysql://localhost:3306/spring_hibernate_many_to_many
|
||||
</property>
|
||||
<property name="hibernate.connection.username">
|
||||
root
|
||||
</property>
|
||||
<property name="hibernate.dialect">
|
||||
org.hibernate.dialect.MySQLDialect
|
||||
</property>
|
||||
<property name="hibernate.current_session_context_class">
|
||||
thread
|
||||
</property>
|
||||
<property name="hibernate.show_sql">true</property>
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung.hibernate.manytomany;
|
||||
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
import com.baeldung.hibernate.manytomany.model.Employee;
|
||||
import com.baeldung.hibernate.manytomany.model.Project;
|
||||
import com.baeldung.manytomany.spring.PersistenceConfig;
|
||||
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = {PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class HibernateManyToManyAnnotationJavaConfigMainIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
private Session session;
|
||||
|
||||
|
||||
@Before
|
||||
public final void before() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
|
||||
@After
|
||||
public final void after() {
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenEntitiesAreCreated_thenNoExceptions() {
|
||||
Set<Project> projects = new HashSet<Project>();
|
||||
projects.add(new Project("IT Project"));
|
||||
projects.add(new Project("Networking Project"));
|
||||
session.persist(new Employee("Peter", "Oven", projects));
|
||||
session.persist(new Employee("Allan", "Norman", projects));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
package com.baeldung.hibernate.manytomany;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import com.baeldung.hibernate.manytomany.util.HibernateUtil;
|
||||
import com.baeldung.hibernate.manytomany.model.Employee;
|
||||
import com.baeldung.hibernate.manytomany.model.Project;
|
||||
|
||||
|
||||
public class HibernateManyToManyAnnotationXMLConfigMainIntegrationTest {
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private Session session;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
sessionFactory = HibernateUtil.getSessionFactory();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenSession_checkIfDatabaseIsPopulated() {
|
||||
Employee employee1 = new Employee("Peter", "Oven");
|
||||
Set<Project> projects = new HashSet<Project>();
|
||||
projects = employee1.getProjects();
|
||||
int noProjects = projects.size();
|
||||
assertEquals(0,noProjects);
|
||||
Project project1 = new Project("IT Project");
|
||||
assertNotNull(project1);
|
||||
projects.add(project1);
|
||||
Project project2 = new Project("Networking Project");
|
||||
assertNotNull(project2);
|
||||
projects.add(project2);
|
||||
employee1.setProjects(projects);
|
||||
assertNotNull(employee1);
|
||||
Employee employee2 = new Employee("Allan", "Norman");
|
||||
employee2.setProjects(projects);
|
||||
assertNotNull(employee2);
|
||||
|
||||
session.persist(employee1);
|
||||
session.persist(employee2);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Project> projectList = session.createQuery("FROM Project").list();
|
||||
assertNotNull(projectList);
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Employee> employeeList = session.createQuery("FROM Employee").list();
|
||||
assertNotNull(employeeList);
|
||||
}
|
||||
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterTests() {
|
||||
sessionFactory.close();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue