Answer the question
In order to leave comments, you need to log in
Tables are not enriched when Entity is created?
When enriching a PostgreSQL table with data, using a SQL file and mode
spring.jpa.hibernate.ddl-auto=create-dropI catch an exception - PSQLException .
@Data
@Entity
@Table(name = "books", schema = "public")
@NoArgsConstructor
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String priceOld;
private String price;
}
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create-drop
insert into books (title, price_old, price) values ('about animals', '$17.99', '$5.99');
ERROR: relation "books" does not exist
Answer the question
In order to leave comments, you need to log in
Good afternoon!
Yes, of course, the sql file is executed first.
It is possible to implement on a miscellaneous:
1) Try to take out DDL requests in sql a file. (schema.sql)
2) Or vice versa, initialize the contents of data.sql using java code. For example, implement the CommandLineRunner interface
3) Try to use the FlyWay lib for migrations
spring.jpa.hibernate.ddl-auto=create-drop
This means that all created tables will be dropped and at the time of start there are no tables in the database where your inserts should be added.
Useful information - https://www.baeldung.com/spring-boot-data-sql-and-...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question