3

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?

2
  • 2
    I guess spring.jpa.properties.hibernate.ddl-auto=create should be spring.jpa.hibernate.ddl-auto=create. Try adding this property as well spring.jpa.show-sql=true to see if the sql is being triggered or not Commented Mar 27, 2020 at 13:35
  • the only thing that worked for me was this prop spring.datasource.initialization-mode=always which is deprecated by the way lol Commented Mar 19, 2022 at 15:20

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.