Merge branch 'master' into BAEL-4590
This commit is contained in:
@@ -11,9 +11,8 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<artifactId>persistence-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jdbc;
|
||||
package com.baeldung.spring.jdbc;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.UUID;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jdbc;
|
||||
package com.baeldung.spring.jdbc;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jdbc.joins;
|
||||
package com.baeldung.spring.jdbc.joins;
|
||||
|
||||
class ArticleWithAuthor {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jdbc.joins;
|
||||
package com.baeldung.spring.jdbc.joins;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jdbc;
|
||||
package com.baeldung.spring.jdbc;
|
||||
|
||||
|
||||
import org.junit.Before;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jdbc;
|
||||
package com.baeldung.spring.jdbc;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jdbc;
|
||||
package com.baeldung.spring.jdbc;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.junit.After;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jdbc;
|
||||
package com.baeldung.spring.jdbc;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jdbc.joins;
|
||||
package com.baeldung.spring.jdbc.joins;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -188,13 +188,6 @@
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Needed for running tests (you may also use TestNG) -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Others -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
|
||||
@@ -75,6 +75,11 @@
|
||||
|
||||
<properties>
|
||||
<flyway.configFiles>src/main/resources/application-${spring-boot.run.profiles}.properties</flyway.configFiles>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
### Relevant Articles:
|
||||
- [Database Migrations with Flyway](http://www.baeldung.com/database-migrations-with-flyway)
|
||||
- [A Guide to Flyway Callbacks](http://www.baeldung.com/flyway-callbacks)
|
||||
- [Rolling Back Migrations with Flyway](https://www.baeldung.com/flyway-roll-back)
|
||||
@@ -65,6 +65,11 @@
|
||||
<properties>
|
||||
<flyway-core.version>5.2.3</flyway-core.version>
|
||||
<flyway-maven-plugin.version>5.0.2</flyway-maven-plugin.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
create table book (
|
||||
id numeric,
|
||||
title varchar(128),
|
||||
author varchar(256),
|
||||
constraint pk_book primary key (id)
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
drop table book;
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.flywayundo;
|
||||
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.flywaydb.core.api.MigrationInfo;
|
||||
import org.flywaydb.core.api.MigrationState;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = FlywayUndoTestConfig.class)
|
||||
@SpringBootTest
|
||||
public class FlywayUndoMigrationIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Test
|
||||
public void givenMigrationsExist_whenApplyMigrations_migrationsAreSuccessful() {
|
||||
Flyway flyway = Flyway.configure()
|
||||
.dataSource(dataSource)
|
||||
.schemas("undo")
|
||||
.locations("db/undo")
|
||||
.load();
|
||||
|
||||
flyway.migrate();
|
||||
|
||||
for (MigrationInfo info : flyway.info().all()) {
|
||||
assertThat(info.getState()).isEqualTo(MigrationState.SUCCESS);
|
||||
}
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.flywayundo;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
public class FlywayUndoTestConfig {
|
||||
|
||||
@Bean
|
||||
public DataSource createDatasource() {
|
||||
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
||||
return dbBuilder.setType(EmbeddedDatabaseType.H2)
|
||||
.setName("DATABASE")
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,4 +9,5 @@ This module contains articles about enterprise concerns such as Multitenancy, Er
|
||||
- [Hibernate Aggregate Functions](https://www.baeldung.com/hibernate-aggregate-functions)
|
||||
- [Common Hibernate Exceptions](https://www.baeldung.com/hibernate-exceptions)
|
||||
- [Hibernate Error “Not all named parameters have been set”](https://www.baeldung.com/hibernate-error-named-parameters-not-set)
|
||||
- [Various Logging Levels in Hibernate](https://www.baeldung.com/hibernate-logging-levels)
|
||||
- [Various Logging Levels in Hibernate](https://www.baeldung.com/hibernate-logging-levels)
|
||||
- [Hibernate: save, persist, update, merge, saveOrUpdate](https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate)
|
||||
@@ -61,13 +61,25 @@
|
||||
<version>${byte-buddy.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<version>${hsqldb.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>geodb-repo</id>
|
||||
<name>GeoDB repository</name>
|
||||
<url>http://repo.boundlessgeo.com/main/</url>
|
||||
<id>osgeo</id>
|
||||
<name>OSGeo Release Repository</name>
|
||||
<url>https://repo.osgeo.org/repository/release/</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
@@ -77,6 +89,7 @@
|
||||
<mariaDB4j.version>2.2.3</mariaDB4j.version>
|
||||
<assertj-core.version>3.8.0</assertj-core.version>
|
||||
<geodb.version>0.9</geodb.version>
|
||||
<hsqldb.version>2.3.4</hsqldb.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
+58
-29
@@ -1,6 +1,14 @@
|
||||
package com.baeldung.persistence.save;
|
||||
|
||||
import com.baeldung.persistence.model.Person;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
@@ -8,9 +16,13 @@ import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.dialect.HSQLDialect;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
import org.junit.*;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import com.baeldung.persistence.model.Person;
|
||||
|
||||
/**
|
||||
* Testing specific implementation details for different methods:
|
||||
@@ -21,12 +33,19 @@ public class SaveMethodsIntegrationTest {
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private Session session;
|
||||
private boolean doNotCommit = false;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
Configuration configuration = new Configuration().addAnnotatedClass(Person.class).setProperty("hibernate.dialect", HSQLDialect.class.getName()).setProperty("hibernate.connection.driver_class", org.hsqldb.jdbcDriver.class.getName())
|
||||
.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test").setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "").setProperty("hibernate.hbm2ddl.auto", "update");
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
|
||||
Configuration configuration = new Configuration().addAnnotatedClass(Person.class)
|
||||
.setProperty("hibernate.dialect", HSQLDialect.class.getName())
|
||||
.setProperty("hibernate.connection.driver_class", org.hsqldb.jdbcDriver.class.getName())
|
||||
.setProperty("hibernate.connection.url", "jdbc:hsqldb: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);
|
||||
}
|
||||
|
||||
@@ -34,6 +53,7 @@ public class SaveMethodsIntegrationTest {
|
||||
public void setUp() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
doNotCommit = false;
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -43,7 +63,8 @@ public class SaveMethodsIntegrationTest {
|
||||
person.setName("John");
|
||||
session.persist(person);
|
||||
|
||||
session.getTransaction().commit();
|
||||
session.getTransaction()
|
||||
.commit();
|
||||
session.close();
|
||||
|
||||
session = sessionFactory.openSession();
|
||||
@@ -68,15 +89,33 @@ public class SaveMethodsIntegrationTest {
|
||||
assertEquals(id1, id2);
|
||||
}
|
||||
|
||||
@Test(expected = HibernateException.class)
|
||||
@Test(expected = PersistenceException.class)
|
||||
public void whenPersistDetached_thenThrowsException() {
|
||||
|
||||
doNotCommit = true;
|
||||
|
||||
Person person = new Person();
|
||||
person.setName("John");
|
||||
session.persist(person);
|
||||
session.evict(person);
|
||||
|
||||
|
||||
session.persist(person);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMergeDetached_thenEntityUpdatedFromDatabase() {
|
||||
|
||||
Person person = new Person();
|
||||
person.setName("John");
|
||||
session.save(person);
|
||||
session.flush();
|
||||
session.evict(person);
|
||||
|
||||
person.setName("Mary");
|
||||
Person mergedPerson = (Person) session.merge(person);
|
||||
|
||||
assertNotSame(person, mergedPerson);
|
||||
assertEquals("Mary", mergedPerson.getName());
|
||||
|
||||
}
|
||||
|
||||
@@ -92,7 +131,8 @@ public class SaveMethodsIntegrationTest {
|
||||
|
||||
assertNotNull(id);
|
||||
|
||||
session.getTransaction().commit();
|
||||
session.getTransaction()
|
||||
.commit();
|
||||
session.close();
|
||||
|
||||
assertEquals(id, person.getId());
|
||||
@@ -128,22 +168,6 @@ public class SaveMethodsIntegrationTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMergeDetached_thenEntityUpdatedFromDatabase() {
|
||||
|
||||
Person person = new Person();
|
||||
person.setName("John");
|
||||
session.save(person);
|
||||
session.evict(person);
|
||||
|
||||
person.setName("Mary");
|
||||
Person mergedPerson = (Person) session.merge(person);
|
||||
|
||||
assertNotSame(person, mergedPerson);
|
||||
assertEquals("Mary", mergedPerson.getName());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMergeTransient_thenNewEntitySavedToDatabase() {
|
||||
|
||||
@@ -151,7 +175,8 @@ public class SaveMethodsIntegrationTest {
|
||||
person.setName("John");
|
||||
Person mergedPerson = (Person) session.merge(person);
|
||||
|
||||
session.getTransaction().commit();
|
||||
session.getTransaction()
|
||||
.commit();
|
||||
session.beginTransaction();
|
||||
|
||||
assertNull(person.getId());
|
||||
@@ -227,7 +252,8 @@ public class SaveMethodsIntegrationTest {
|
||||
person.setName("John");
|
||||
session.saveOrUpdate(person);
|
||||
|
||||
session.getTransaction().commit();
|
||||
session.getTransaction()
|
||||
.commit();
|
||||
session.close();
|
||||
|
||||
session = sessionFactory.openSession();
|
||||
@@ -250,7 +276,10 @@ public class SaveMethodsIntegrationTest {
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
session.getTransaction().commit();
|
||||
if (!doNotCommit) {
|
||||
session.getTransaction()
|
||||
.commit();
|
||||
}
|
||||
session.close();
|
||||
}
|
||||
|
||||
@@ -170,7 +170,6 @@
|
||||
<guava.version>29.0-jre</guava.version>
|
||||
<hibernate-types.version>2.9.7</hibernate-types.version>
|
||||
<hibernate.version>5.4.14.Final</hibernate.version>
|
||||
<jackson.version>2.10.3</jackson.version>
|
||||
<javassist.version>3.27.0-GA</javassist.version>
|
||||
<jaxb.version>2.3.1</jaxb.version>
|
||||
<log4jdbc.version>2.0.0</log4jdbc.version>
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Getting Started with jOOQ](https://www.baeldung.com/jooq-intro)
|
||||
@@ -0,0 +1,46 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>jooq</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>jooq-examples</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>jOOQ Examples</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>persistence-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jooq</groupId>
|
||||
<artifactId>jooq</artifactId>
|
||||
<version>3.13.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jooq</groupId>
|
||||
<artifactId>jooq-meta</artifactId>
|
||||
<version>3.13.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jooq</groupId>
|
||||
<artifactId>jooq-codegen</artifactId>
|
||||
<version>3.13.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.2.16</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.200</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.jooq;
|
||||
|
||||
import org.jooq.Condition;
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.Field;
|
||||
import org.jooq.Record;
|
||||
import org.jooq.Result;
|
||||
import org.jooq.SelectFieldOrAsterisk;
|
||||
import org.jooq.Table;
|
||||
import org.jooq.UpdatableRecord;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class Crud {
|
||||
|
||||
public static <R extends UpdatableRecord<R>> void save(UpdatableRecord<R> record) {
|
||||
record.store();
|
||||
}
|
||||
|
||||
public static Result<Record> getAll(DSLContext context, Table<? extends Record> table) {
|
||||
return context.select()
|
||||
.from(table)
|
||||
.fetch();
|
||||
}
|
||||
|
||||
public static Result<Record> getFields(DSLContext context, Table<? extends Record> table, SelectFieldOrAsterisk... fields) {
|
||||
return context.select(fields)
|
||||
.from(table)
|
||||
.fetch();
|
||||
}
|
||||
|
||||
public static <R extends Record> R getOne(DSLContext context, Table<R> table, Condition condition) {
|
||||
return context.fetchOne(table, condition);
|
||||
}
|
||||
|
||||
public static <T> void update(DSLContext context, Table<? extends Record> table, Map<Field<T>, T> values, Condition condition) {
|
||||
context.update(table)
|
||||
.set(values)
|
||||
.where(condition)
|
||||
.execute();
|
||||
}
|
||||
|
||||
public static <R extends UpdatableRecord<R>> void update(UpdatableRecord<R> record) {
|
||||
record.update();
|
||||
}
|
||||
|
||||
public static void delete(DSLContext context, Table<? extends Record> table, Condition condition) {
|
||||
context.delete(table)
|
||||
.where(condition)
|
||||
.execute();
|
||||
}
|
||||
|
||||
public static <R extends UpdatableRecord<R>> void delete(UpdatableRecord<R> record) {
|
||||
record.delete();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package com.baeldung.jooq;
|
||||
|
||||
import com.baeldung.jooq.model.tables.Article;
|
||||
import com.baeldung.jooq.model.tables.Author;
|
||||
import com.baeldung.jooq.model.tables.records.ArticleRecord;
|
||||
import com.baeldung.jooq.model.tables.records.AuthorRecord;
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.Field;
|
||||
import org.jooq.Record;
|
||||
import org.jooq.Result;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.impl.DSL;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static com.baeldung.jooq.Crud.delete;
|
||||
import static com.baeldung.jooq.Crud.getAll;
|
||||
import static com.baeldung.jooq.Crud.getFields;
|
||||
import static com.baeldung.jooq.Crud.getOne;
|
||||
import static com.baeldung.jooq.Crud.save;
|
||||
import static com.baeldung.jooq.Crud.update;
|
||||
|
||||
public class CrudExamples {
|
||||
|
||||
public void crudExamples() throws SQLException {
|
||||
String userName = "username";
|
||||
String password = "password";
|
||||
String url = "jdbc:postgresql://db_url:5432/baeldung_database";
|
||||
|
||||
Connection conn = DriverManager.getConnection(url, userName, password);
|
||||
DSLContext context = DSL.using(conn, SQLDialect.POSTGRES);
|
||||
|
||||
createValues(context);
|
||||
readValues(context);
|
||||
updateValues(context);
|
||||
deleteValues(context);
|
||||
}
|
||||
|
||||
private void createValues(DSLContext context) {
|
||||
ArticleRecord article = context.newRecord(Article.ARTICLE);
|
||||
|
||||
article.setId(2);
|
||||
article.setTitle("jOOQ examples");
|
||||
article.setDescription("A few examples of jOOQ CRUD operations");
|
||||
article.setAuthorId(1);
|
||||
|
||||
save(article);
|
||||
|
||||
AuthorRecord author = context.newRecord(Author.AUTHOR);
|
||||
author.setId(1);
|
||||
author.setFirstName("John");
|
||||
author.setLastName("Smith");
|
||||
author.setAge(40);
|
||||
|
||||
save(author);
|
||||
}
|
||||
|
||||
private void readValues(DSLContext context) {
|
||||
Result<Record> authors = getAll(
|
||||
context,
|
||||
Author.AUTHOR
|
||||
);
|
||||
|
||||
authors.forEach(author -> {
|
||||
Integer id = author.getValue(Author.AUTHOR.ID);
|
||||
String firstName = author.getValue(Author.AUTHOR.FIRST_NAME);
|
||||
String lastName = author.getValue(Author.AUTHOR.LAST_NAME);
|
||||
Integer age = author.getValue(Author.AUTHOR.AGE);
|
||||
System.out.printf("Author %s %s has id: %d and age: %d%n", firstName, lastName, id, age);
|
||||
});
|
||||
|
||||
Result<Record> articles = getFields(
|
||||
context,
|
||||
Author.AUTHOR,
|
||||
Article.ARTICLE.ID, Article.ARTICLE.TITLE
|
||||
);
|
||||
|
||||
AuthorRecord author = getOne(
|
||||
context,
|
||||
Author.AUTHOR,
|
||||
Author.AUTHOR.ID.eq(1)
|
||||
);
|
||||
}
|
||||
|
||||
private void updateValues(DSLContext context) {
|
||||
HashMap<Field<String>, String> fieldsToUpdate = new HashMap<>();
|
||||
fieldsToUpdate.put(Author.AUTHOR.FIRST_NAME, "David");
|
||||
fieldsToUpdate.put(Author.AUTHOR.LAST_NAME, "Brown");
|
||||
update(
|
||||
context,
|
||||
Author.AUTHOR,
|
||||
fieldsToUpdate,
|
||||
Author.AUTHOR.ID.eq(1)
|
||||
);
|
||||
|
||||
ArticleRecord article = context.fetchOne(Article.ARTICLE, Article.ARTICLE.ID.eq(1));
|
||||
article.setTitle("A New Article Title");
|
||||
update(article);
|
||||
}
|
||||
|
||||
private void deleteValues(DSLContext context) {
|
||||
delete(
|
||||
context,
|
||||
Article.ARTICLE,
|
||||
Article.ARTICLE.ID.eq(1)
|
||||
);
|
||||
|
||||
AuthorRecord author = context.fetchOne(Author.AUTHOR, Author.AUTHOR.ID.eq(1));
|
||||
delete(author);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* This file is generated by jOOQ.
|
||||
*/
|
||||
package com.baeldung.jooq.model;
|
||||
|
||||
|
||||
import org.jooq.Schema;
|
||||
import org.jooq.impl.CatalogImpl;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* This class is generated by jOOQ.
|
||||
*/
|
||||
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
|
||||
public class DefaultCatalog extends CatalogImpl {
|
||||
|
||||
private static final long serialVersionUID = 1035293962;
|
||||
|
||||
/**
|
||||
* The reference instance of <code>DEFAULT_CATALOG</code>
|
||||
*/
|
||||
public static final DefaultCatalog DEFAULT_CATALOG = new DefaultCatalog();
|
||||
|
||||
/**
|
||||
* The schema <code>public</code>.
|
||||
*/
|
||||
public final Public PUBLIC = Public.PUBLIC;
|
||||
|
||||
/**
|
||||
* No further instances allowed
|
||||
*/
|
||||
private DefaultCatalog() {
|
||||
super("");
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Schema> getSchemas() {
|
||||
return Arrays.<Schema>asList(
|
||||
Public.PUBLIC);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* This file is generated by jOOQ.
|
||||
*/
|
||||
package com.baeldung.jooq.model;
|
||||
|
||||
|
||||
import com.baeldung.jooq.model.tables.Article;
|
||||
import com.baeldung.jooq.model.tables.Author;
|
||||
import com.baeldung.jooq.model.tables.records.ArticleRecord;
|
||||
import com.baeldung.jooq.model.tables.records.AuthorRecord;
|
||||
import org.jooq.ForeignKey;
|
||||
import org.jooq.TableField;
|
||||
import org.jooq.UniqueKey;
|
||||
import org.jooq.impl.Internal;
|
||||
|
||||
|
||||
/**
|
||||
* A class modelling foreign key relationships and constraints of tables of
|
||||
* the <code>public</code> schema.
|
||||
*/
|
||||
@SuppressWarnings({"all", "unchecked", "rawtypes"})
|
||||
public class Keys {
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// UNIQUE and PRIMARY KEY definitions
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
public static final UniqueKey<ArticleRecord> ARTICLE_PKEY = UniqueKeys0.ARTICLE_PKEY;
|
||||
public static final UniqueKey<AuthorRecord> AUTHOR_PKEY = UniqueKeys0.AUTHOR_PKEY;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// FOREIGN KEY definitions
|
||||
// -------------------------------------------------------------------------
|
||||
public static final ForeignKey<ArticleRecord, AuthorRecord> ARTICLE__XXX = ForeignKeys0.ARTICLE__XXX;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// [#1459] distribute members to avoid static initialisers > 64kb
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private static class UniqueKeys0 {
|
||||
public static final UniqueKey<ArticleRecord> ARTICLE_PKEY = Internal.createUniqueKey(Article.ARTICLE, "article_pkey", new TableField[]{Article.ARTICLE.ID}, true);
|
||||
public static final UniqueKey<AuthorRecord> AUTHOR_PKEY = Internal.createUniqueKey(Author.AUTHOR, "author_pkey", new TableField[]{Author.AUTHOR.ID}, true);
|
||||
}
|
||||
|
||||
private static class ForeignKeys0 {
|
||||
public static final ForeignKey<ArticleRecord, AuthorRecord> ARTICLE__XXX = Internal.createForeignKey(Keys.AUTHOR_PKEY, Article.ARTICLE, "xxx", new TableField[]{Article.ARTICLE.AUTHOR_ID}, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* This file is generated by jOOQ.
|
||||
*/
|
||||
package com.baeldung.jooq.model;
|
||||
|
||||
import com.baeldung.jooq.model.tables.Article;
|
||||
import com.baeldung.jooq.model.tables.Author;
|
||||
import org.jooq.Catalog;
|
||||
import org.jooq.Table;
|
||||
import org.jooq.impl.SchemaImpl;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* This class is generated by jOOQ.
|
||||
*/
|
||||
@SuppressWarnings({"all", "unchecked", "rawtypes"})
|
||||
public class Public extends SchemaImpl {
|
||||
|
||||
private static final long serialVersionUID = -2049410122;
|
||||
|
||||
/**
|
||||
* The reference instance of <code>public</code>
|
||||
*/
|
||||
public static final Public PUBLIC = new Public();
|
||||
|
||||
/**
|
||||
* The table <code>public.article</code>.
|
||||
*/
|
||||
public final Article ARTICLE = Article.ARTICLE;
|
||||
|
||||
/**
|
||||
* The table <code>public.author</code>.
|
||||
*/
|
||||
public final Author AUTHOR = Author.AUTHOR;
|
||||
|
||||
/**
|
||||
* No further instances allowed
|
||||
*/
|
||||
private Public() {
|
||||
super("public", null);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Catalog getCatalog() {
|
||||
return DefaultCatalog.DEFAULT_CATALOG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Table<?>> getTables() {
|
||||
return Arrays.<Table<?>>asList(
|
||||
Article.ARTICLE,
|
||||
Author.AUTHOR
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* This file is generated by jOOQ.
|
||||
*/
|
||||
package com.baeldung.jooq.model;
|
||||
|
||||
import com.baeldung.jooq.model.tables.Article;
|
||||
import com.baeldung.jooq.model.tables.Author;
|
||||
|
||||
/**
|
||||
* Convenience access to all tables in public
|
||||
*/
|
||||
@SuppressWarnings({"all", "unchecked", "rawtypes"})
|
||||
public class Tables {
|
||||
|
||||
/**
|
||||
* The table <code>public.article</code>.
|
||||
*/
|
||||
public static final Article ARTICLE = Article.ARTICLE;
|
||||
|
||||
/**
|
||||
* The table <code>public.author</code>.
|
||||
*/
|
||||
public static final Author AUTHOR = Author.AUTHOR;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* This file is generated by jOOQ.
|
||||
*/
|
||||
package com.baeldung.jooq.model.tables;
|
||||
|
||||
|
||||
import com.baeldung.jooq.model.Keys;
|
||||
import com.baeldung.jooq.model.Public;
|
||||
import com.baeldung.jooq.model.tables.records.ArticleRecord;
|
||||
import org.jooq.Field;
|
||||
import org.jooq.ForeignKey;
|
||||
import org.jooq.Name;
|
||||
import org.jooq.Record;
|
||||
import org.jooq.Row4;
|
||||
import org.jooq.Schema;
|
||||
import org.jooq.Table;
|
||||
import org.jooq.TableField;
|
||||
import org.jooq.TableOptions;
|
||||
import org.jooq.UniqueKey;
|
||||
import org.jooq.impl.DSL;
|
||||
import org.jooq.impl.TableImpl;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* This class is generated by jOOQ.
|
||||
*/
|
||||
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
|
||||
public class Article extends TableImpl<ArticleRecord> {
|
||||
|
||||
private static final long serialVersionUID = -1401275800;
|
||||
|
||||
/**
|
||||
* The reference instance of <code>public.article</code>
|
||||
*/
|
||||
public static final Article ARTICLE = new Article();
|
||||
|
||||
/**
|
||||
* The class holding records for this type
|
||||
*/
|
||||
@Override
|
||||
public Class<ArticleRecord> getRecordType() {
|
||||
return ArticleRecord.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* The column <code>public.article.id</code>.
|
||||
*/
|
||||
public final TableField<ArticleRecord, Integer> ID = createField(DSL.name("id"), org.jooq.impl.SQLDataType.INTEGER.nullable(false), this, "");
|
||||
|
||||
/**
|
||||
* The column <code>public.article.title</code>.
|
||||
*/
|
||||
public final TableField<ArticleRecord, String> TITLE = createField(DSL.name("title"), org.jooq.impl.SQLDataType.VARCHAR(255).nullable(false), this, "");
|
||||
|
||||
/**
|
||||
* The column <code>public.article.description</code>.
|
||||
*/
|
||||
public final TableField<ArticleRecord, String> DESCRIPTION = createField(DSL.name("description"), org.jooq.impl.SQLDataType.VARCHAR(255), this, "");
|
||||
|
||||
/**
|
||||
* The column <code>public.article.author_id</code>.
|
||||
*/
|
||||
public final TableField<ArticleRecord, Integer> AUTHOR_ID = createField(DSL.name("author_id"), org.jooq.impl.SQLDataType.INTEGER, this, "");
|
||||
|
||||
/**
|
||||
* Create a <code>public.article</code> table reference
|
||||
*/
|
||||
public Article() {
|
||||
this(DSL.name("article"), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an aliased <code>public.article</code> table reference
|
||||
*/
|
||||
public Article(String alias) {
|
||||
this(DSL.name(alias), ARTICLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an aliased <code>public.article</code> table reference
|
||||
*/
|
||||
public Article(Name alias) {
|
||||
this(alias, ARTICLE);
|
||||
}
|
||||
|
||||
private Article(Name alias, Table<ArticleRecord> aliased) {
|
||||
this(alias, aliased, null);
|
||||
}
|
||||
|
||||
private Article(Name alias, Table<ArticleRecord> aliased, Field<?>[] parameters) {
|
||||
super(alias, null, aliased, parameters, DSL.comment(""), TableOptions.table());
|
||||
}
|
||||
|
||||
public <O extends Record> Article(Table<O> child, ForeignKey<O, ArticleRecord> key) {
|
||||
super(child, key, ARTICLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Schema getSchema() {
|
||||
return Public.PUBLIC;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UniqueKey<ArticleRecord> getPrimaryKey() {
|
||||
return Keys.ARTICLE_PKEY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UniqueKey<ArticleRecord>> getKeys() {
|
||||
return Arrays.<UniqueKey<ArticleRecord>>asList(Keys.ARTICLE_PKEY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ForeignKey<ArticleRecord, ?>> getReferences() {
|
||||
return Arrays.<ForeignKey<ArticleRecord, ?>>asList(Keys.ARTICLE__XXX);
|
||||
}
|
||||
|
||||
public Author author() {
|
||||
return new Author(this, Keys.ARTICLE__XXX);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Article as(String alias) {
|
||||
return new Article(DSL.name(alias), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Article as(Name alias) {
|
||||
return new Article(alias, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename this table
|
||||
*/
|
||||
@Override
|
||||
public Article rename(String name) {
|
||||
return new Article(DSL.name(name), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename this table
|
||||
*/
|
||||
@Override
|
||||
public Article rename(Name name) {
|
||||
return new Article(name, null);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Row4 type methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public Row4<Integer, String, String, Integer> fieldsRow() {
|
||||
return (Row4) super.fieldsRow();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* This file is generated by jOOQ.
|
||||
*/
|
||||
package com.baeldung.jooq.model.tables;
|
||||
|
||||
import com.baeldung.jooq.model.Keys;
|
||||
import com.baeldung.jooq.model.Public;
|
||||
import com.baeldung.jooq.model.tables.records.AuthorRecord;
|
||||
import org.jooq.Field;
|
||||
import org.jooq.ForeignKey;
|
||||
import org.jooq.Name;
|
||||
import org.jooq.Record;
|
||||
import org.jooq.Row4;
|
||||
import org.jooq.Schema;
|
||||
import org.jooq.Table;
|
||||
import org.jooq.TableField;
|
||||
import org.jooq.TableOptions;
|
||||
import org.jooq.UniqueKey;
|
||||
import org.jooq.impl.DSL;
|
||||
import org.jooq.impl.TableImpl;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* This class is generated by jOOQ.
|
||||
*/
|
||||
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
|
||||
public class Author extends TableImpl<AuthorRecord> {
|
||||
|
||||
private static final long serialVersionUID = -798376522;
|
||||
|
||||
/**
|
||||
* The reference instance of <code>public.author</code>
|
||||
*/
|
||||
public static final Author AUTHOR = new Author();
|
||||
|
||||
/**
|
||||
* The class holding records for this type
|
||||
*/
|
||||
@Override
|
||||
public Class<AuthorRecord> getRecordType() {
|
||||
return AuthorRecord.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* The column <code>public.author.id</code>.
|
||||
*/
|
||||
public final TableField<AuthorRecord, Integer> ID = createField(DSL.name("id"), org.jooq.impl.SQLDataType.INTEGER.nullable(false), AUTHOR, "");
|
||||
|
||||
/**
|
||||
* The column <code>public.author.first_name</code>.
|
||||
*/
|
||||
public final TableField<AuthorRecord, String> FIRST_NAME = createField(DSL.name("first_name"), org.jooq.impl.SQLDataType.VARCHAR(255), this, "");
|
||||
|
||||
/**
|
||||
* The column <code>public.author.last_name</code>.
|
||||
*/
|
||||
public final TableField<AuthorRecord, String> LAST_NAME = createField(DSL.name("last_name"), org.jooq.impl.SQLDataType.VARCHAR(255), this, "");
|
||||
|
||||
/**
|
||||
* The column <code>public.author.age</code>.
|
||||
*/
|
||||
public final TableField<AuthorRecord, Integer> AGE = createField(DSL.name("age"), org.jooq.impl.SQLDataType.INTEGER, this, "");
|
||||
|
||||
/**
|
||||
* Create a <code>public.author</code> table reference
|
||||
*/
|
||||
public Author() {
|
||||
this(DSL.name("author"), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an aliased <code>public.author</code> table reference
|
||||
*/
|
||||
public Author(String alias) {
|
||||
this(DSL.name(alias), AUTHOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an aliased <code>public.author</code> table reference
|
||||
*/
|
||||
public Author(Name alias) {
|
||||
this(alias, AUTHOR);
|
||||
}
|
||||
|
||||
private Author(Name alias, Table<AuthorRecord> aliased) {
|
||||
this(alias, aliased, null);
|
||||
}
|
||||
|
||||
private Author(Name alias, Table<AuthorRecord> aliased, Field<?>[] parameters) {
|
||||
super(alias, null, aliased, parameters, DSL.comment(""), TableOptions.table());
|
||||
}
|
||||
|
||||
public <O extends Record> Author(Table<O> child, ForeignKey<O, AuthorRecord> key) {
|
||||
super(child, key, AUTHOR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Schema getSchema() {
|
||||
return Public.PUBLIC;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UniqueKey<AuthorRecord> getPrimaryKey() {
|
||||
return Keys.AUTHOR_PKEY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UniqueKey<AuthorRecord>> getKeys() {
|
||||
return Arrays.<UniqueKey<AuthorRecord>>asList(Keys.AUTHOR_PKEY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Author as(String alias) {
|
||||
return new Author(DSL.name(alias), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Author as(Name alias) {
|
||||
return new Author(alias, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename this table
|
||||
*/
|
||||
@Override
|
||||
public Author rename(String name) {
|
||||
return new Author(DSL.name(name), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename this table
|
||||
*/
|
||||
@Override
|
||||
public Author rename(Name name) {
|
||||
return new Author(name, null);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Row4 type methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public Row4<Integer, String, String, Integer> fieldsRow() {
|
||||
return (Row4) super.fieldsRow();
|
||||
}
|
||||
}
|
||||
+218
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
* This file is generated by jOOQ.
|
||||
*/
|
||||
package com.baeldung.jooq.model.tables.records;
|
||||
|
||||
|
||||
import com.baeldung.jooq.model.tables.Article;
|
||||
|
||||
import org.jooq.Field;
|
||||
import org.jooq.Record1;
|
||||
import org.jooq.Record4;
|
||||
import org.jooq.Row4;
|
||||
import org.jooq.impl.UpdatableRecordImpl;
|
||||
|
||||
|
||||
/**
|
||||
* This class is generated by jOOQ.
|
||||
*/
|
||||
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
|
||||
public class ArticleRecord extends UpdatableRecordImpl<ArticleRecord> implements Record4<Integer, String, String, Integer> {
|
||||
|
||||
private static final long serialVersionUID = 1297442421;
|
||||
|
||||
/**
|
||||
* Setter for <code>public.article.id</code>.
|
||||
*/
|
||||
public void setId(Integer value) {
|
||||
set(0, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for <code>public.article.id</code>.
|
||||
*/
|
||||
public Integer getId() {
|
||||
return (Integer) get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for <code>public.article.title</code>.
|
||||
*/
|
||||
public void setTitle(String value) {
|
||||
set(1, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for <code>public.article.title</code>.
|
||||
*/
|
||||
public String getTitle() {
|
||||
return (String) get(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for <code>public.article.description</code>.
|
||||
*/
|
||||
public void setDescription(String value) {
|
||||
set(2, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for <code>public.article.description</code>.
|
||||
*/
|
||||
public String getDescription() {
|
||||
return (String) get(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for <code>public.article.author_id</code>.
|
||||
*/
|
||||
public void setAuthorId(Integer value) {
|
||||
set(3, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for <code>public.article.author_id</code>.
|
||||
*/
|
||||
public Integer getAuthorId() {
|
||||
return (Integer) get(3);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Primary key information
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public Record1<Integer> key() {
|
||||
return (Record1) super.key();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Record4 type implementation
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public Row4<Integer, String, String, Integer> fieldsRow() {
|
||||
return (Row4) super.fieldsRow();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Row4<Integer, String, String, Integer> valuesRow() {
|
||||
return (Row4) super.valuesRow();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Field<Integer> field1() {
|
||||
return Article.ARTICLE.ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Field<String> field2() {
|
||||
return Article.ARTICLE.TITLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Field<String> field3() {
|
||||
return Article.ARTICLE.DESCRIPTION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Field<Integer> field4() {
|
||||
return Article.ARTICLE.AUTHOR_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer component1() {
|
||||
return getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String component2() {
|
||||
return getTitle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String component3() {
|
||||
return getDescription();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer component4() {
|
||||
return getAuthorId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer value1() {
|
||||
return getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String value2() {
|
||||
return getTitle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String value3() {
|
||||
return getDescription();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer value4() {
|
||||
return getAuthorId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArticleRecord value1(Integer value) {
|
||||
setId(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArticleRecord value2(String value) {
|
||||
setTitle(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArticleRecord value3(String value) {
|
||||
setDescription(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArticleRecord value4(Integer value) {
|
||||
setAuthorId(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArticleRecord values(Integer value1, String value2, String value3, Integer value4) {
|
||||
value1(value1);
|
||||
value2(value2);
|
||||
value3(value3);
|
||||
value4(value4);
|
||||
return this;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Constructors
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Create a detached ArticleRecord
|
||||
*/
|
||||
public ArticleRecord() {
|
||||
super(Article.ARTICLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a detached, initialised ArticleRecord
|
||||
*/
|
||||
public ArticleRecord(Integer id, String title, String description, Integer authorId) {
|
||||
super(Article.ARTICLE);
|
||||
|
||||
set(0, id);
|
||||
set(1, title);
|
||||
set(2, description);
|
||||
set(3, authorId);
|
||||
}
|
||||
}
|
||||
+217
@@ -0,0 +1,217 @@
|
||||
/*
|
||||
* This file is generated by jOOQ.
|
||||
*/
|
||||
package com.baeldung.jooq.model.tables.records;
|
||||
|
||||
|
||||
import com.baeldung.jooq.model.tables.Author;
|
||||
import org.jooq.Field;
|
||||
import org.jooq.Record1;
|
||||
import org.jooq.Record4;
|
||||
import org.jooq.Row4;
|
||||
import org.jooq.impl.UpdatableRecordImpl;
|
||||
|
||||
|
||||
/**
|
||||
* This class is generated by jOOQ.
|
||||
*/
|
||||
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
|
||||
public class AuthorRecord extends UpdatableRecordImpl<AuthorRecord> implements Record4<Integer, String, String, Integer> {
|
||||
|
||||
private static final long serialVersionUID = -2120822720;
|
||||
|
||||
/**
|
||||
* Setter for <code>public.author.id</code>.
|
||||
*/
|
||||
public void setId(Integer value) {
|
||||
set(0, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for <code>public.author.id</code>.
|
||||
*/
|
||||
public Integer getId() {
|
||||
return (Integer) get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for <code>public.author.first_name</code>.
|
||||
*/
|
||||
public void setFirstName(String value) {
|
||||
set(1, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for <code>public.author.first_name</code>.
|
||||
*/
|
||||
public String getFirstName() {
|
||||
return (String) get(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for <code>public.author.last_name</code>.
|
||||
*/
|
||||
public void setLastName(String value) {
|
||||
set(2, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for <code>public.author.last_name</code>.
|
||||
*/
|
||||
public String getLastName() {
|
||||
return (String) get(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for <code>public.author.age</code>.
|
||||
*/
|
||||
public void setAge(Integer value) {
|
||||
set(3, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for <code>public.author.age</code>.
|
||||
*/
|
||||
public Integer getAge() {
|
||||
return (Integer) get(3);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Primary key information
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public Record1<Integer> key() {
|
||||
return (Record1) super.key();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Record4 type implementation
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public Row4<Integer, String, String, Integer> fieldsRow() {
|
||||
return (Row4) super.fieldsRow();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Row4<Integer, String, String, Integer> valuesRow() {
|
||||
return (Row4) super.valuesRow();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Field<Integer> field1() {
|
||||
return Author.AUTHOR.ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Field<String> field2() {
|
||||
return Author.AUTHOR.FIRST_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Field<String> field3() {
|
||||
return Author.AUTHOR.LAST_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Field<Integer> field4() {
|
||||
return Author.AUTHOR.AGE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer component1() {
|
||||
return getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String component2() {
|
||||
return getFirstName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String component3() {
|
||||
return getLastName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer component4() {
|
||||
return getAge();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer value1() {
|
||||
return getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String value2() {
|
||||
return getFirstName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String value3() {
|
||||
return getLastName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer value4() {
|
||||
return getAge();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthorRecord value1(Integer value) {
|
||||
setId(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthorRecord value2(String value) {
|
||||
setFirstName(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthorRecord value3(String value) {
|
||||
setLastName(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthorRecord value4(Integer value) {
|
||||
setAge(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthorRecord values(Integer value1, String value2, String value3, Integer value4) {
|
||||
value1(value1);
|
||||
value2(value2);
|
||||
value3(value3);
|
||||
value4(value4);
|
||||
return this;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Constructors
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Create a detached AuthorRecord
|
||||
*/
|
||||
public AuthorRecord() {
|
||||
super(Author.AUTHOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a detached, initialised AuthorRecord
|
||||
*/
|
||||
public AuthorRecord(Integer id, String firstName, String lastName, Integer age) {
|
||||
super(Author.AUTHOR);
|
||||
|
||||
set(0, id);
|
||||
set(1, firstName);
|
||||
set(2, lastName);
|
||||
set(3, age);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
package com.baeldung.jooq;
|
||||
|
||||
import com.baeldung.jooq.model.tables.Article;
|
||||
import com.baeldung.jooq.model.tables.Author;
|
||||
import com.baeldung.jooq.model.tables.records.ArticleRecord;
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.Field;
|
||||
import org.jooq.Record;
|
||||
import org.jooq.Result;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.impl.DSL;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static com.baeldung.jooq.Crud.delete;
|
||||
import static com.baeldung.jooq.Crud.getAll;
|
||||
import static com.baeldung.jooq.Crud.getFields;
|
||||
import static com.baeldung.jooq.Crud.getOne;
|
||||
import static com.baeldung.jooq.Crud.save;
|
||||
import static com.baeldung.jooq.Crud.update;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class CrudLiveTest {
|
||||
|
||||
static DSLContext context;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() throws SQLException {
|
||||
Connection conn = DriverManager.getConnection("jdbc:h2:mem:tes;INIT=CREATE SCHEMA IF NOT EXISTS \"public\"");
|
||||
context = DSL.using(conn, SQLDialect.H2);
|
||||
|
||||
context.createTable(Author.AUTHOR)
|
||||
.columns(
|
||||
Author.AUTHOR.ID,
|
||||
Author.AUTHOR.FIRST_NAME,
|
||||
Author.AUTHOR.LAST_NAME,
|
||||
Author.AUTHOR.AGE
|
||||
)
|
||||
.execute();
|
||||
context.createTable(Article.ARTICLE)
|
||||
.columns(
|
||||
Article.ARTICLE.ID,
|
||||
Article.ARTICLE.TITLE,
|
||||
Article.ARTICLE.DESCRIPTION,
|
||||
Article.ARTICLE.AUTHOR_ID
|
||||
)
|
||||
.execute();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void cleanup() {
|
||||
context.truncateTable(Article.ARTICLE)
|
||||
.execute();
|
||||
context.truncateTable(Author.AUTHOR)
|
||||
.execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArticleRecord_whenSave_thenNewRecordInDb() {
|
||||
// given
|
||||
ArticleRecord article = article();
|
||||
|
||||
// when
|
||||
save(article);
|
||||
|
||||
// then
|
||||
ArticleRecord savedArticle = getOne(
|
||||
context,
|
||||
Article.ARTICLE,
|
||||
Article.ARTICLE.ID.eq(1)
|
||||
);
|
||||
assertEquals(savedArticle.getId().intValue(), 1);
|
||||
assertEquals(savedArticle.getTitle(), "jOOQ examples");
|
||||
assertEquals(savedArticle.getDescription(), "A few examples of jOOQ CRUD operations");
|
||||
assertEquals(savedArticle.getAuthorId().intValue(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArticleRecord_whenGetAll_thenValidOneRecord() {
|
||||
// given
|
||||
ArticleRecord article = article();
|
||||
save(article);
|
||||
|
||||
// when
|
||||
Result<Record> articles = getAll(
|
||||
context,
|
||||
Article.ARTICLE
|
||||
);
|
||||
|
||||
// then
|
||||
assertEquals(articles.size(), 1);
|
||||
|
||||
Record record = articles.get(0);
|
||||
ArticleRecord savedArticle = record.into(Article.ARTICLE);
|
||||
|
||||
assertEquals(savedArticle.getId().intValue(), 1);
|
||||
assertEquals(savedArticle.getTitle(), "jOOQ examples");
|
||||
assertEquals(savedArticle.getDescription(), "A few examples of jOOQ CRUD operations");
|
||||
assertEquals(savedArticle.getAuthorId().intValue(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArticleRecord_whenGetOnlyTitles_thenValidOneValue() {
|
||||
// given
|
||||
ArticleRecord article = article();
|
||||
save(article);
|
||||
|
||||
// when
|
||||
Result<Record> articles = getFields(
|
||||
context,
|
||||
Article.ARTICLE,
|
||||
Article.ARTICLE.TITLE
|
||||
);
|
||||
|
||||
// then
|
||||
assertEquals(articles.size(), 1);
|
||||
|
||||
Record record = articles.get(0);
|
||||
ArticleRecord savedArticle = record.into(Article.ARTICLE);
|
||||
|
||||
assertNull(savedArticle.getId());
|
||||
assertEquals(savedArticle.getTitle(), "jOOQ examples");
|
||||
assertNull(savedArticle.getDescription());
|
||||
assertNull(savedArticle.getAuthorId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArticleRecord_whenGetOne_thenValidRecord() {
|
||||
// given
|
||||
ArticleRecord article = article();
|
||||
save(article);
|
||||
|
||||
// when
|
||||
ArticleRecord savedArticle = getOne(
|
||||
context,
|
||||
Article.ARTICLE,
|
||||
Article.ARTICLE.ID.eq(1)
|
||||
);
|
||||
|
||||
// then
|
||||
assertEquals(savedArticle.getId().intValue(), 1);
|
||||
assertEquals(savedArticle.getTitle(), "jOOQ examples");
|
||||
assertEquals(savedArticle.getDescription(), "A few examples of jOOQ CRUD operations");
|
||||
assertEquals(savedArticle.getAuthorId().intValue(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArticleRecord_whenUpdateById_thenChangedValuesDbTable() {
|
||||
// given
|
||||
ArticleRecord article = article();
|
||||
save(article);
|
||||
|
||||
HashMap<Field<String>, String> updateFields = new HashMap<>();
|
||||
updateFields.put(Article.ARTICLE.TITLE, "new title");
|
||||
updateFields.put(Article.ARTICLE.DESCRIPTION, "new description");
|
||||
|
||||
// when
|
||||
update(
|
||||
context,
|
||||
Article.ARTICLE,
|
||||
updateFields,
|
||||
Article.ARTICLE.ID.eq(1)
|
||||
);
|
||||
|
||||
// then
|
||||
ArticleRecord updatedArticle = getOne(
|
||||
context,
|
||||
Article.ARTICLE,
|
||||
Article.ARTICLE.ID.eq(1)
|
||||
);
|
||||
assertEquals(updatedArticle.getId().intValue(), 1);
|
||||
assertEquals(updatedArticle.getTitle(), "new title");
|
||||
assertEquals(updatedArticle.getDescription(), "new description");
|
||||
assertEquals(updatedArticle.getAuthorId().intValue(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArticleRecord_whenUpdate_thenChangedValuesDbTable() {
|
||||
// given
|
||||
ArticleRecord article = article();
|
||||
save(article);
|
||||
|
||||
article.setTitle("new title");
|
||||
article.setDescription("new description");
|
||||
|
||||
// when
|
||||
update(article);
|
||||
|
||||
// then
|
||||
ArticleRecord updatedArticle = getOne(
|
||||
context,
|
||||
Article.ARTICLE,
|
||||
Article.ARTICLE.ID.eq(1)
|
||||
);
|
||||
assertEquals(updatedArticle.getId().intValue(), 1);
|
||||
assertEquals(updatedArticle.getTitle(), "new title");
|
||||
assertEquals(updatedArticle.getDescription(), "new description");
|
||||
assertEquals(updatedArticle.getAuthorId().intValue(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArticleRecord_whenDelete_thenEmptyDbTable() {
|
||||
// given
|
||||
ArticleRecord article = article();
|
||||
save(article);
|
||||
|
||||
// when
|
||||
delete(article);
|
||||
|
||||
// then
|
||||
Result<Record> articles = getAll(
|
||||
context,
|
||||
Article.ARTICLE
|
||||
);
|
||||
assertTrue(articles.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArticleRecord_whenDeleteById_thenEmptyDbTable() {
|
||||
// given
|
||||
ArticleRecord article = article();
|
||||
save(article);
|
||||
|
||||
// when
|
||||
delete(
|
||||
context,
|
||||
Article.ARTICLE,
|
||||
Article.ARTICLE.ID.eq(1)
|
||||
);
|
||||
|
||||
// then
|
||||
Result<Record> articles = getAll(
|
||||
context,
|
||||
Article.ARTICLE
|
||||
);
|
||||
assertTrue(articles.isEmpty());
|
||||
}
|
||||
|
||||
private ArticleRecord article() {
|
||||
ArticleRecord article = context.newRecord(Article.ARTICLE);
|
||||
article.setId(1);
|
||||
article.setTitle("jOOQ examples");
|
||||
article.setDescription("A few examples of jOOQ CRUD operations");
|
||||
article.setAuthorId(1);
|
||||
|
||||
return article;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
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>jpa-hibernate-cascade-type</artifactId>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>persistence-modules</artifactId>
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
<module>java-jpa-2</module> <!-- long running -->
|
||||
<module>java-mongodb</module> <!-- long running -->
|
||||
<module>jnosql</module> <!-- long running -->
|
||||
<module>jooq</module>
|
||||
<module>jpa-hibernate-cascade-type</module>
|
||||
<module>liquibase</module>
|
||||
<module>orientdb</module>
|
||||
@@ -68,7 +69,9 @@
|
||||
<module>spring-data-jpa-enterprise</module>
|
||||
<module>spring-data-jpa-filtering</module>
|
||||
<module>spring-data-jpa-query</module>
|
||||
<module>spring-data-jpa-query-2</module>
|
||||
<module>spring-data-jpa-repo</module>
|
||||
<module>spring-data-jpa-repo-2</module>
|
||||
|
||||
<module>spring-data-jdbc</module>
|
||||
|
||||
@@ -78,17 +81,21 @@
|
||||
<module>spring-data-redis</module>
|
||||
<module>spring-data-solr</module>
|
||||
<module>spring-hibernate-3</module>
|
||||
<module>spring-hibernate-5</module> <!-- long running -->
|
||||
<module>spring-hibernate4</module>
|
||||
<module>spring-hibernate-5</module> <!-- long running -->
|
||||
<module>spring-jpa</module>
|
||||
<module>spring-jpa-2</module>
|
||||
<module>spring-jdbc</module>
|
||||
<!-- <module>spring-mybatis</module> --> <!-- needs fixing in BAEL-9021 -->
|
||||
<module>spring-persistence-simple</module>
|
||||
<module>spring-persistence-simple-2</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<!-- persistence -->
|
||||
<hibernate.version>5.2.17.Final</hibernate.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
@@ -21,6 +21,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
@@ -64,6 +68,11 @@
|
||||
<properties>
|
||||
<r2dbc-h2.version>0.8.1.RELEASE</r2dbc-h2.version>
|
||||
<h2.version>1.4.200</h2.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -61,6 +61,11 @@
|
||||
<redisson.version>3.13.1</redisson.version>
|
||||
<jedis.version>3.3.0</jedis.version>
|
||||
<epoll.version>4.1.50.Final</epoll.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -39,6 +39,11 @@
|
||||
|
||||
<properties>
|
||||
<mysql-connector-java.version>8.0.12</mysql-connector-java.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -17,6 +17,13 @@
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit</groupId>
|
||||
<artifactId>junit-bom</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
@@ -36,7 +43,6 @@
|
||||
<artifactId>jdbi3-sqlobject</artifactId>
|
||||
<version>${jdbi.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
@@ -46,6 +46,11 @@
|
||||
<!-- The main class to start by executing java -jar -->
|
||||
<start-class>com.baeldung.h2db.demo.server.SpringBootApp</start-class>
|
||||
<db-util.version>1.0.4</db-util.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
#spring.datasource.url=jdbc:h2:file:C:/data/demodb
|
||||
#spring.datasource.url=jdbc:h2:file:~/demodb
|
||||
spring.datasource.url=jdbc:h2:file:./src/main/resources/db/demodb
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.h2.console.enabled=true
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
spring.h2.console.path=/h2-console
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.persistent;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.h2db.auto.configuration.AutoConfigurationDemo;
|
||||
|
||||
@ActiveProfiles("persistent-on")
|
||||
@RunWith(SpringRunner.class)
|
||||
@DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD)
|
||||
@SpringBootTest(classes = AutoConfigurationDemo.class)
|
||||
public class FilesLocationUnitTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenApplicationStarted_thenEmbeddedDbSubfolderCreated() {
|
||||
File subdirectory = new File("src/main/resources/db");
|
||||
System.out.println(subdirectory.getAbsolutePath());
|
||||
assertTrue(subdirectory.exists());
|
||||
assertTrue(subdirectory.isDirectory());
|
||||
}
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenApplicationStarted_thenEmbeddedDbFilesCreated() {
|
||||
File dbFile = new File("src/main/resources/db/demodb.mv.db");
|
||||
System.out.println(dbFile.getAbsolutePath());
|
||||
|
||||
assertTrue(dbFile.exists());
|
||||
assertTrue(dbFile.isFile());
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
File dbFile = new File("src/main/resources/db/demodb.mv.db");
|
||||
dbFile.deleteOnExit();
|
||||
}
|
||||
}
|
||||
@@ -36,4 +36,10 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
</project>
|
||||
+13
-12
@@ -1,18 +1,8 @@
|
||||
package com.baeldung.mongodb;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
import com.mongodb.BasicDBObjectBuilder;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.MongoClient;
|
||||
|
||||
import com.mongodb.client.MongoClients;
|
||||
import de.flapdoodle.embed.mongo.MongodExecutable;
|
||||
import de.flapdoodle.embed.mongo.MongodStarter;
|
||||
import de.flapdoodle.embed.mongo.config.IMongodConfig;
|
||||
@@ -20,8 +10,19 @@ import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
|
||||
import de.flapdoodle.embed.mongo.config.Net;
|
||||
import de.flapdoodle.embed.mongo.distribution.Version;
|
||||
import de.flapdoodle.embed.process.runtime.Network;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ManualEmbeddedMongoDbIntegrationTest {
|
||||
|
||||
private static final String CONNECTION_STRING = "mongodb://%s:%d";
|
||||
|
||||
private MongodExecutable mongodExecutable;
|
||||
private MongoTemplate mongoTemplate;
|
||||
|
||||
@@ -42,7 +43,7 @@ class ManualEmbeddedMongoDbIntegrationTest {
|
||||
MongodStarter starter = MongodStarter.getDefaultInstance();
|
||||
mongodExecutable = starter.prepare(mongodConfig);
|
||||
mongodExecutable.start();
|
||||
mongoTemplate = new MongoTemplate(new MongoClient(ip, randomPort), "test");
|
||||
mongoTemplate = new MongoTemplate(MongoClients.create(String.format(CONNECTION_STRING, ip, randomPort)),"test");
|
||||
}
|
||||
|
||||
@DisplayName("Given object When save object using MongoDB template Then object can be found")
|
||||
|
||||
@@ -76,6 +76,11 @@
|
||||
<properties>
|
||||
<mockito.version>2.23.0</mockito.version>
|
||||
<validation-api.version>2.0.1.Final</validation-api.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -51,7 +51,13 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
|
||||
<cassandra-unit-spring.version>3.11.2.0</cassandra-unit-spring.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -8,9 +8,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-4</artifactId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-spring-4</relativePath>
|
||||
<relativePath>../../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -30,6 +30,31 @@
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aop</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-expression</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
@@ -79,6 +104,11 @@
|
||||
<cassandra-unit-spring.version>2.1.9.2</cassandra-unit-spring.version>
|
||||
<cassandra-unit-shaded.version>2.1.9.2</cassandra-unit-shaded.version>
|
||||
<hector-core.version>2.0-0</hector-core.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -16,6 +16,11 @@
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<cosmodb.version>2.3.0</cosmodb.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -182,6 +182,11 @@
|
||||
<dynamodblocal.version>1.11.86</dynamodblocal.version>
|
||||
<dynamodblocal.repository.url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</dynamodblocal.repository.url>
|
||||
<maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -69,5 +69,10 @@
|
||||
<fastjson.version>1.2.47</fastjson.version>
|
||||
<spatial4j.version>0.7</spatial4j.version>
|
||||
<jts.version>1.15.0</jts.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -26,4 +26,11 @@
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
@@ -72,6 +72,11 @@
|
||||
<testcontainers.postgresql.version>1.10.6</testcontainers.postgresql.version>
|
||||
<postgresql.version>42.2.5</postgresql.version>
|
||||
<guava.version>21.0</guava.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -66,6 +66,11 @@
|
||||
<datasource-proxy.version>1.4.1</datasource-proxy.version>
|
||||
<guava.version>21.0</guava.version>
|
||||
<testcontainers.version>1.12.2</testcontainers.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -95,13 +95,14 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<spring-boot-version>2.1.9.RELEASE</spring-boot-version>
|
||||
<start-class>com.baeldung.springdatageode.app.ClientCacheApp</start-class>
|
||||
<spring-geode-starter-version>1.1.1.RELEASE</spring-geode-starter-version>
|
||||
<spring.boot.starter.version>2.1.9.RELEASE</spring.boot.starter.version>
|
||||
<mapstruct.version>1.3.1.Final</mapstruct.version>
|
||||
<guava.version>21.0</guava.version>
|
||||
<testcontainers.version>1.12.2</testcontainers.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
+2
-2
@@ -2,8 +2,8 @@ package com.baeldung.multipledb.dao.user;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.multipledb.model.user.PossessionMultipleDB;
|
||||
import com.baeldung.multipledb.model.user.Possession;
|
||||
|
||||
public interface PossessionRepository extends JpaRepository<PossessionMultipleDB, Long> {
|
||||
public interface PossessionRepository extends JpaRepository<Possession, Long> {
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -2,7 +2,7 @@ package com.baeldung.multipledb.dao.user;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.multipledb.model.user.UserMultipleDB;
|
||||
import com.baeldung.multipledb.model.user.User;
|
||||
|
||||
public interface UserRepository extends JpaRepository<UserMultipleDB, Integer> {
|
||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
}
|
||||
+2
-6
@@ -1,10 +1,6 @@
|
||||
package com.baeldung.spring.data.persistence.model;
|
||||
package com.baeldung.multipledb.model.user;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
-82
@@ -1,82 +0,0 @@
|
||||
package com.baeldung.multipledb.model.user;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
public class PossessionMultipleDB {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public PossessionMultipleDB() {
|
||||
super();
|
||||
}
|
||||
|
||||
public PossessionMultipleDB(final String name) {
|
||||
super();
|
||||
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = (prime * result) + (int) (id ^ (id >>> 32));
|
||||
result = (prime * result) + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final PossessionMultipleDB other = (PossessionMultipleDB) obj;
|
||||
if (id != other.id) {
|
||||
return false;
|
||||
}
|
||||
if (name == null) {
|
||||
if (other.name != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!name.equals(other.name)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("Possesion [id=").append(id).append(", name=").append(name).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
+6
-6
@@ -6,7 +6,7 @@ import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class UserMultipleDB {
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@@ -18,13 +18,13 @@ public class UserMultipleDB {
|
||||
private Integer status;
|
||||
|
||||
@OneToMany
|
||||
List<PossessionMultipleDB> possessionList;
|
||||
List<Possession> possessionList;
|
||||
|
||||
public UserMultipleDB() {
|
||||
public User() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserMultipleDB(String name, String email, Integer status) {
|
||||
public User(String name, String email, Integer status) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.status = status;
|
||||
@@ -70,11 +70,11 @@ public class UserMultipleDB {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public List<PossessionMultipleDB> getPossessionList() {
|
||||
public List<Possession> getPossessionList() {
|
||||
return possessionList;
|
||||
}
|
||||
|
||||
public void setPossessionList(List<PossessionMultipleDB> possessionList) {
|
||||
public void setPossessionList(List<Possession> possessionList) {
|
||||
this.possessionList = possessionList;
|
||||
}
|
||||
|
||||
+7
-7
@@ -20,8 +20,8 @@ import com.baeldung.multipledb.dao.product.ProductRepository;
|
||||
import com.baeldung.multipledb.dao.user.PossessionRepository;
|
||||
import com.baeldung.multipledb.dao.user.UserRepository;
|
||||
import com.baeldung.multipledb.model.product.Product;
|
||||
import com.baeldung.multipledb.model.user.PossessionMultipleDB;
|
||||
import com.baeldung.multipledb.model.user.UserMultipleDB;
|
||||
import com.baeldung.multipledb.model.user.Possession;
|
||||
import com.baeldung.multipledb.model.user.User;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes=MultipleDbApplication.class)
|
||||
@@ -42,15 +42,15 @@ public class JpaMultipleDBIntegrationTest {
|
||||
@Test
|
||||
@Transactional("userTransactionManager")
|
||||
public void whenCreatingUser_thenCreated() {
|
||||
UserMultipleDB user = new UserMultipleDB();
|
||||
User user = new User();
|
||||
user.setName("John");
|
||||
user.setEmail("john@test.com");
|
||||
user.setAge(20);
|
||||
PossessionMultipleDB p = new PossessionMultipleDB("sample");
|
||||
Possession p = new Possession("sample");
|
||||
p = possessionRepository.save(p);
|
||||
user.setPossessionList(Collections.singletonList(p));
|
||||
user = userRepository.save(user);
|
||||
final Optional<UserMultipleDB> result = userRepository.findById(user.getId());
|
||||
final Optional<User> result = userRepository.findById(user.getId());
|
||||
assertTrue(result.isPresent());
|
||||
System.out.println(result.get().getPossessionList());
|
||||
assertEquals(1, result.get().getPossessionList().size());
|
||||
@@ -59,14 +59,14 @@ public class JpaMultipleDBIntegrationTest {
|
||||
@Test
|
||||
@Transactional("userTransactionManager")
|
||||
public void whenCreatingUsersWithSameEmail_thenRollback() {
|
||||
UserMultipleDB user1 = new UserMultipleDB();
|
||||
User user1 = new User();
|
||||
user1.setName("John");
|
||||
user1.setEmail("john@test.com");
|
||||
user1.setAge(20);
|
||||
user1 = userRepository.save(user1);
|
||||
assertTrue(userRepository.findById(user1.getId()).isPresent());
|
||||
|
||||
UserMultipleDB user2 = new UserMultipleDB();
|
||||
User user2 = new User();
|
||||
user2.setName("Tom");
|
||||
user2.setEmail("john@test.com");
|
||||
user2.setAge(10);
|
||||
|
||||
@@ -72,6 +72,11 @@
|
||||
<testcontainers.postgresql.version>1.10.6</testcontainers.postgresql.version>
|
||||
<postgresql.version>42.2.5</postgresql.version>
|
||||
<guava.version>21.0</guava.version>
|
||||
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -3,7 +3,15 @@
|
||||
This module contains articles about querying data using Spring Data JPA
|
||||
|
||||
### Relevant Articles:
|
||||
- [Spring Data JPA @Query Annotation](https://www.baeldung.com/spring-data-jpa-query)
|
||||
- [Use Criteria Queries in a Spring Data Application](https://www.baeldung.com/spring-data-criteria-queries)
|
||||
- [Query Entities by Dates and Times with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-query-by-date)
|
||||
- [Hibernate Pagination](https://www.baeldung.com/hibernate-pagination)
|
||||
- [Sorting with Hibernate](https://www.baeldung.com/hibernate-sort)
|
||||
- [Stored Procedures with Hibernate](https://www.baeldung.com/stored-procedures-with-hibernate-tutorial)
|
||||
- [Eager/Lazy Loading In Hibernate](https://www.baeldung.com/hibernate-lazy-eager-loading)
|
||||
- [Auditing with JPA, Hibernate, and Spring Data JPA](https://www.baeldung.com/database-auditing-jpa)
|
||||
|
||||
- More articles: [[<-- prev]](../spring-data-jpa-query)
|
||||
|
||||
### Eclipse Config
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-data-jpa-query</artifactId>
|
||||
<artifactId>spring-data-jpa-query-2</artifactId>
|
||||
<name>spring-data-jpa-query-2</name>
|
||||
|
||||
<parent>
|
||||
@@ -18,6 +18,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
@@ -28,6 +32,66 @@
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-envers</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.transaction</groupId>
|
||||
<artifactId>jta</artifactId>
|
||||
<version>${jta.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat</groupId>
|
||||
<artifactId>tomcat-dbcp</artifactId>
|
||||
<version>${tomcat-dbcp.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-envers</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
|
||||
<jta.version>1.1</jta.version>
|
||||
<guava.version>21.0</guava.version>
|
||||
<!-- persistence -->
|
||||
<hibernate.version>5.2.10.Final</hibernate.version>
|
||||
</properties>
|
||||
</project>
|
||||
+5
-5
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.spring;
|
||||
package com.baeldung.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -15,8 +15,8 @@ import org.springframework.dao.annotation.PersistenceExceptionTranslationPostPro
|
||||
import org.springframework.data.domain.AuditorAware;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.orm.hibernate4.HibernateTransactionManager;
|
||||
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
||||
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
||||
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.JpaVendorAdapter;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
@@ -47,7 +47,7 @@ import com.google.common.base.Preconditions;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@EnableJpaRepositories(basePackages = { "com.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager")
|
||||
@EnableJpaRepositories(basePackages = { "com.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager", entityManagerFactoryRef = "jpaEntityManager")
|
||||
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
|
||||
@PropertySource({ "classpath:persistence-h2.properties" })
|
||||
@ComponentScan({ "com.baeldung.persistence" })
|
||||
@@ -75,7 +75,7 @@ public class PersistenceConfig {
|
||||
return sessionFactory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Bean("jpaEntityManager")
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
|
||||
emf.setDataSource(restDataSource());
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.spring;
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
+2
-3
@@ -9,11 +9,10 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
||||
public class AuditorAwareImpl implements AuditorAware<String> {
|
||||
|
||||
@Override
|
||||
public String getCurrentAuditor() {
|
||||
public Optional<String> getCurrentAuditor() {
|
||||
return Optional.ofNullable(SecurityContextHolder.getContext())
|
||||
.map(e -> e.getAuthentication())
|
||||
.map(Authentication::getName)
|
||||
.orElse(null);
|
||||
.map(Authentication::getName);
|
||||
}
|
||||
|
||||
}
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import com.baeldung.persistence.model.Book;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
public interface BookRepository extends JpaRepository<Book, Long>, BookRepositoryCustom, JpaSpecificationExecutor<Book> {
|
||||
|
||||
}
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import com.baeldung.persistence.model.Book;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface BookRepositoryCustom {
|
||||
|
||||
List<Book> findBooksByAuthorNameAndTitle(String authorName, String title);
|
||||
|
||||
}
|
||||
-44
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import com.baeldung.persistence.model.Book;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public class BookRepositoryImpl implements BookRepositoryCustom {
|
||||
|
||||
private EntityManager em;
|
||||
|
||||
public BookRepositoryImpl(EntityManager em) {
|
||||
this.em = em;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> findBooksByAuthorNameAndTitle(String authorName, String title) {
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<Book> cq = cb.createQuery(Book.class);
|
||||
|
||||
Root<Book> book = cq.from(Book.class);
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
|
||||
if (authorName != null) {
|
||||
predicates.add(cb.equal(book.get("author"), authorName));
|
||||
}
|
||||
if (title != null) {
|
||||
predicates.add(cb.like(book.get("title"), "%" + title + "%"));
|
||||
}
|
||||
cq.where(predicates.toArray(new Predicate[0]));
|
||||
|
||||
TypedQuery<Book> query = em.createQuery(cq);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
}
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import com.baeldung.persistence.model.Book;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.baeldung.persistence.dao.BookSpecifications.hasAuthor;
|
||||
import static com.baeldung.persistence.dao.BookSpecifications.titleContains;
|
||||
import static org.springframework.data.jpa.domain.Specification.where;
|
||||
|
||||
@Service
|
||||
public class BookService {
|
||||
|
||||
private BookRepository bookRepository;
|
||||
|
||||
public BookService(BookRepository bookRepository) {
|
||||
this.bookRepository = bookRepository;
|
||||
}
|
||||
|
||||
public List<Book> query(String author, String title) {
|
||||
return bookRepository.findAll(where(hasAuthor(author)).and(titleContains(title)));
|
||||
}
|
||||
|
||||
}
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import com.baeldung.persistence.model.Book;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
public class BookSpecifications {
|
||||
|
||||
public static Specification<Book> hasAuthor(String author) {
|
||||
return (book, cq, cb) -> cb.equal(book.get("author"), author);
|
||||
}
|
||||
|
||||
public static Specification<Book> titleContains(String title) {
|
||||
return (book, cq, cb) -> cb.like(book.get("title"), "%" + title + "%");
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -12,7 +12,7 @@ import javax.persistence.criteria.Root;
|
||||
|
||||
public class AbstractJpaDao<T extends Serializable> extends AbstractDao<T> implements IOperations<T> {
|
||||
|
||||
@PersistenceContext
|
||||
@PersistenceContext(unitName = "jpaEntityManager")
|
||||
private EntityManager em;
|
||||
|
||||
// API
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user