JAVA-4 : added new module hibernate5-jpa
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
## Hibernate 5
|
||||
|
||||
This module contains articles about Hibernate 5.
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [JPA Attribute Converters](http://www.baeldung.com/jpa-attribute-converters)
|
||||
- [Pessimistic Locking in JPA](http://www.baeldung.com/jpa-pessimistic-locking)
|
||||
- [Bootstrapping JPA Programmatically in Java](http://www.baeldung.com/java-bootstrap-jpa)
|
||||
- [Optimistic Locking in JPA](http://www.baeldung.com/jpa-optimistic-locking)
|
||||
- [Criteria API – An Example of IN Expressions](https://www.baeldung.com/jpa-criteria-api-in-expressions)
|
||||
- [One-to-One Relationship in JPA](https://www.baeldung.com/jpa-one-to-one)
|
||||
- [Enabling Transaction Locks in Spring Data JPA](https://www.baeldung.com/java-jpa-transaction-locks)
|
||||
- [TransactionRequiredException Error](https://www.baeldung.com/jpa-transaction-required-exception)
|
||||
- [FetchMode in Spring Data JPA](https://www.baeldung.com/spring-data-jpa-fetchmode)
|
||||
- [JPA/Hibernate Persistence Context](https://www.baeldung.com/jpa-hibernate-persistence-context)
|
||||
@@ -0,0 +1,95 @@
|
||||
<?xml version="1.0"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>hibernate5-jpa</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>hibernate5-jpa</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>persistence-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-spatial</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${mysql.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.vorburger.mariaDB4j</groupId>
|
||||
<artifactId>mariaDB4j</artifactId>
|
||||
<version>${mariaDB4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-testing</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${openjdk-jmh.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<hibernate.version>5.3.7.Final</hibernate.version>
|
||||
<mysql.version>6.0.6</mysql.version>
|
||||
<mariaDB4j.version>2.2.3</mariaDB4j.version>
|
||||
<assertj-core.version>3.8.0</assertj-core.version>
|
||||
<openjdk-jmh.version>1.21</openjdk-jmh.version>
|
||||
<spring-boot.version>2.1.7.RELEASE</spring-boot.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
package com.baeldung.hibernate;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
import com.baeldung.hibernate.entities.DeptEmployee;
|
||||
import com.baeldung.hibernate.optimisticlocking.OptimisticLockingCourse;
|
||||
import com.baeldung.hibernate.optimisticlocking.OptimisticLockingStudent;
|
||||
import com.baeldung.hibernate.pessimisticlocking.Individual;
|
||||
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingCourse;
|
||||
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingEmployee;
|
||||
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingStudent;
|
||||
import com.baeldung.hibernate.pojo.Person;
|
||||
import com.baeldung.hibernate.pojo.Post;
|
||||
import com.baeldung.hibernate.pojo.Student;
|
||||
|
||||
public class HibernateUtil {
|
||||
private static String PROPERTY_FILE_NAME;
|
||||
|
||||
public static SessionFactory getSessionFactory() throws IOException {
|
||||
return getSessionFactory(null);
|
||||
}
|
||||
|
||||
public static SessionFactory getSessionFactory(String propertyFileName) throws IOException {
|
||||
PROPERTY_FILE_NAME = propertyFileName;
|
||||
ServiceRegistry serviceRegistry = configureServiceRegistry();
|
||||
return makeSessionFactory(serviceRegistry);
|
||||
}
|
||||
|
||||
public static SessionFactory getSessionFactoryByProperties(Properties properties) throws IOException {
|
||||
ServiceRegistry serviceRegistry = configureServiceRegistry(properties);
|
||||
return makeSessionFactory(serviceRegistry);
|
||||
}
|
||||
|
||||
private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry) {
|
||||
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
|
||||
|
||||
metadataSources.addPackage("com.baeldung.hibernate.pojo");
|
||||
metadataSources.addAnnotatedClass(Person.class);
|
||||
metadataSources.addAnnotatedClass(Student.class);
|
||||
metadataSources.addAnnotatedClass(Individual.class);
|
||||
metadataSources.addAnnotatedClass(PessimisticLockingEmployee.class);
|
||||
metadataSources.addAnnotatedClass(PessimisticLockingStudent.class);
|
||||
metadataSources.addAnnotatedClass(PessimisticLockingCourse.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Customer.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Address.class);
|
||||
metadataSources.addAnnotatedClass(DeptEmployee.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.entities.Department.class);
|
||||
metadataSources.addAnnotatedClass(OptimisticLockingCourse.class);
|
||||
metadataSources.addAnnotatedClass(OptimisticLockingStudent.class);
|
||||
metadataSources.addAnnotatedClass(Post.class);
|
||||
|
||||
Metadata metadata = metadataSources.getMetadataBuilder()
|
||||
.build();
|
||||
|
||||
return metadata.getSessionFactoryBuilder()
|
||||
.build();
|
||||
|
||||
}
|
||||
|
||||
private static ServiceRegistry configureServiceRegistry() throws IOException {
|
||||
return configureServiceRegistry(getProperties());
|
||||
}
|
||||
|
||||
private static ServiceRegistry configureServiceRegistry(Properties properties) throws IOException {
|
||||
return new StandardServiceRegistryBuilder().applySettings(properties)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static Properties getProperties() throws IOException {
|
||||
Properties properties = new Properties();
|
||||
URL propertiesURL = Thread.currentThread()
|
||||
.getContextClassLoader()
|
||||
.getResource(StringUtils.defaultString(PROPERTY_FILE_NAME, "hibernate.properties"));
|
||||
try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) {
|
||||
properties.load(inputStream);
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.hibernate;
|
||||
|
||||
public class UnsupportedTenancyException extends Exception {
|
||||
public UnsupportedTenancyException (String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.hibernate.converters;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
import javax.persistence.Converter;
|
||||
|
||||
import com.baeldung.hibernate.pojo.PersonName;
|
||||
|
||||
@Converter
|
||||
public class PersonNameConverter implements AttributeConverter<PersonName, String> {
|
||||
|
||||
private static final String SEPARATOR = ", ";
|
||||
|
||||
@Override
|
||||
public String convertToDatabaseColumn(PersonName personName) {
|
||||
if (personName == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (personName.getSurname() != null && !personName.getSurname()
|
||||
.isEmpty()) {
|
||||
sb.append(personName.getSurname());
|
||||
sb.append(SEPARATOR);
|
||||
}
|
||||
|
||||
if (personName.getName() != null && !personName.getName()
|
||||
.isEmpty()) {
|
||||
sb.append(personName.getName());
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PersonName convertToEntityAttribute(String dbPersonName) {
|
||||
if (dbPersonName == null || dbPersonName.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String[] pieces = dbPersonName.split(SEPARATOR);
|
||||
|
||||
if (pieces == null || pieces.length == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
PersonName personName = new PersonName();
|
||||
String firstPiece = !pieces[0].isEmpty() ? pieces[0] : null;
|
||||
if (dbPersonName.contains(SEPARATOR)) {
|
||||
personName.setSurname(firstPiece);
|
||||
|
||||
if (pieces.length >= 2 && pieces[1] != null && !pieces[1].isEmpty()) {
|
||||
personName.setName(pieces[1]);
|
||||
}
|
||||
} else {
|
||||
personName.setName(firstPiece);
|
||||
}
|
||||
|
||||
return personName;
|
||||
}
|
||||
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.hibernate.entities;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Department {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy="department")
|
||||
private List<DeptEmployee> employees;
|
||||
|
||||
public Department(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<DeptEmployee> getEmployees() {
|
||||
return employees;
|
||||
}
|
||||
|
||||
public void setEmployees(List<DeptEmployee> employees) {
|
||||
this.employees = employees;
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package com.baeldung.hibernate.entities;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@org.hibernate.annotations.NamedQueries({ @org.hibernate.annotations.NamedQuery(name = "DeptEmployee_FindByEmployeeNumber", query = "from DeptEmployee where employeeNumber = :employeeNo"),
|
||||
@org.hibernate.annotations.NamedQuery(name = "DeptEmployee_FindAllByDesgination", query = "from DeptEmployee where designation = :designation"),
|
||||
@org.hibernate.annotations.NamedQuery(name = "DeptEmployee_UpdateEmployeeDepartment", query = "Update DeptEmployee set department = :newDepartment where employeeNumber = :employeeNo"),
|
||||
@org.hibernate.annotations.NamedQuery(name = "DeptEmployee_FindAllByDepartment", query = "from DeptEmployee where department = :department", timeout = 1, fetchSize = 10) })
|
||||
@org.hibernate.annotations.NamedNativeQueries({ @org.hibernate.annotations.NamedNativeQuery(name = "DeptEmployee_FindByEmployeeName", query = "select * from deptemployee emp where name=:name", resultClass = DeptEmployee.class),
|
||||
@org.hibernate.annotations.NamedNativeQuery(name = "DeptEmployee_UpdateEmployeeDesignation", query = "call UPDATE_EMPLOYEE_DESIGNATION(:employeeNumber, :newDesignation)", resultClass = DeptEmployee.class) })
|
||||
@Entity
|
||||
public class DeptEmployee {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private long id;
|
||||
|
||||
private String employeeNumber;
|
||||
|
||||
private String title;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToOne
|
||||
private Department department;
|
||||
|
||||
public DeptEmployee(String name, String employeeNumber, Department department) {
|
||||
this.name = name;
|
||||
this.employeeNumber = employeeNumber;
|
||||
this.department = department;
|
||||
}
|
||||
|
||||
public DeptEmployee(String name, String employeeNumber, String title, Department department) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.employeeNumber = employeeNumber;
|
||||
this.title = title;
|
||||
this.department = department;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getEmployeeNumber() {
|
||||
return employeeNumber;
|
||||
}
|
||||
|
||||
public void setEmployeeNumber(String employeeNumber) {
|
||||
this.employeeNumber = employeeNumber;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Department getDepartment() {
|
||||
return department;
|
||||
}
|
||||
|
||||
public void setDepartment(Department department) {
|
||||
this.department = department;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.hibernate.jpabootstrap.application;
|
||||
|
||||
import com.baeldung.hibernate.jpabootstrap.config.JpaEntityManagerFactory;
|
||||
import com.baeldung.hibernate.jpabootstrap.entities.User;
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
EntityManager entityManager = getJpaEntityManager();
|
||||
User user = entityManager.find(User.class, 1);
|
||||
System.out.println(user);
|
||||
entityManager.getTransaction().begin();
|
||||
user.setName("John");
|
||||
user.setEmail("[email protected]");
|
||||
entityManager.merge(user);
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.persist(new User("Monica", "[email protected]"));
|
||||
entityManager.getTransaction().commit();
|
||||
|
||||
// additional CRUD operations
|
||||
|
||||
}
|
||||
|
||||
private static class EntityManagerHolder {
|
||||
private static final EntityManager ENTITY_MANAGER = new JpaEntityManagerFactory(
|
||||
new Class[]{User.class}).getEntityManager();
|
||||
}
|
||||
|
||||
public static EntityManager getJpaEntityManager() {
|
||||
return EntityManagerHolder.ENTITY_MANAGER;
|
||||
}
|
||||
}
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
package com.baeldung.hibernate.jpabootstrap.config;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import javax.sql.DataSource;
|
||||
import javax.persistence.SharedCacheMode;
|
||||
import javax.persistence.ValidationMode;
|
||||
import javax.persistence.spi.ClassTransformer;
|
||||
import javax.persistence.spi.PersistenceUnitInfo;
|
||||
import javax.persistence.spi.PersistenceUnitTransactionType;
|
||||
import org.hibernate.jpa.HibernatePersistenceProvider;
|
||||
|
||||
public class HibernatePersistenceUnitInfo implements PersistenceUnitInfo {
|
||||
|
||||
public static final String JPA_VERSION = "2.1";
|
||||
private final String persistenceUnitName;
|
||||
private PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
|
||||
private final List<String> managedClassNames;
|
||||
private final List<String> mappingFileNames = new ArrayList<>();
|
||||
private final Properties properties;
|
||||
private DataSource jtaDataSource;
|
||||
private DataSource nonjtaDataSource;
|
||||
private final List<ClassTransformer> transformers = new ArrayList<>();
|
||||
|
||||
public HibernatePersistenceUnitInfo(String persistenceUnitName, List<String> managedClassNames, Properties properties) {
|
||||
this.persistenceUnitName = persistenceUnitName;
|
||||
this.managedClassNames = managedClassNames;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPersistenceUnitName() {
|
||||
return persistenceUnitName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPersistenceProviderClassName() {
|
||||
return HibernatePersistenceProvider.class.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PersistenceUnitTransactionType getTransactionType() {
|
||||
return transactionType;
|
||||
}
|
||||
|
||||
public HibernatePersistenceUnitInfo setJtaDataSource(DataSource jtaDataSource) {
|
||||
this.jtaDataSource = jtaDataSource;
|
||||
this.nonjtaDataSource = null;
|
||||
transactionType = PersistenceUnitTransactionType.JTA;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource getJtaDataSource() {
|
||||
return jtaDataSource;
|
||||
}
|
||||
|
||||
public HibernatePersistenceUnitInfo setNonJtaDataSource(DataSource nonJtaDataSource) {
|
||||
this.nonjtaDataSource = nonJtaDataSource;
|
||||
this.jtaDataSource = null;
|
||||
transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource getNonJtaDataSource() {
|
||||
return nonjtaDataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getMappingFileNames() {
|
||||
return mappingFileNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<URL> getJarFileUrls() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL getPersistenceUnitRootUrl() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getManagedClassNames() {
|
||||
return managedClassNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean excludeUnlistedClasses() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SharedCacheMode getSharedCacheMode() {
|
||||
return SharedCacheMode.UNSPECIFIED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValidationMode getValidationMode() {
|
||||
return ValidationMode.AUTO;
|
||||
}
|
||||
|
||||
public Properties getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPersistenceXMLSchemaVersion() {
|
||||
return JPA_VERSION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassLoader getClassLoader() {
|
||||
return Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addTransformer(ClassTransformer transformer) {
|
||||
transformers.add(transformer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassLoader getNewTempClassLoader() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.hibernate.jpabootstrap.config;
|
||||
|
||||
import com.mysql.cj.jdbc.MysqlDataSource;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.sql.DataSource;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.spi.PersistenceUnitInfo;
|
||||
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;
|
||||
import org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor;
|
||||
|
||||
public class JpaEntityManagerFactory {
|
||||
|
||||
private final String DB_URL = "jdbc:mysql://databaseurl";
|
||||
private final String DB_USER_NAME = "username";
|
||||
private final String DB_PASSWORD = "password";
|
||||
private final Class[] entityClasses;
|
||||
|
||||
public JpaEntityManagerFactory(Class[] entityClasses) {
|
||||
this.entityClasses = entityClasses;
|
||||
}
|
||||
|
||||
public EntityManager getEntityManager() {
|
||||
return getEntityManagerFactory().createEntityManager();
|
||||
}
|
||||
|
||||
protected EntityManagerFactory getEntityManagerFactory() {
|
||||
PersistenceUnitInfo persistenceUnitInfo = getPersistenceUnitInfo(getClass().getSimpleName());
|
||||
Map<String, Object> configuration = new HashMap<>();
|
||||
return new EntityManagerFactoryBuilderImpl(new PersistenceUnitInfoDescriptor(persistenceUnitInfo), configuration)
|
||||
.build();
|
||||
}
|
||||
|
||||
protected HibernatePersistenceUnitInfo getPersistenceUnitInfo(String name) {
|
||||
return new HibernatePersistenceUnitInfo(name, getEntityClassNames(), getProperties());
|
||||
}
|
||||
|
||||
protected List<String> getEntityClassNames() {
|
||||
return Arrays.asList(getEntities())
|
||||
.stream()
|
||||
.map(Class::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
protected Properties getProperties() {
|
||||
Properties properties = new Properties();
|
||||
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
|
||||
properties.put("hibernate.id.new_generator_mappings", false);
|
||||
properties.put("hibernate.connection.datasource", getMysqlDataSource());
|
||||
return properties;
|
||||
}
|
||||
|
||||
protected Class[] getEntities() {
|
||||
return entityClasses;
|
||||
}
|
||||
|
||||
protected DataSource getMysqlDataSource() {
|
||||
MysqlDataSource mysqlDataSource = new MysqlDataSource();
|
||||
mysqlDataSource.setURL(DB_URL);
|
||||
mysqlDataSource.setUser(DB_USER_NAME);
|
||||
mysqlDataSource.setPassword(DB_PASSWORD);
|
||||
return mysqlDataSource;
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.hibernate.jpabootstrap.entities;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
public User(){}
|
||||
|
||||
public User(String name, String email) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.hibernate.jpacriteriabuilder.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.hibernate.entities.DeptEmployee;
|
||||
|
||||
public interface EmployeeSearchService {
|
||||
|
||||
List<DeptEmployee> filterbyTitleUsingCriteriaBuilder(List<String> titles);
|
||||
|
||||
List<DeptEmployee> filterbyTitleUsingExpression(List<String> titles);
|
||||
|
||||
List<DeptEmployee> searchByDepartmentQuery(String query);
|
||||
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package com.baeldung.hibernate.jpacriteriabuilder.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaBuilder.In;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
import javax.persistence.criteria.Subquery;
|
||||
|
||||
import com.baeldung.hibernate.entities.Department;
|
||||
import com.baeldung.hibernate.entities.DeptEmployee;
|
||||
|
||||
public class EmployeeSearchServiceImpl implements EmployeeSearchService {
|
||||
|
||||
private EntityManager entityManager;
|
||||
private CriteriaBuilder criteriaBuilder;
|
||||
|
||||
public EmployeeSearchServiceImpl(EntityManager entityManager) {
|
||||
this.entityManager = entityManager;
|
||||
this.criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DeptEmployee> filterbyTitleUsingCriteriaBuilder(List<String> titles) {
|
||||
CriteriaQuery<DeptEmployee> criteriaQuery = createCriteriaQuery(DeptEmployee.class);
|
||||
Root<DeptEmployee> root = criteriaQuery.from(DeptEmployee.class);
|
||||
In<String> inClause = criteriaBuilder.in(root.get("title"));
|
||||
for (String title : titles) {
|
||||
inClause.value(title);
|
||||
}
|
||||
criteriaQuery.select(root)
|
||||
.where(inClause);
|
||||
TypedQuery<DeptEmployee> query = entityManager.createQuery(criteriaQuery);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DeptEmployee> filterbyTitleUsingExpression(List<String> titles) {
|
||||
CriteriaQuery<DeptEmployee> criteriaQuery = createCriteriaQuery(DeptEmployee.class);
|
||||
Root<DeptEmployee> root = criteriaQuery.from(DeptEmployee.class);
|
||||
criteriaQuery.select(root)
|
||||
.where(root.get("title")
|
||||
.in(titles));
|
||||
TypedQuery<DeptEmployee> query = entityManager.createQuery(criteriaQuery);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DeptEmployee> searchByDepartmentQuery(String searchKey) {
|
||||
CriteriaQuery<DeptEmployee> criteriaQuery = createCriteriaQuery(DeptEmployee.class);
|
||||
Root<DeptEmployee> emp = criteriaQuery.from(DeptEmployee.class);
|
||||
|
||||
Subquery<Department> subquery = criteriaQuery.subquery(Department.class);
|
||||
Root<Department> dept = subquery.from(Department.class);
|
||||
subquery.select(dept)
|
||||
.distinct(true)
|
||||
.where(criteriaBuilder.like(dept.get("name"), new StringBuffer("%").append(searchKey)
|
||||
.append("%")
|
||||
.toString()));
|
||||
|
||||
criteriaQuery.select(emp)
|
||||
.where(criteriaBuilder.in(emp.get("department"))
|
||||
.value(subquery));
|
||||
|
||||
TypedQuery<DeptEmployee> query = entityManager.createQuery(criteriaQuery);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
private <T> CriteriaQuery<T> createCriteriaQuery(Class<T> klass) {
|
||||
return criteriaBuilder.createQuery(klass);
|
||||
}
|
||||
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.hibernate.onetoone;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Properties;
|
||||
|
||||
public class HibernateUtil {
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private HibernateUtil() {
|
||||
}
|
||||
|
||||
public static SessionFactory getSessionFactory(Strategy strategy) {
|
||||
return buildSessionFactory(strategy);
|
||||
}
|
||||
|
||||
private static SessionFactory buildSessionFactory(Strategy strategy) {
|
||||
try {
|
||||
ServiceRegistry serviceRegistry = configureServiceRegistry();
|
||||
|
||||
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
|
||||
|
||||
for (Class<?> entityClass : strategy.getEntityClasses()) {
|
||||
metadataSources.addAnnotatedClass(entityClass);
|
||||
}
|
||||
|
||||
Metadata metadata = metadataSources.getMetadataBuilder()
|
||||
.build();
|
||||
|
||||
return metadata.getSessionFactoryBuilder()
|
||||
.build();
|
||||
} catch (IOException ex) {
|
||||
throw new ExceptionInInitializerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static ServiceRegistry configureServiceRegistry() throws IOException {
|
||||
Properties properties = getProperties();
|
||||
return new StandardServiceRegistryBuilder().applySettings(properties)
|
||||
.build();
|
||||
}
|
||||
|
||||
private static Properties getProperties() throws IOException {
|
||||
Properties properties = new Properties();
|
||||
URL propertiesURL = Thread.currentThread()
|
||||
.getContextClassLoader()
|
||||
.getResource("hibernate.properties");
|
||||
try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) {
|
||||
properties.load(inputStream);
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.hibernate.onetoone;
|
||||
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public enum Strategy {
|
||||
//See that the classes belongs to different packages
|
||||
FOREIGN_KEY(Arrays.asList(com.baeldung.hibernate.onetoone.foreignkeybased.User.class,
|
||||
com.baeldung.hibernate.onetoone.foreignkeybased.Address.class)),
|
||||
SHARED_PRIMARY_KEY(Arrays.asList(com.baeldung.hibernate.onetoone.sharedkeybased.User.class,
|
||||
com.baeldung.hibernate.onetoone.sharedkeybased.Address.class)),
|
||||
JOIN_TABLE_BASED(Arrays.asList(com.baeldung.hibernate.onetoone.jointablebased.Employee.class,
|
||||
com.baeldung.hibernate.onetoone.jointablebased.WorkStation.class));
|
||||
|
||||
private List<Class<?>> entityClasses;
|
||||
|
||||
Strategy(List<Class<?>> entityClasses) {
|
||||
this.entityClasses = entityClasses;
|
||||
}
|
||||
|
||||
public List<Class<?>> getEntityClasses() {
|
||||
return entityClasses;
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.hibernate.onetoone.foreignkeybased;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "address")
|
||||
public class Address {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "street")
|
||||
private String street;
|
||||
|
||||
@Column(name = "city")
|
||||
private String city;
|
||||
|
||||
@OneToOne(mappedBy = "address")
|
||||
private User user;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.hibernate.onetoone.foreignkeybased;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "username")
|
||||
private String userName;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "address_id", referencedColumnName = "id")
|
||||
private Address address;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.hibernate.onetoone.jointablebased;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "employee")
|
||||
public class Employee {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "ename")
|
||||
private String name;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
@JoinTable(name = "emp_workstation", joinColumns = {@JoinColumn(name = "employee_id", referencedColumnName = "id")},
|
||||
inverseJoinColumns = {@JoinColumn(name = "workstation_id", referencedColumnName = "id")})
|
||||
private WorkStation workStation;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public WorkStation getWorkStation() {
|
||||
return workStation;
|
||||
}
|
||||
|
||||
public void setWorkStation(WorkStation workStation) {
|
||||
this.workStation = workStation;
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.hibernate.onetoone.jointablebased;
|
||||
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "workstation")
|
||||
public class WorkStation {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "workstation_number")
|
||||
private Integer workstationNumber;
|
||||
|
||||
@Column(name = "floor")
|
||||
private String floor;
|
||||
|
||||
@OneToOne(mappedBy = "workStation")
|
||||
private Employee employee;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getWorkstationNumber() {
|
||||
return workstationNumber;
|
||||
}
|
||||
|
||||
public void setWorkstationNumber(Integer workstationNumber) {
|
||||
this.workstationNumber = workstationNumber;
|
||||
}
|
||||
|
||||
public String getFloor() {
|
||||
return floor;
|
||||
}
|
||||
|
||||
public void setFloor(String floor) {
|
||||
this.floor = floor;
|
||||
}
|
||||
|
||||
public Employee getEmployee() {
|
||||
return employee;
|
||||
}
|
||||
|
||||
public void setEmployee(Employee employee) {
|
||||
this.employee = employee;
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.hibernate.onetoone.sharedkeybased;
|
||||
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.MapsId;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "address")
|
||||
public class Address {
|
||||
|
||||
@Id
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "street")
|
||||
private String street;
|
||||
|
||||
@Column(name = "city")
|
||||
private String city;
|
||||
|
||||
@OneToOne
|
||||
@MapsId
|
||||
private User user;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.hibernate.onetoone.sharedkeybased;
|
||||
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "username")
|
||||
private String userName;
|
||||
|
||||
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
|
||||
private Address address;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.hibernate.optimisticlocking;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class OptimisticLockingCourse {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToOne
|
||||
@JoinTable(name = "optimistic_student_course")
|
||||
private OptimisticLockingStudent student;
|
||||
|
||||
public OptimisticLockingCourse(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public OptimisticLockingCourse() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public OptimisticLockingStudent getStudent() {
|
||||
return student;
|
||||
}
|
||||
|
||||
public void setStudent(OptimisticLockingStudent student) {
|
||||
this.student = student;
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.hibernate.optimisticlocking;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class OptimisticLockingStudent {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String lastName;
|
||||
@Version
|
||||
private Integer version;
|
||||
|
||||
@OneToMany(mappedBy = "student")
|
||||
private List<OptimisticLockingCourse> courses;
|
||||
|
||||
public OptimisticLockingStudent(Long id, String name, String lastName, List<OptimisticLockingCourse> courses) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.lastName = lastName;
|
||||
this.courses = courses;
|
||||
}
|
||||
|
||||
public OptimisticLockingStudent() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Integer version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public List<OptimisticLockingCourse> getCourses() {
|
||||
return courses;
|
||||
}
|
||||
|
||||
public void setCourses(List<OptimisticLockingCourse> courses) {
|
||||
this.courses = courses;
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class Address {
|
||||
|
||||
private String country;
|
||||
private String city;
|
||||
|
||||
public Address(String country, String city) {
|
||||
this.country = country;
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public Address() {
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Customer {
|
||||
|
||||
@Id
|
||||
private Long customerId;
|
||||
private String name;
|
||||
private String lastName;
|
||||
@ElementCollection
|
||||
@CollectionTable(name = "customer_address")
|
||||
private List<Address> addressList;
|
||||
|
||||
public Customer() {
|
||||
}
|
||||
|
||||
public Customer(Long customerId, String name, String lastName, List<Address> addressList) {
|
||||
this.customerId = customerId;
|
||||
this.name = name;
|
||||
this.lastName = lastName;
|
||||
this.addressList = addressList;
|
||||
}
|
||||
|
||||
public Long getCustomerId() {
|
||||
return customerId;
|
||||
}
|
||||
|
||||
public void setCustomerId(Long customerId) {
|
||||
this.customerId = customerId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public List<Address> getAddressList() {
|
||||
return addressList;
|
||||
}
|
||||
|
||||
public void setAddressList(List<Address> addressList) {
|
||||
this.addressList = addressList;
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.JOINED)
|
||||
public class Individual {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private String name;
|
||||
private String lastName;
|
||||
|
||||
public Individual(Long id, String name, String lastName) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Individual() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class PessimisticLockingCourse {
|
||||
|
||||
@Id
|
||||
private Long courseId;
|
||||
private String name;
|
||||
@ManyToOne
|
||||
@JoinTable(name = "student_course")
|
||||
private PessimisticLockingStudent student;
|
||||
|
||||
public PessimisticLockingCourse(Long courseId, String name, PessimisticLockingStudent student) {
|
||||
this.courseId = courseId;
|
||||
this.name = name;
|
||||
this.student = student;
|
||||
}
|
||||
|
||||
public PessimisticLockingCourse() {
|
||||
}
|
||||
|
||||
public Long getCourseId() {
|
||||
return courseId;
|
||||
}
|
||||
|
||||
public void setCourseId(Long courseId) {
|
||||
this.courseId = courseId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public PessimisticLockingStudent getStudent() {
|
||||
return student;
|
||||
}
|
||||
|
||||
public void setStudent(PessimisticLockingStudent students) {
|
||||
this.student = students;
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Entity
|
||||
public class PessimisticLockingEmployee extends Individual {
|
||||
|
||||
private BigDecimal salary;
|
||||
|
||||
public PessimisticLockingEmployee(Long id, String name, String lastName, BigDecimal salary) {
|
||||
super(id, name, lastName);
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
public PessimisticLockingEmployee() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BigDecimal getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(BigDecimal average) {
|
||||
this.salary = average;
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class PessimisticLockingStudent {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private String name;
|
||||
@OneToMany(mappedBy = "student")
|
||||
private List<PessimisticLockingCourse> courses;
|
||||
|
||||
public PessimisticLockingStudent(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public PessimisticLockingStudent() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<PessimisticLockingCourse> getCourses() {
|
||||
return courses;
|
||||
}
|
||||
|
||||
public void setCourses(List<PessimisticLockingCourse> courses) {
|
||||
this.courses = courses;
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.hibernate.pojo;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "MOVIE")
|
||||
public class Movie {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String movieName;
|
||||
|
||||
private Integer releaseYear;
|
||||
|
||||
private String language;
|
||||
|
||||
public String getMovieName() {
|
||||
return movieName;
|
||||
}
|
||||
|
||||
public void setMovieName(String movieName) {
|
||||
this.movieName = movieName;
|
||||
}
|
||||
|
||||
public Integer getReleaseYear() {
|
||||
return releaseYear;
|
||||
}
|
||||
|
||||
public void setReleaseYear(Integer releaseYear) {
|
||||
this.releaseYear = releaseYear;
|
||||
}
|
||||
|
||||
public String getLanguage() {
|
||||
return language;
|
||||
}
|
||||
|
||||
public void setLanguage(String language) {
|
||||
this.language = language;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user