as a beginner in Spring Boot, I have a problem that I don’t fully understand and I hope someone in this forum can help me!? Maybe someone else has already encountered this problem?
I’ve started a new project with jpa, hibernate and a h2 db. I created two entities: one for a allergen and one for a product. The product also should have a one to many unidirectional relationship.
Problem:
I created the entities like listed below and run the project – everything worked fine! But as soon as I add a new field to my product, I get the following error message:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table products (id bigint not null, available boolean not null, description varchar(255), image_field blob, name varchar(100) not null, price integer not null check ((price_extern>=0) and (price_extern<=1000)), primary key (id))"; expects "identifier"
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table if exists products_allergens add constraint FKnf3ow75ap2qbokwprur8tdrg2 foreign key (product_id) references products" via JDBC [Table "products" not found;]
If I remove the new property, the code runs as expected. Also, it doesn’t make any difference which field I’m adding. String, Integer, all causes the same problem.
Allergen:
@Entity
@Table(name = "allergens")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
public class Allergen {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "shortcut", unique = true, nullable = false)
@NotBlank(message = "Shortcut cannot be blank")
@Size(min = 1, max = 2)
private String shortcut;
@Column(name = "name", unique = true, nullable = false, length = 50)
@NotBlank
private String name;
}
Product:
@Entity
@Table(name = "products")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "name", unique = true, nullable = false, length = 100)
@NotBlank(message = "Name cannot be blank")
private String name;
@Column(name = "description")
private String description;
@Column(name = "price", nullable = false)
@NotBlank(message = "Price cannot be blank")
@Min(0)
@Max(1000)
private int price = 0;
@Column(name = "available", nullable = false)
private boolean available = true;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@Column(name = "allergens")
private List<Allergen> allergens;
@Lob
@Column(name = "image_field")
private byte[] imageField;
}
And here are my property information in application.properties:
spring.application.name=backend
server.port=8000
spring.datasource.url=jdbc:h2:mem:testdb;DATABASE_TO_UPPER=false;
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=root
spring.datasource.password=
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.defer-datasource-initialization=true
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.sql.init.mode = always
Does anyone have any idea how to deal with this error message? Have I missed something? Apart from the two entities and the entries in the application.properties, nothing has been created or changed so far.
Thanks and have a great weekend!