Reputation: 160
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?
Upvotes: 3
Views: 4374
Reputation: 531
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.
Upvotes: 2