Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot data.sql does not initialize data in Postgresql

I have a Spring Rest Api application. This is the entity:

@Entity
@Table(name="a_example")
public class Example
{
    @Id
    @GeneratedValue
    private Long id;

    private String name;
    private String description;

    @Column(unique = true, nullable = false)
    private String filename;
}

Here is my data.sql in the resources folder:

INSERT INTO a_example(name, description, filename) VALUES('Xyz', 'Lorem ipsum', 'xyz');

INSERT INTO a_example(name, description, filename) VALUES('Pqr', 'Lorem ipsum', 'pqr');

And my application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/xyz
spring.datasource.username=admin
spring.datasource.password=admin
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.initialization-mode=always
spring.datasource.initialize=true
spring.datasource.data=classpath:data.sql

spring.jpa.generate-ddl=true
spring.jpa.properties.hibernate.ddl-auto=create

spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect

The table exists, I can add data other ways, but I would prefer the data.sql to initalize it.

What am I missing?

like image 628
hansolo Avatar asked Nov 27 '25 00:11

hansolo


1 Answers

Change spring.jpa.properties.hibernate.ddl-auto=create to spring.jpa.hibernate.ddl-auto=create in the application.properties file And Change GeneratedValue annotation in the Example entity as below:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

FYI: You can use Liquibase or Flyway libraries too.

like image 126
Nasir Avatar answered Nov 29 '25 14:11

Nasir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!