Merge branch 'master' into JAVA-3576

This commit is contained in:
freelansam
2021-04-10 16:21:13 +05:30
committed by GitHub
104 changed files with 1789 additions and 283 deletions

View File

@@ -2,3 +2,4 @@
- [Getting Database URL From JDBC Connection Object](https://www.baeldung.com/jdbc-get-url-from-connection)
- [JDBC URL Format For Different Databases](https://www.baeldung.com/java-jdbc-url-format)
- [How to Check if a Database Table Exists with JDBC](https://www.baeldung.com/jdbc-check-table-exists)

View File

@@ -0,0 +1,103 @@
package com.baeldung.hibernate.lazycollection.model;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OrderColumn;
import javax.persistence.OneToMany;
import javax.persistence.Entity;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Branch {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
public Branch() {
}
public Branch(String name) {
this.name = name;
}
@OneToMany(mappedBy = "mainBranch")
@LazyCollection(LazyCollectionOption.TRUE)
private List<Employee> mainEmployees;
@OneToMany(mappedBy = "subBranch")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Employee> subEmployees;
@OneToMany(mappedBy = "additionalBranch")
@LazyCollection(LazyCollectionOption.EXTRA)
@OrderColumn(name = "order_id")
private List<Employee> additionalEmployees;
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<Employee> getMainEmployees() {
return mainEmployees;
}
public void setMainEmployees(List<Employee> mainEmployees) {
this.mainEmployees = mainEmployees;
}
public List<Employee> getSubEmployees() {
return subEmployees;
}
public void setSubEmployees(List<Employee> subEmployees) {
this.subEmployees = subEmployees;
}
public List<Employee> getAdditionalEmployees() {
return additionalEmployees;
}
public void setAdditionalEmployees(List<Employee> additionalEmployees) {
this.additionalEmployees = additionalEmployees;
}
public void addMainEmployee(Employee employee) {
if (this.mainEmployees == null) {
this.mainEmployees = new ArrayList<>();
}
this.mainEmployees.add(employee);
}
public void addSubEmployee(Employee employee) {
if (this.subEmployees == null) {
this.subEmployees = new ArrayList<>();
}
this.subEmployees.add(employee);
}
public void addAdditionalEmployee(Employee employee) {
if (this.additionalEmployees == null) {
this.additionalEmployees = new ArrayList<>();
}
this.additionalEmployees.add(employee);
}
}

View File

@@ -0,0 +1,88 @@
package com.baeldung.hibernate.lazycollection.model;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OrderColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Entity;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String address;
public Employee() {
}
public Employee(String name, Branch mainBranch, Branch subBranch, Branch additionalBranch) {
this.name = name;
this.mainBranch = mainBranch;
this.subBranch = subBranch;
this.additionalBranch = additionalBranch;
}
@ManyToOne
private Branch mainBranch;
@ManyToOne
private Branch subBranch;
@ManyToOne
private Branch additionalBranch;
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Branch getMainBranch() {
return mainBranch;
}
public void setMainBranch(Branch mainBranch) {
this.mainBranch = mainBranch;
}
public Branch getSubBranch() {
return subBranch;
}
public void setSubBranch(Branch subBranch) {
this.subBranch = subBranch;
}
public Branch getAdditionalBranch() {
return additionalBranch;
}
public void setAdditionalBranch(Branch additionalBranch) {
this.additionalBranch = additionalBranch;
}
}

View File

@@ -0,0 +1,110 @@
package com.baeldung.hibernate.lazycollection;
import com.baeldung.hibernate.lazycollection.model.Branch;
import com.baeldung.hibernate.lazycollection.model.Employee;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.annotation.PostConstruct;
public class LazyCollectionIntegrationTest {
private static SessionFactory sessionFactory;
private Session session;
Branch branch;
@BeforeClass
public static void beforeTests() {
Configuration configuration = new Configuration().addAnnotatedClass(Branch.class)
.addAnnotatedClass(Employee.class).setProperty("hibernate.dialect", H2Dialect.class.getName())
.setProperty("hibernate.connection.driver_class", org.h2.Driver.class.getName())
.setProperty("hibernate.connection.url", "jdbc:h2:mem:test")
.setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "")
.setProperty("hibernate.hbm2ddl.auto", "update");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
@Before
public void setUp() {
session = sessionFactory.openSession();
session.beginTransaction();
branch = new Branch("Main Branch");
session.save(branch);
Employee mainEmployee1 = new Employee("main employee 1", branch, null, null);
Employee mainEmployee2 = new Employee("main employee 2", branch, null, null);
Employee mainEmployee3 = new Employee("main employee 3", branch, null, null);
session.save(mainEmployee1);
session.save(mainEmployee2);
session.save(mainEmployee3);
Employee subEmployee1 = new Employee("sub employee 1", null, branch, null);
Employee subEmployee2 = new Employee("sub employee 2", null, branch, null);
Employee subEmployee3 = new Employee("sub employee 3", null, branch, null);
session.save(subEmployee1);
session.save(subEmployee2);
session.save(subEmployee3);
Employee additionalEmployee1 = new Employee("additional employee 1", null, null, branch);
Employee additionalEmployee2 = new Employee("additional employee 2", null, null, branch);
Employee additionalEmployee3 = new Employee("additional employee 3", null, null, branch);
session.save(additionalEmployee1);
session.save(additionalEmployee2);
session.save(additionalEmployee3);
session.flush();
session.refresh(branch);
session.getTransaction().commit();
session.close();
}
@Test
public void testLazyFetching() {
Assert.assertFalse(Hibernate.isInitialized(branch.getMainEmployees()));
}
@Test
public void testEagerFetching() {
Assert.assertTrue(Hibernate.isInitialized(branch.getSubEmployees()));
}
@Test
public void testExtraFetching() {
Assert.assertFalse(Hibernate.isInitialized(branch.getAdditionalEmployees()));
}
@After
public void tearDown() {
session.getTransaction().commit();
session.close();
}
@AfterClass
public static void afterTests() {
sessionFactory.close();
}
}

View File

@@ -9,6 +9,7 @@ import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry;
public class HibernateConfig {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
@@ -16,12 +17,12 @@ public class HibernateConfig {
Configuration configuration = new Configuration();
Properties settings = new Properties();
settings.put(Environment.DRIVER, "com.mysql.cj.jdbc.Driver");
settings.put(Environment.URL, "jdbc:mysql://localhost:3306/app_db?useSSL=false");
settings.put(Environment.USER, "root");
settings.put(Environment.PASS, "password");
settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
settings.put(Environment.DRIVER, "org.h2.Driver");
settings.put(Environment.URL, "jdbc:h2:mem:test");
settings.put(Environment.USER, "sa");
settings.put(Environment.PASS, "");
settings.put(Environment.DIALECT, "org.hibernate.dialect.H2Dialect");
settings.put(Environment.HBM2DDL_AUTO, "create-drop");
configuration.setProperties(settings);
configuration.addAnnotatedClass(User.class);

View File

@@ -1,18 +1,24 @@
package com.baeldung.ignorable.fields;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.io.*;
import java.util.List;
import java.util.Random;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class TransientFieldUnitTest {
@@ -34,18 +40,20 @@ public class TransientFieldUnitTest {
@Test
public void givenFieldWithTransientAnnotation_whenSerializingObject_thenFieldSerialized() throws IOException, ClassNotFoundException {
FileOutputStream fout = new FileOutputStream("test.obj");
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(user);
out.flush();
out.close();
try (FileOutputStream fout = new FileOutputStream("test.obj")) {
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(user);
out.flush();
}
FileInputStream fin = new FileInputStream("test.obj");
ObjectInputStream in = new ObjectInputStream(fin);
User savedUser = (User) in.readObject();
in.close();
try (FileInputStream fin = new FileInputStream("test.obj")) {
ObjectInputStream in = new ObjectInputStream(fin);
User savedUser = (User) in.readObject();
assertEquals(user.getCurrentDevice(), savedUser.getCurrentDevice());
assertEquals(user.getCurrentDevice(), savedUser.getCurrentDevice());
}
Files.deleteIfExists(Paths.get("test.obj"));
}
@Test

View File

@@ -55,7 +55,6 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec-maven-plugin.version}</version>
<executions>
<execution>
<id>document</id>

View File

@@ -38,6 +38,7 @@
<module>java-jdbi</module>
<module>java-jpa</module> <!-- long running -->
<module>java-jpa-2</module> <!-- long running -->
<module>java-jpa-3</module>
<module>java-mongodb</module> <!-- long running -->
<module>jnosql</module> <!-- long running -->
<module>jooq</module>

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@@ -6,18 +6,15 @@ import com.baeldung.projection.repository.PersonRepository;
import com.baeldung.projection.view.AddressView;
import com.baeldung.projection.view.PersonDto;
import com.baeldung.projection.view.PersonView;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;
@DataJpaTest
@RunWith(SpringRunner.class)
@Sql(scripts = "/projection-insert-data.sql")
@Sql(scripts = "/projection-clean-up-data.sql", executionPhase = AFTER_TEST_METHOD)
public class JpaProjectionIntegrationTest {

View File

@@ -5,6 +5,7 @@ This module contains articles about Spring with jOOQ
### Relevant Articles:
- [Spring Boot Support for jOOQ](https://www.baeldung.com/spring-boot-support-for-jooq)
- [Introduction to jOOQ with Spring](https://www.baeldung.com/jooq-with-spring)
- [Count Query In jOOQ](https://www.baeldung.com/jooq-count-query)
In order to fix the error "Plugin execution not covered by lifecycle configuration: org.jooq:jooq-codegen-maven:3.7.3:generate (execution: default, phase: generate-sources)", right-click on the error message and choose "Mark goal generated as ignore in pom.xml". Until version 1.4.x, the maven-plugin-plugin was covered by the default lifecycle mapping that ships with m2e.