我正在尝试编写测试并在内存DB中使用。我的问题是从休眠实体为测试创建架构。
我有这样的实体
@Entity
@Table(name = "users", schema = "public")
@Getter
@Setter
@NoArgsConstructor
public class User {
@Id
@Column(name = "user_id", updatable = false, nullable = false, unique = true)
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;
@Column(name = "name")
private String name;
@Column(name = "product")
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinTable(name = "products_users",
joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "product_id")})
private Set<Product> products;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss")
@Column(name = "created_on")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Date createdOn;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss")
@Column(name = "modified_on")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Date modifiedOn;
public User(String name, Set<Product> products, Date createdOn, Date modifiedOn) {
this.name = name;
this.products = products;
this.createdOn = createdOn;
this.modifiedOn = modifiedOn;
}
}
@Entity
@Table(name = "product", schema = "public")
@Getter
@Setter
@NoArgsConstructor
public class Product {
@Id
@Column(name = "product_id", updatable = false, nullable = false, unique = true)
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;
@Column(name = "name")
private String name;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd@HH:mm:ss")
@Column(name = "created_on")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Date createdOn;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd@HH:mm:ss")
@Column(name = "modified_on")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Date modifiedOn;
public Product(String name, Date createdOn, Date modifiedOn) {
this.name = name;
this.createdOn = createdOn;
this.modifiedOn = modifiedOn;
}
}
@Entity
@Table(name = "products_users", schema = "public")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ProductsUsers {
@Id
@Column(name = "user_id", updatable = false, nullable = false, unique = true)
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private UUID userId;
@Column(name = "product_id")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private UUID productId;
}
和
application.properties
spring.output.ansi.enabled=ALWAYS
server.port=8081
debug=true
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=123
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=false
我有
application.properties
在测试包中
spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS public;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.default_schema=public
我得到这样的异常
org.hibernate.tool.schema.spi.CommandAcceptanceException:错误 执行DDL“更改表” public“。” products_users“删除约束 通过JDBC语句“ FK2w1ylwiyjuy67n1ycr5lv5or3”
由以下原因引起:org.h2.jdbc.JdbcSQLSyntaxErrorException:表 找不到“ products_users”; SQL语句:alter table “ public”。“ products_users”放置约束 “ FK2w1ylwiyjuy67n1ycr5lv5or3”