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);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user