Код: Выделить всё
CREATE TABLE BOOKS (
book_id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
title VARCHAR(50) NOT NULL,
author_last_name VARCHAR(30) NOT NULL,
author_first_name VARCHAR(30),
rating INTEGER CHECK (rating IS NULL OR (rating >= 1 and rating & l t ; d a t a b a s e . u r l & g t ; j d b c : h 2 : f i l e : . / t e s t d b ; M O D E = M S S Q L S e r v e r ; D B _ C L O S E _ O N _ E X I T = F A L S E ; D B _ C L O S E _ D E L A Y = - 1 ; I N I T = C R E A T E S C H E M A I F N O T E X I S T S p u b l i c \ ; s e t S C H E M A p u b l i c & l t ; / d a t a b a s e . u r l & g t ; < b r / > & l t ; d a t a b a s e . p a s s w o r d & g t ; p a s s w o r d & l t ; / d a t a b a s e . p a s s w o r d & g t ; < b r / > & l t ; j o o q . v e r s i o n & g t ; 3 . 1 8 . 1 3 & l t ; / j o o q . v e r s i o n & g t ; < b r / > & l t ; f l y w a y . c l e a n D i s a b l e d & g t ; f a l s e & l t ; / f l y w a y . c l e a n D i s a b l e d & g t ; < b r / > & l t ; h 2 . v e r s i o n & g t ; 2 . 1 . 2 1 4 & l t ; / h 2 . v e r s i o n & g t ; < b r / > & l t ; t e s t c o n t a i n e r s . v e r s i o n & g t ; 1 . 1 9 . 1 & l t ; / t e s t c o n t a i n e r s . v e r s i o n & g t ; < b r / > & l t ; t e s t c o n t a i n e r s - j o o q - c o d e g e n - m a v e n - p l u g i n . v e r s i o n & g t ; 0 . 0 . 2 & l t ; / t e s t c o n t a i n e r s - j o o q - c o d e g e n - m a v e n - p l u g i n . v e r s i o n & g t ; < b r / > & l t ; / p r o p e r t i e s & g t ; < b r / > < b r / > . . . < b r / > < b r / > & l t ; b u i l d & g t ; < b r / > & l t ; p l u g i n s & g t ; < b r / > & l t ; p l u g i n & g t ; < b r / > & l t ; g r o u p I d & g t ; o r g . s p r i n g f r a m e w o r k . b o o t & l t ; / g r o u p I d & g t ; < b r / > & l t ; a r t i f a c t I d & g t ; s p r i n g - b o o t - m a v e n - p l u g i n & l t ; / a r t i f a c t I d & g t ; < b r / > & l t ; c o n f i g u r a t i o n & g t ; < b r / > & l t ; e x c l u d e s & g t ; < b r / > & l t ; e x c l u d e & g t ; < b r / > & l t ; g r o u p I d & g t ; o r g . p r o j e c t l o m b o k & l t ; / g r o u p I d & g t ; < b r / > & l t ; a r t i f a c t I d & g t ; l o m b o k & l t ; / a r t i f a c t I d & g t ; < b r / > & l t ; / e x c l u d e & g t ; < b r / > & l t ; / e x c l u d e s & g t ; < b r / > & l t ; / c o n f i g u r a t i o n & g t ; < b r / > & l t ; / p l u g i n & g t ; < b r / > < b r / > < b r / > & l t ; p l u g i n & g t ; < b r / > & l t ; g r o u p I d & g t ; o r g . t e s t c o n t a i n e r s & l t ; / g r o u p I d & g t ; < b r / > & l t ; a r t i f a c t I d & g t ; t e s t c o n t a i n e r s - j o o q - c o d e g e n - m a v e n - p l u g i n & l t ; / a r t i f a c t I d & g t ; < b r / > & l t ; v e r s i o n & g t ; $ { t e s t c o n t a i n e r s - j o o q - c o d e g e n - m a v e n - p l u g i n . v e r s i o n } & l t ; / v e r s i o n & g t ; < b r / > & l t ; d e p e n d e n c i e s & g t ; < b r / > & l t ; d e p e n d e n c y & g t ; < b r / > & l t ; g r o u p I d & g t ; o r g . t e s t c o n t a i n e r s & l t ; / g r o u p I d & g t ; < b r / > & l t ; a r t i f a c t I d & g t ; p o s t g r e s q l & l t ; / a r t i f a c t I d & g t ; < b r / > ${testcontainers.version}
org.postgresql
postgresql
42.6.0
generate-jooq-sources
generate
generate-sources
POSTGRES
postgres:15.3-alpine
public
true
filesystem:src/main/resources/db/migration
.*
flyway_schema_history
public
example.micronaut.jooqtest
src/main/java
org.codehaus.mojo
build-helper-maven-plugin
generate-sources
add-source
src/main/jooq
org.flywaydb
flyway-maven-plugin
10.10.0
flyway-migrate
generate-sources
migrate
${database.user}
${database.password}
${database.url}
${database.password}
classpath:db/migration
Код: Выделить всё
@Repository
public class BookRepository {
@Autowired
DSLContext dslContext;
public Book addBook(Book book){
BooksRecord booksRecord = dslContext.insertInto(Tables.BOOKS).set(toRecord(book)).returning().fetchOneInto(BooksRecord.class);
book.setBook_id(booksRecord.getBookId());
return book;
}
public List getAllBooks(){
List booksRecords = dslContext.selectFrom(Tables.BOOKS).fetchInto(BooksRecord.class);
return booksRecords.stream().map(this::fromRecord).collect(Collectors.toList());
}
public Book fromRecord(BooksRecord record){
return Book.builder()
.book_id(record.getBookId())
.author_first_name(record.getAuthorFirstName())
.author_last_name(record.getAuthorLastName())
.rating(record.getRating())
.build();
}
public BooksRecord toRecord(Book book){
BooksRecord booksRecord = new BooksRecord();
booksRecord.setTitle(book.getTitle());
booksRecord.setAuthorFirstName(book.getAuthor_first_name());
booksRecord.setAuthorLastName(book.getAuthor_last_name());
booksRecord.setRating(book.getRating());
return booksRecord;
}
}
Однако, когда я пытаюсь добавить книгу, отправив запрос через Postman, я получаю исключение:
Код: Выделить всё
org.h2.jdbc.JdbcSQLSyntaxErrorException: Schema "public" not found; SQL statement:
select "book_id", "title", "author_last_name", "author_first_name", "rating" from final table (insert into "public"."books" ("title", "author_last_name", "author_first_name", "rating") values (?, ?, ?, ?)) "books" [90079-214]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:632) ~[h2-2.1.214.jar:2.1.214]
DataSourceConfig .java
Код: Выделить всё
@Configuration
public class DataSourceConfiguration {
@Bean
public DataSource dataSource() {
return DataSourceBuilder.create()
.url("jdbc:h2:file:./testdb;INIT=CREATE SCHEMA IF NOT EXISTS public\\;SET SCHEMA public;")
.username("sa")
.password("password")
.driverClassName("org.h2.Driver")
.build();
}
}
Код: Выделить всё
@Configuration
public class JooqConfiguration {
@Autowired
private DataSource dataSource;
@Bean(name = "jooqConfig")
public DefaultConfiguration jooqConfiguration() {
DefaultConfiguration configuration = new DefaultConfiguration();
configuration.set(SQLDialect.H2);
configuration.setDataSource(dataSource);
// Set the default schema name
configuration.set(new Settings().withRenderSchema(false).withRenderSchema(false)
.withRenderMapping(new RenderMapping().withSchemata(new MappedSchema().withInput("public").withOutput("PUBLIC"))));
return configuration;
}
@Bean(name = "defaultDslContext")
public DefaultDSLContext dslContext() {
return new DefaultDSLContext(jooqConfiguration());
}
}
Код: Выделить всё
org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "books" not found (candidates are: "BOOKS"); SQL statement:
select "book_id", "title", "author_last_name", "author_first_name", "rating" from final table (insert into "books" ("title", "author_last_name", "author_first_name", "rating") values (?, ?, ?, ?)) "books" [42103-214]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:502) ~[h2-2.1.214.jar:2.1.214]
Подробнее здесь: https://stackoverflow.com/questions/781 ... n-together