[#8108] Add a CriteriaQuery vs jOOQ Exampl
This commit is contained in:
parent
c74ec4414b
commit
aefd551690
@ -18,7 +18,7 @@
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<org.hibernate.version>5.3.1.Final</org.hibernate.version>
|
||||
<org.hibernate.version>5.3.7.Final</org.hibernate.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@ -38,10 +38,6 @@
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<configuration>
|
||||
<fork>true</fork>
|
||||
<maxmem>1024m</maxmem>
|
||||
<meminitial>256m</meminitial>
|
||||
<encoding>UTF-8</encoding>
|
||||
<!-- [java-9] -->
|
||||
<release>10</release>
|
||||
<!-- [/java-9] -->
|
||||
@ -50,14 +46,34 @@
|
||||
<source>10</source>
|
||||
<target>10</target>
|
||||
|
||||
<debug>true</debug>
|
||||
<debuglevel>lines,vars,source</debuglevel>
|
||||
<!-- [#2413] Make compiler warnings a bit more visible
|
||||
But don't fail (yet) -->
|
||||
<compilerArgs>
|
||||
<arg>-Xlint:varargs</arg>
|
||||
</compilerArgs>
|
||||
<!-- How to properly set things up in JDK 9+
|
||||
https://stackoverflow.com/a/51409471/521799 -->
|
||||
<compilerArguments>
|
||||
<AaddGeneratedAnnotation>false</AaddGeneratedAnnotation>
|
||||
<Adebug>true</Adebug>
|
||||
</compilerArguments>
|
||||
|
||||
<annotationProcessorPaths>
|
||||
<annotationProcessorPath>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-jpamodelgen</artifactId>
|
||||
<version>${org.hibernate.version}</version>
|
||||
</annotationProcessorPath>
|
||||
<annotationProcessorPath>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</annotationProcessorPath>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@ -101,12 +101,12 @@
|
||||
<meminitial>256m</meminitial>
|
||||
<encoding>UTF-8</encoding>
|
||||
<!-- [java-9] -->
|
||||
<release>10</release>
|
||||
<release>11</release>
|
||||
<!-- [/java-9] -->
|
||||
|
||||
<!-- IntelliJ needs these https://youtrack.jetbrains.com/issue/IDEA-195472 -->
|
||||
<source>10</source>
|
||||
<target>10</target>
|
||||
<source>11</source>
|
||||
<target>11</target>
|
||||
|
||||
<debug>true</debug>
|
||||
<debuglevel>lines,vars,source</debuglevel>
|
||||
|
||||
@ -0,0 +1,164 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Other licenses:
|
||||
* -----------------------------------------------------------------------------
|
||||
* Commercial licenses for this work are available. These replace the above
|
||||
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
|
||||
* database integrations.
|
||||
*
|
||||
* For more information, please visit: http://www.jooq.org/licenses
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
package org.jooq.example.jpa;
|
||||
|
||||
import static org.jooq.example.jpa.jooq.Tables.FILM;
|
||||
import static org.jooq.example.jpa.jooq.Tables.FILM_ACTOR;
|
||||
import static org.jooq.example.jpa.jooq.Tables.LANGUAGE;
|
||||
import static org.jooq.impl.DSL.count;
|
||||
import static org.jooq.impl.DSL.countDistinct;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Tuple;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Expression;
|
||||
import javax.persistence.criteria.Join;
|
||||
import javax.persistence.criteria.Path;
|
||||
import javax.persistence.criteria.Root;
|
||||
import javax.persistence.criteria.SetJoin;
|
||||
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.example.jpa.entity.Actor;
|
||||
import org.jooq.example.jpa.entity.Actor_;
|
||||
import org.jooq.example.jpa.entity.Film;
|
||||
import org.jooq.example.jpa.entity.Film_;
|
||||
import org.jooq.example.jpa.entity.Language;
|
||||
import org.jooq.example.jpa.entity.Language_;
|
||||
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
class CriteriaQueryOrJOOQ {
|
||||
|
||||
private static void run(EntityManager em, DSLContext ctx) {
|
||||
filmLengthAndLanguages(em, ctx);
|
||||
numberOfFilmsPerLanguage(em, ctx);
|
||||
numberOfActorsPerLanguage(em, ctx);
|
||||
}
|
||||
|
||||
private static void filmLengthAndLanguages(EntityManager em, DSLContext ctx) {
|
||||
// Using criteria query
|
||||
// --------------------
|
||||
|
||||
CriteriaBuilder qb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<Film> q = qb.createQuery(Film.class);
|
||||
Root<Film> root = q.from(Film.class);
|
||||
q = q.select(root);
|
||||
TypedQuery<Film> typed = em.createQuery(q);
|
||||
|
||||
for (Film film : typed.getResultList())
|
||||
System.out.println(film.title.value + " (" + film.length + " minutes) in " + film.language.name);
|
||||
|
||||
// Using jOOQ
|
||||
// ----------
|
||||
|
||||
for (var rec :
|
||||
ctx.select(FILM.TITLE, FILM.LENGTH, LANGUAGE.NAME)
|
||||
.from(FILM)
|
||||
.join(LANGUAGE).on(FILM.LANGUAGE_LANGUAGEID.eq(LANGUAGE.LANGUAGEID)))
|
||||
System.out.println(rec.get(FILM.TITLE) + " (" + rec.get(FILM.LENGTH) + " minutes) in " + rec.get(LANGUAGE.NAME));
|
||||
}
|
||||
|
||||
private static void numberOfFilmsPerLanguage(EntityManager em, DSLContext ctx) {
|
||||
|
||||
// Using criteria query
|
||||
// --------------------
|
||||
|
||||
CriteriaBuilder qb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<Tuple> q = qb.createTupleQuery();
|
||||
Root<Film> filmRoot = q.from(Film.class);
|
||||
Join<Film, Language> filmJoin = filmRoot.join(Film_.language);
|
||||
Path<String> languagePath = filmJoin.get(Language_.name);
|
||||
Path<Integer> filmPath = filmRoot.get(Film_.filmId);
|
||||
Expression<Long> count = qb.count(filmPath);
|
||||
q = q.multiselect(languagePath, count);
|
||||
q = q.groupBy(languagePath);
|
||||
TypedQuery<Tuple> typed = em.createQuery(q);
|
||||
|
||||
for (Tuple tuple : typed.getResultList())
|
||||
System.out.println(tuple.get(languagePath) + " (" + tuple.get(count) + " films)");
|
||||
|
||||
// Using jOOQ
|
||||
// ----------
|
||||
|
||||
for (var rec :
|
||||
ctx.select(LANGUAGE.NAME, count())
|
||||
.from(LANGUAGE)
|
||||
.join(FILM).on(FILM.LANGUAGE_LANGUAGEID.eq(LANGUAGE.LANGUAGEID))
|
||||
.groupBy(LANGUAGE.NAME))
|
||||
System.out.println(rec.get(LANGUAGE.NAME) + " (" + rec.get(count()) + " films)");
|
||||
}
|
||||
|
||||
private static void numberOfActorsPerLanguage(EntityManager em, DSLContext ctx) {
|
||||
|
||||
// Using criteria query
|
||||
// --------------------
|
||||
|
||||
CriteriaBuilder qb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<Tuple> q = qb.createTupleQuery();
|
||||
Root<Film> filmRoot = q.from(Film.class);
|
||||
Join<Film, Language> filmJoin = filmRoot.join(Film_.language);
|
||||
Path<String> languagePath = filmJoin.get(Language_.name);
|
||||
SetJoin<Film, Actor> actorJoin = filmRoot.join(Film_.actors);
|
||||
Path<Integer> actorPath = actorJoin.get(Actor_.actorId);
|
||||
Expression<Long> count = qb.countDistinct(actorPath);
|
||||
q = q.multiselect(languagePath, count);
|
||||
q = q.groupBy(languagePath);
|
||||
TypedQuery<Tuple> typed = em.createQuery(q);
|
||||
|
||||
for (Tuple tuple : typed.getResultList())
|
||||
System.out.println(tuple.get(languagePath) + " (" + tuple.get(count) + " actors)");
|
||||
|
||||
// Using jOOQ
|
||||
// ----------
|
||||
|
||||
for (var rec :
|
||||
ctx.select(LANGUAGE.NAME, countDistinct(FILM_ACTOR.ACTORS_ACTORID).as("c"))
|
||||
.from(LANGUAGE)
|
||||
.join(FILM).on(FILM.LANGUAGE_LANGUAGEID.eq(LANGUAGE.LANGUAGEID))
|
||||
.join(FILM_ACTOR).on(FILM.FILMID.eq(FILM_ACTOR.FILMS_FILMID))
|
||||
.groupBy(LANGUAGE.NAME))
|
||||
System.out.println(rec.get(LANGUAGE.NAME) + " (" + rec.get("c") + " actors)");
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Setup.run(CriteriaQueryOrJOOQ::run);
|
||||
}
|
||||
}
|
||||
@ -45,37 +45,9 @@ import static org.jooq.impl.DSL.count;
|
||||
import static org.jooq.impl.DSL.max;
|
||||
import static org.jooq.impl.DSL.min;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Year;
|
||||
import java.util.Arrays;
|
||||
import java.util.EnumSet;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.ExecuteContext;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.example.jpa.embeddables.Title;
|
||||
import org.jooq.example.jpa.entity.Actor;
|
||||
import org.jooq.example.jpa.entity.Film;
|
||||
import org.jooq.example.jpa.entity.Language;
|
||||
import org.jooq.impl.DSL;
|
||||
import org.jooq.impl.DefaultConfiguration;
|
||||
import org.jooq.impl.DefaultExecuteListener;
|
||||
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
||||
import org.hibernate.jpa.HibernatePersistenceProvider;
|
||||
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
||||
import org.hibernate.tool.schema.TargetType;
|
||||
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
@ -83,50 +55,6 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
class JPAExample {
|
||||
|
||||
private static void run(EntityManager em, DSLContext ctx) {
|
||||
|
||||
// Set up database
|
||||
// ---------------
|
||||
Language english = new Language("English");
|
||||
Language german = new Language("German");
|
||||
|
||||
Actor umaThurman = new Actor("Uma", "Thurman");
|
||||
Actor davidCarradine = new Actor("David", "Carradine");
|
||||
Actor darylHannah = new Actor("Daryl", "Hannah");
|
||||
Actor michaelAngarano = new Actor("Michael", "Angarano");
|
||||
Actor reeceThompson = new Actor("Reece", "Thompson");
|
||||
|
||||
Film killBill = new Film(
|
||||
Title.of("Kill Bill"),
|
||||
english,
|
||||
111,
|
||||
Year.of(2015)
|
||||
);
|
||||
Film meerjungfrauen = new Film(
|
||||
Title.of("Meerjungfrauen ticken anders"),
|
||||
german,
|
||||
89,
|
||||
Year.of(2017)
|
||||
);
|
||||
|
||||
killBill.actors.addAll(Arrays.asList(umaThurman, davidCarradine, darylHannah));
|
||||
meerjungfrauen.actors.addAll(Arrays.asList(umaThurman, michaelAngarano, reeceThompson));
|
||||
|
||||
em.persist(english);
|
||||
em.persist(german);
|
||||
|
||||
em.persist(umaThurman);
|
||||
em.persist(davidCarradine);
|
||||
em.persist(darylHannah);
|
||||
em.persist(michaelAngarano);
|
||||
em.persist(reeceThompson);
|
||||
|
||||
em.persist(killBill);
|
||||
em.persist(meerjungfrauen);
|
||||
|
||||
// Flush your changes to the database to be sure that jOOQ can pick them up below
|
||||
// ------------------------------------------------------------------------------
|
||||
em.flush();
|
||||
|
||||
System.out.println(
|
||||
ctx.select(
|
||||
ACTOR.FIRSTNAME,
|
||||
@ -149,107 +77,7 @@ class JPAExample {
|
||||
);
|
||||
}
|
||||
|
||||
// Just ignore that enterprisish bootstrapping madness down there. The beef of the example is above this line
|
||||
// ----------------------------------------------------------------------------------------------------------
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public static void main(String[] args) throws Exception {
|
||||
Connection connection = null;
|
||||
EntityManagerFactory emf = null;
|
||||
EntityManager em = null;
|
||||
|
||||
try {
|
||||
|
||||
// Bootstrapping JDBC:
|
||||
Class.forName("org.h2.Driver");
|
||||
connection = DriverManager.getConnection("jdbc:h2:mem:jooq-jpa-example", "sa", "");
|
||||
final Connection c = connection;
|
||||
|
||||
// Creating an in-memory H2 database from our entities
|
||||
MetadataSources metadata = new MetadataSources(
|
||||
new StandardServiceRegistryBuilder()
|
||||
.applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
|
||||
.applySetting("javax.persistence.schema-generation-connection", connection)
|
||||
.applySetting("javax.persistence.create-database-schemas", true)
|
||||
|
||||
// [#5607] JPADatabase causes warnings - This prevents
|
||||
// them
|
||||
.applySetting(AvailableSettings.CONNECTION_PROVIDER, new ConnectionProvider() {
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public boolean isUnwrappableAs(Class unwrapType) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T unwrap(Class<T> unwrapType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
return c;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeConnection(Connection conn) throws SQLException {}
|
||||
|
||||
@Override
|
||||
public boolean supportsAggressiveRelease() {
|
||||
return true;
|
||||
}
|
||||
})
|
||||
.build());
|
||||
|
||||
metadata.addAnnotatedClass(Actor.class);
|
||||
metadata.addAnnotatedClass(Film.class);
|
||||
metadata.addAnnotatedClass(Language.class);
|
||||
|
||||
SchemaExport export = new SchemaExport();
|
||||
export.create(EnumSet.of(TargetType.DATABASE), metadata.buildMetadata());
|
||||
|
||||
// Setting up an EntityManager using Spring (much easier than out-of-the-box Hibernate)
|
||||
LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
|
||||
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
|
||||
adapter.setDatabasePlatform(SQLDialect.H2.thirdParty().hibernateDialect());
|
||||
bean.setDataSource(new SingleConnectionDataSource(connection, true));
|
||||
bean.setPackagesToScan("org.jooq.example.jpa.entity");
|
||||
bean.setJpaVendorAdapter(adapter);
|
||||
bean.setPersistenceUnitName("test");
|
||||
bean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
|
||||
bean.afterPropertiesSet();
|
||||
|
||||
emf = bean.getObject();
|
||||
em = emf.createEntityManager();
|
||||
|
||||
final EntityManager e = em;
|
||||
|
||||
// Run some Hibernate / jOOQ logic inside of a transaction
|
||||
em.getTransaction().begin();
|
||||
run(
|
||||
em,
|
||||
DSL.using(new DefaultConfiguration()
|
||||
.set(connection)
|
||||
.set(new DefaultExecuteListener() {
|
||||
@Override
|
||||
public void start(ExecuteContext ctx) {
|
||||
// Flush all changes from the EntityManager to the database for them to be visible in jOOQ
|
||||
e.flush();
|
||||
super.start(ctx);
|
||||
}
|
||||
})
|
||||
));
|
||||
em.getTransaction().commit();
|
||||
}
|
||||
finally {
|
||||
if (em != null)
|
||||
em.close();
|
||||
|
||||
if (emf != null)
|
||||
emf.close();
|
||||
|
||||
if (connection != null)
|
||||
connection.close();
|
||||
}
|
||||
Setup.run(JPAExample::run);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,228 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Other licenses:
|
||||
* -----------------------------------------------------------------------------
|
||||
* Commercial licenses for this work are available. These replace the above
|
||||
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
|
||||
* database integrations.
|
||||
*
|
||||
* For more information, please visit: http://www.jooq.org/licenses
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
package org.jooq.example.jpa;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Year;
|
||||
import java.util.Arrays;
|
||||
import java.util.EnumSet;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.ExecuteContext;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.example.jpa.embeddables.Title;
|
||||
import org.jooq.example.jpa.entity.Actor;
|
||||
import org.jooq.example.jpa.entity.Film;
|
||||
import org.jooq.example.jpa.entity.Language;
|
||||
import org.jooq.impl.DSL;
|
||||
import org.jooq.impl.DefaultConfiguration;
|
||||
import org.jooq.impl.DefaultExecuteListener;
|
||||
import org.jooq.tools.jdbc.LoggingConnection;
|
||||
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
||||
import org.hibernate.jpa.HibernatePersistenceProvider;
|
||||
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
||||
import org.hibernate.tool.schema.TargetType;
|
||||
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
final class Setup {
|
||||
|
||||
// This class sets up an EntityManager and configures the jOOQ DSLContext
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
static void run(BiConsumer<EntityManager, DSLContext> consumer) throws Exception {
|
||||
Connection connection = null;
|
||||
EntityManagerFactory emf = null;
|
||||
EntityManager em = null;
|
||||
|
||||
try {
|
||||
|
||||
// Bootstrapping JDBC:
|
||||
Class.forName("org.h2.Driver");
|
||||
connection = new LoggingConnection(DriverManager.getConnection("jdbc:h2:mem:jooq-jpa-example", "sa", ""));
|
||||
final Connection c = connection;
|
||||
|
||||
// Creating an in-memory H2 database from our entities
|
||||
MetadataSources metadata = new MetadataSources(
|
||||
new StandardServiceRegistryBuilder()
|
||||
.applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
|
||||
.applySetting("javax.persistence.schema-generation-connection", connection)
|
||||
.applySetting("javax.persistence.create-database-schemas", true)
|
||||
|
||||
// [#5607] JPADatabase causes warnings - This prevents
|
||||
// them
|
||||
.applySetting(AvailableSettings.CONNECTION_PROVIDER, new ConnectionProvider() {
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public boolean isUnwrappableAs(Class unwrapType) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T unwrap(Class<T> unwrapType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
return c;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeConnection(Connection conn) throws SQLException {}
|
||||
|
||||
@Override
|
||||
public boolean supportsAggressiveRelease() {
|
||||
return true;
|
||||
}
|
||||
})
|
||||
.build());
|
||||
|
||||
metadata.addAnnotatedClass(Actor.class);
|
||||
metadata.addAnnotatedClass(Film.class);
|
||||
metadata.addAnnotatedClass(Language.class);
|
||||
|
||||
SchemaExport export = new SchemaExport();
|
||||
export.create(EnumSet.of(TargetType.DATABASE), metadata.buildMetadata());
|
||||
|
||||
// Setting up an EntityManager using Spring (much easier than out-of-the-box Hibernate)
|
||||
LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
|
||||
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
|
||||
adapter.setDatabasePlatform(SQLDialect.H2.thirdParty().hibernateDialect());
|
||||
bean.setDataSource(new SingleConnectionDataSource(connection, true));
|
||||
bean.setPackagesToScan("org.jooq.example.jpa.entity");
|
||||
bean.setJpaVendorAdapter(adapter);
|
||||
bean.setPersistenceUnitName("test");
|
||||
bean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
|
||||
bean.afterPropertiesSet();
|
||||
|
||||
emf = bean.getObject();
|
||||
em = emf.createEntityManager();
|
||||
|
||||
final EntityManager e = em;
|
||||
|
||||
// Run some Hibernate / jOOQ logic inside of a transaction
|
||||
em.getTransaction().begin();
|
||||
data(em);
|
||||
|
||||
consumer.accept(
|
||||
em,
|
||||
DSL.using(new DefaultConfiguration()
|
||||
.set(connection)
|
||||
.set(new DefaultExecuteListener() {
|
||||
@Override
|
||||
public void start(ExecuteContext ctx) {
|
||||
// Flush all changes from the EntityManager to the database for them to be visible in jOOQ
|
||||
e.flush();
|
||||
super.start(ctx);
|
||||
}
|
||||
})
|
||||
));
|
||||
em.getTransaction().commit();
|
||||
}
|
||||
finally {
|
||||
if (em != null)
|
||||
em.close();
|
||||
|
||||
if (emf != null)
|
||||
emf.close();
|
||||
|
||||
if (connection != null)
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
static void data(EntityManager em) {
|
||||
|
||||
// Set up database
|
||||
// ---------------
|
||||
Language english = new Language("English");
|
||||
Language german = new Language("German");
|
||||
|
||||
Actor umaThurman = new Actor("Uma", "Thurman");
|
||||
Actor davidCarradine = new Actor("David", "Carradine");
|
||||
Actor darylHannah = new Actor("Daryl", "Hannah");
|
||||
Actor michaelAngarano = new Actor("Michael", "Angarano");
|
||||
Actor reeceThompson = new Actor("Reece", "Thompson");
|
||||
|
||||
Film killBill = new Film(
|
||||
Title.of("Kill Bill"),
|
||||
english,
|
||||
111,
|
||||
Year.of(2015)
|
||||
);
|
||||
Film meerjungfrauen = new Film(
|
||||
Title.of("Meerjungfrauen ticken anders"),
|
||||
german,
|
||||
89,
|
||||
Year.of(2017)
|
||||
);
|
||||
|
||||
killBill.actors.addAll(Arrays.asList(umaThurman, davidCarradine, darylHannah));
|
||||
meerjungfrauen.actors.addAll(Arrays.asList(umaThurman, michaelAngarano, reeceThompson));
|
||||
|
||||
em.persist(english);
|
||||
em.persist(german);
|
||||
|
||||
em.persist(umaThurman);
|
||||
em.persist(davidCarradine);
|
||||
em.persist(darylHannah);
|
||||
em.persist(michaelAngarano);
|
||||
em.persist(reeceThompson);
|
||||
|
||||
em.persist(killBill);
|
||||
em.persist(meerjungfrauen);
|
||||
em.flush();
|
||||
}
|
||||
|
||||
private Setup() {}
|
||||
}
|
||||
@ -31,7 +31,7 @@ import org.jooq.impl.TableImpl;
|
||||
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
|
||||
public class Actor extends TableImpl<ActorRecord> {
|
||||
|
||||
private static final long serialVersionUID = -102517780;
|
||||
private static final long serialVersionUID = -197980969;
|
||||
|
||||
/**
|
||||
* The reference instance of <code>PUBLIC.ACTOR</code>
|
||||
@ -49,17 +49,17 @@ public class Actor extends TableImpl<ActorRecord> {
|
||||
/**
|
||||
* The column <code>PUBLIC.ACTOR.ACTORID</code>.
|
||||
*/
|
||||
public final TableField<ActorRecord, Integer> ACTORID = createField("ACTORID", org.jooq.impl.SQLDataType.INTEGER.nullable(false).identity(true), this, "");
|
||||
public final TableField<ActorRecord, Integer> ACTORID = createField(DSL.name("ACTORID"), org.jooq.impl.SQLDataType.INTEGER.nullable(false).identity(true), this, "");
|
||||
|
||||
/**
|
||||
* The column <code>PUBLIC.ACTOR.FIRSTNAME</code>.
|
||||
*/
|
||||
public final TableField<ActorRecord, String> FIRSTNAME = createField("FIRSTNAME", org.jooq.impl.SQLDataType.VARCHAR(255), this, "");
|
||||
public final TableField<ActorRecord, String> FIRSTNAME = createField(DSL.name("FIRSTNAME"), org.jooq.impl.SQLDataType.VARCHAR(255), this, "");
|
||||
|
||||
/**
|
||||
* The column <code>PUBLIC.ACTOR.LASTNAME</code>.
|
||||
*/
|
||||
public final TableField<ActorRecord, String> LASTNAME = createField("LASTNAME", org.jooq.impl.SQLDataType.VARCHAR(255), this, "");
|
||||
public final TableField<ActorRecord, String> LASTNAME = createField(DSL.name("LASTNAME"), org.jooq.impl.SQLDataType.VARCHAR(255), this, "");
|
||||
|
||||
/**
|
||||
* Create a <code>PUBLIC.ACTOR</code> table reference
|
||||
|
||||
@ -4,7 +4,6 @@
|
||||
package org.jooq.example.jpa.jooq.tables;
|
||||
|
||||
|
||||
import java.time.Year;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@ -32,7 +31,7 @@ import org.jooq.impl.TableImpl;
|
||||
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
|
||||
public class Film extends TableImpl<FilmRecord> {
|
||||
|
||||
private static final long serialVersionUID = 1433747065;
|
||||
private static final long serialVersionUID = 1425273525;
|
||||
|
||||
/**
|
||||
* The reference instance of <code>PUBLIC.FILM</code>
|
||||
@ -50,32 +49,33 @@ public class Film extends TableImpl<FilmRecord> {
|
||||
/**
|
||||
* The column <code>PUBLIC.FILM.FILMID</code>.
|
||||
*/
|
||||
public final TableField<FilmRecord, Integer> FILMID = createField("FILMID", org.jooq.impl.SQLDataType.INTEGER.nullable(false).identity(true), this, "");
|
||||
public final TableField<FilmRecord, Integer> FILMID = createField(DSL.name("FILMID"), org.jooq.impl.SQLDataType.INTEGER.nullable(false).identity(true), this, "");
|
||||
|
||||
/**
|
||||
* The column <code>PUBLIC.FILM.LENGTH</code>.
|
||||
*/
|
||||
public final TableField<FilmRecord, Integer> LENGTH = createField("LENGTH", org.jooq.impl.SQLDataType.INTEGER, this, "");
|
||||
public final TableField<FilmRecord, Integer> LENGTH = createField(DSL.name("LENGTH"), org.jooq.impl.SQLDataType.INTEGER, this, "");
|
||||
|
||||
/**
|
||||
* The column <code>PUBLIC.FILM.RELEASE_YEAR</code>.
|
||||
* @deprecated Unknown data type. Please define an explicit {@link org.jooq.Binding} to specify how this type should be handled. Deprecation can be turned off using {@literal <deprecationOnUnknownTypes/>} in your code generator configuration.
|
||||
*/
|
||||
public final TableField<FilmRecord, Year> RELEASE_YEAR = createField("RELEASE_YEAR", org.jooq.impl.SQLDataType.INTEGER, this, "", new org.jooq.impl.JPAConverter(org.jooq.example.jpa.converters.YearConverter.class));
|
||||
@java.lang.Deprecated
|
||||
public final TableField<FilmRecord, Object> RELEASE_YEAR = createField(DSL.name("RELEASE_YEAR"), org.jooq.impl.SQLDataType.INTEGER, this, "", new org.jooq.impl.JPAConverter(org.jooq.example.jpa.converters.YearConverter.class));
|
||||
|
||||
/**
|
||||
* The column <code>PUBLIC.FILM.TITLE</code>.
|
||||
*/
|
||||
public final TableField<FilmRecord, String> TITLE = createField("TITLE", org.jooq.impl.SQLDataType.VARCHAR(255), this, "");
|
||||
public final TableField<FilmRecord, String> TITLE = createField(DSL.name("TITLE"), org.jooq.impl.SQLDataType.VARCHAR(255), this, "");
|
||||
|
||||
/**
|
||||
* The column <code>PUBLIC.FILM.LANGUAGE_LANGUAGEID</code>.
|
||||
*/
|
||||
public final TableField<FilmRecord, Integer> LANGUAGE_LANGUAGEID = createField("LANGUAGE_LANGUAGEID", org.jooq.impl.SQLDataType.INTEGER, this, "");
|
||||
public final TableField<FilmRecord, Integer> LANGUAGE_LANGUAGEID = createField(DSL.name("LANGUAGE_LANGUAGEID"), org.jooq.impl.SQLDataType.INTEGER, this, "");
|
||||
|
||||
/**
|
||||
* The column <code>PUBLIC.FILM.ORIGINALLANGUAGE_LANGUAGEID</code>.
|
||||
*/
|
||||
public final TableField<FilmRecord, Integer> ORIGINALLANGUAGE_LANGUAGEID = createField("ORIGINALLANGUAGE_LANGUAGEID", org.jooq.impl.SQLDataType.INTEGER, this, "");
|
||||
public final TableField<FilmRecord, Integer> ORIGINALLANGUAGE_LANGUAGEID = createField(DSL.name("ORIGINALLANGUAGE_LANGUAGEID"), org.jooq.impl.SQLDataType.INTEGER, this, "");
|
||||
|
||||
/**
|
||||
* Create a <code>PUBLIC.FILM</code> table reference
|
||||
|
||||
@ -30,7 +30,7 @@ import org.jooq.impl.TableImpl;
|
||||
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
|
||||
public class FilmActor extends TableImpl<FilmActorRecord> {
|
||||
|
||||
private static final long serialVersionUID = -2005425571;
|
||||
private static final long serialVersionUID = 1565489533;
|
||||
|
||||
/**
|
||||
* The reference instance of <code>PUBLIC.FILM_ACTOR</code>
|
||||
@ -48,12 +48,12 @@ public class FilmActor extends TableImpl<FilmActorRecord> {
|
||||
/**
|
||||
* The column <code>PUBLIC.FILM_ACTOR.FILMS_FILMID</code>.
|
||||
*/
|
||||
public final TableField<FilmActorRecord, Integer> FILMS_FILMID = createField("FILMS_FILMID", org.jooq.impl.SQLDataType.INTEGER.nullable(false), this, "");
|
||||
public final TableField<FilmActorRecord, Integer> FILMS_FILMID = createField(DSL.name("FILMS_FILMID"), org.jooq.impl.SQLDataType.INTEGER.nullable(false), this, "");
|
||||
|
||||
/**
|
||||
* The column <code>PUBLIC.FILM_ACTOR.ACTORS_ACTORID</code>.
|
||||
*/
|
||||
public final TableField<FilmActorRecord, Integer> ACTORS_ACTORID = createField("ACTORS_ACTORID", org.jooq.impl.SQLDataType.INTEGER.nullable(false), this, "");
|
||||
public final TableField<FilmActorRecord, Integer> ACTORS_ACTORID = createField(DSL.name("ACTORS_ACTORID"), org.jooq.impl.SQLDataType.INTEGER.nullable(false), this, "");
|
||||
|
||||
/**
|
||||
* Create a <code>PUBLIC.FILM_ACTOR</code> table reference
|
||||
|
||||
@ -31,7 +31,7 @@ import org.jooq.impl.TableImpl;
|
||||
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
|
||||
public class Language extends TableImpl<LanguageRecord> {
|
||||
|
||||
private static final long serialVersionUID = 1261703936;
|
||||
private static final long serialVersionUID = -636203274;
|
||||
|
||||
/**
|
||||
* The reference instance of <code>PUBLIC.LANGUAGE</code>
|
||||
@ -49,12 +49,12 @@ public class Language extends TableImpl<LanguageRecord> {
|
||||
/**
|
||||
* The column <code>PUBLIC.LANGUAGE.LANGUAGEID</code>.
|
||||
*/
|
||||
public final TableField<LanguageRecord, Integer> LANGUAGEID = createField("LANGUAGEID", org.jooq.impl.SQLDataType.INTEGER.nullable(false).identity(true), this, "");
|
||||
public final TableField<LanguageRecord, Integer> LANGUAGEID = createField(DSL.name("LANGUAGEID"), org.jooq.impl.SQLDataType.INTEGER.nullable(false).identity(true), this, "");
|
||||
|
||||
/**
|
||||
* The column <code>PUBLIC.LANGUAGE.NAME</code>.
|
||||
*/
|
||||
public final TableField<LanguageRecord, String> NAME = createField("NAME", org.jooq.impl.SQLDataType.VARCHAR(255), this, "");
|
||||
public final TableField<LanguageRecord, String> NAME = createField(DSL.name("NAME"), org.jooq.impl.SQLDataType.VARCHAR(255), this, "");
|
||||
|
||||
/**
|
||||
* Create a <code>PUBLIC.LANGUAGE</code> table reference
|
||||
|
||||
@ -4,8 +4,6 @@
|
||||
package org.jooq.example.jpa.jooq.tables.records;
|
||||
|
||||
|
||||
import java.time.Year;
|
||||
|
||||
import org.jooq.Field;
|
||||
import org.jooq.Record1;
|
||||
import org.jooq.Record6;
|
||||
@ -18,9 +16,9 @@ import org.jooq.impl.UpdatableRecordImpl;
|
||||
* This class is generated by jOOQ.
|
||||
*/
|
||||
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
|
||||
public class FilmRecord extends UpdatableRecordImpl<FilmRecord> implements Record6<Integer, Integer, Year, String, Integer, Integer> {
|
||||
public class FilmRecord extends UpdatableRecordImpl<FilmRecord> implements Record6<Integer, Integer, Object, String, Integer, Integer> {
|
||||
|
||||
private static final long serialVersionUID = 2076456687;
|
||||
private static final long serialVersionUID = -596044573;
|
||||
|
||||
/**
|
||||
* Setter for <code>PUBLIC.FILM.FILMID</code>.
|
||||
@ -51,17 +49,19 @@ public class FilmRecord extends UpdatableRecordImpl<FilmRecord> implements Recor
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for <code>PUBLIC.FILM.RELEASE_YEAR</code>.
|
||||
* @deprecated Unknown data type. Please define an explicit {@link org.jooq.Binding} to specify how this type should be handled. Deprecation can be turned off using {@literal <deprecationOnUnknownTypes/>} in your code generator configuration.
|
||||
*/
|
||||
public void setReleaseYear(Year value) {
|
||||
@java.lang.Deprecated
|
||||
public void setReleaseYear(Object value) {
|
||||
set(2, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for <code>PUBLIC.FILM.RELEASE_YEAR</code>.
|
||||
* @deprecated Unknown data type. Please define an explicit {@link org.jooq.Binding} to specify how this type should be handled. Deprecation can be turned off using {@literal <deprecationOnUnknownTypes/>} in your code generator configuration.
|
||||
*/
|
||||
public Year getReleaseYear() {
|
||||
return (Year) get(2);
|
||||
@java.lang.Deprecated
|
||||
public Object getReleaseYear() {
|
||||
return get(2);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -126,7 +126,7 @@ public class FilmRecord extends UpdatableRecordImpl<FilmRecord> implements Recor
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Row6<Integer, Integer, Year, String, Integer, Integer> fieldsRow() {
|
||||
public Row6<Integer, Integer, Object, String, Integer, Integer> fieldsRow() {
|
||||
return (Row6) super.fieldsRow();
|
||||
}
|
||||
|
||||
@ -134,7 +134,7 @@ public class FilmRecord extends UpdatableRecordImpl<FilmRecord> implements Recor
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Row6<Integer, Integer, Year, String, Integer, Integer> valuesRow() {
|
||||
public Row6<Integer, Integer, Object, String, Integer, Integer> valuesRow() {
|
||||
return (Row6) super.valuesRow();
|
||||
}
|
||||
|
||||
@ -155,10 +155,11 @@ public class FilmRecord extends UpdatableRecordImpl<FilmRecord> implements Recor
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @deprecated Unknown data type. Please define an explicit {@link org.jooq.Binding} to specify how this type should be handled. Deprecation can be turned off using {@literal <deprecationOnUnknownTypes/>} in your code generator configuration.
|
||||
*/
|
||||
@java.lang.Deprecated
|
||||
@Override
|
||||
public Field<Year> field3() {
|
||||
public Field<Object> field3() {
|
||||
return Film.FILM.RELEASE_YEAR;
|
||||
}
|
||||
|
||||
@ -203,10 +204,11 @@ public class FilmRecord extends UpdatableRecordImpl<FilmRecord> implements Recor
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @deprecated Unknown data type. Please define an explicit {@link org.jooq.Binding} to specify how this type should be handled. Deprecation can be turned off using {@literal <deprecationOnUnknownTypes/>} in your code generator configuration.
|
||||
*/
|
||||
@java.lang.Deprecated
|
||||
@Override
|
||||
public Year component3() {
|
||||
public Object component3() {
|
||||
return getReleaseYear();
|
||||
}
|
||||
|
||||
@ -251,10 +253,11 @@ public class FilmRecord extends UpdatableRecordImpl<FilmRecord> implements Recor
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @deprecated Unknown data type. Please define an explicit {@link org.jooq.Binding} to specify how this type should be handled. Deprecation can be turned off using {@literal <deprecationOnUnknownTypes/>} in your code generator configuration.
|
||||
*/
|
||||
@java.lang.Deprecated
|
||||
@Override
|
||||
public Year value3() {
|
||||
public Object value3() {
|
||||
return getReleaseYear();
|
||||
}
|
||||
|
||||
@ -301,10 +304,11 @@ public class FilmRecord extends UpdatableRecordImpl<FilmRecord> implements Recor
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @deprecated Unknown data type. Please define an explicit {@link org.jooq.Binding} to specify how this type should be handled. Deprecation can be turned off using {@literal <deprecationOnUnknownTypes/>} in your code generator configuration.
|
||||
*/
|
||||
@java.lang.Deprecated
|
||||
@Override
|
||||
public FilmRecord value3(Year value) {
|
||||
public FilmRecord value3(Object value) {
|
||||
setReleaseYear(value);
|
||||
return this;
|
||||
}
|
||||
@ -340,7 +344,7 @@ public class FilmRecord extends UpdatableRecordImpl<FilmRecord> implements Recor
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public FilmRecord values(Integer value1, Integer value2, Year value3, String value4, Integer value5, Integer value6) {
|
||||
public FilmRecord values(Integer value1, Integer value2, Object value3, String value4, Integer value5, Integer value6) {
|
||||
value1(value1);
|
||||
value2(value2);
|
||||
value3(value3);
|
||||
@ -364,7 +368,7 @@ public class FilmRecord extends UpdatableRecordImpl<FilmRecord> implements Recor
|
||||
/**
|
||||
* Create a detached, initialised FilmRecord
|
||||
*/
|
||||
public FilmRecord(Integer filmid, Integer length, Year releaseYear, String title, Integer languageLanguageid, Integer originallanguageLanguageid) {
|
||||
public FilmRecord(Integer filmid, Integer length, Object releaseYear, String title, Integer languageLanguageid, Integer originallanguageLanguageid) {
|
||||
super(Film.FILM);
|
||||
|
||||
set(0, filmid);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user