Created
April 17, 2022 22:47
-
-
Save rnkoaa/62bd8a8bf999fe3467f131852ec57a51 to your computer and use it in GitHub Desktop.
Jooq MultiSet select for one-to-many using Sakila DB on Postgresql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public record Actor(int id, String firstName, String lastName) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.jooq.impl.DSL.multiset; | |
import static org.jooq.impl.DSL.select; | |
import static org.sakila.jooq.tables.Film.FILM; | |
import static org.sakila.jooq.tables.FilmActor.FILM_ACTOR; | |
import static org.sakila.jooq.tables.FilmCategory.FILM_CATEGORY; | |
import java.sql.SQLException; | |
import java.util.List; | |
import org.jooq.DSLContext; | |
import org.jooq.Record1; | |
public class Application { | |
public static void main(String[] args) throws SQLException { | |
DSLContext dslContext = new DatabaseConfig() | |
.dslContext(); | |
Application application = new Application(); | |
List<Film> films = application.searchAllFilms(dslContext, 20); | |
// System.out.println(); | |
films.forEach(film -> System.out.println(film.title())); | |
} | |
List<Film> searchAllFilms(DSLContext dsl, int count) { | |
return dsl.select( | |
FILM.FILM_ID, | |
FILM.TITLE, | |
FILM.DESCRIPTION, | |
FILM.RELEASE_YEAR, | |
FILM.RENTAL_DURATION, | |
FILM.RENTAL_RATE, | |
FILM.LENGTH, | |
FILM.REPLACEMENT_COST, | |
FILM.RATING, | |
multiset( | |
dsl.select( | |
FILM_ACTOR.ACTOR_ID, | |
FILM_ACTOR.actor().FIRST_NAME, | |
FILM_ACTOR.actor().LAST_NAME | |
) | |
.from(FILM_ACTOR) | |
.where(FILM_ACTOR.FILM_ID.eq(FILM.FILM_ID)) | |
).as("actors").convertFrom(r -> r.map(rec -> new Actor(rec.value1(), rec.value2(), rec.value2()))), | |
multiset( | |
select(FILM_CATEGORY.category().NAME) | |
.from(FILM_CATEGORY) | |
.where(FILM_CATEGORY.FILM_ID.eq(FILM.FILM_ID)) | |
).as("categories").convertFrom(r -> r.map(Record1::value1)) | |
) | |
.from(FILM) | |
.where(FILM.FILM_ID.lt(count)) | |
.orderBy(FILM.TITLE) | |
.fetch(record -> new Film( | |
record.value1(), | |
record.value2(), | |
record.value3(), | |
record.value4(), | |
record.value5(), | |
record.value6(), | |
record.value7(), | |
record.value8(), | |
record.value9().getLiteral(), | |
record.value10(), | |
record.value11() | |
) | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DatabaseConfig { | |
public final String url = "jdbc:postgresql://localhost:5432/postgres"; | |
public final String user = "postgres"; | |
public final String password = "postgres"; | |
public DSLContext dslContext() throws SQLException { | |
final Connection connection = DriverManager.getConnection(url, user, password); | |
Settings settings = new Settings(); | |
settings.withExecuteLogging(false); | |
// settings.withL | |
return DSL.using(connection, SQLDialect.POSTGRES, settings); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3" | |
services: | |
auto-dealership: | |
container_name: sakila | |
image: 'postgres:14-alpine' | |
environment: | |
- POSTGRES_USER=postgres | |
- POSTGRES_DATABASE=postgres | |
- POSTGRES_PASSWORD=postgres | |
ports: | |
- '5432:5432' | |
volumes: | |
- sakila-postgres:/var/lib/postgresql/data | |
volumes: | |
sakila-postgres: | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public record Film( | |
int id, | |
String title, | |
String description, | |
int releaseYear, | |
int rentalDuration, | |
BigDecimal rentalRate, | |
int length, | |
BigDecimal replacementCost, | |
String mpaaRating, | |
List<Actor> actors, | |
List<String> categories | |
) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment