Hibernate 验证器看不到表格

问题描述 投票:0回答:1

问题是,在启用 hibernade.ddl-auto: validate 的情况下,我收到一条错误,指出 users_roles 表不存在: 无法初始化 JPA EntityManagerFactory:[PersistenceUnit:默认] 无法构建 Hibernate SessionFactory;嵌套异常是 org.hibernate.tool.schema.spi.SchemaManagementException:架构验证:缺少表 [users_roles] 该表已在数据库迁移中注册。某种愚蠢的错误?我找不到问题。

迁移代码:

create schema if not exists zhem;

create table if not exists zhem.users
(
    user_id    bigserial primary key,
    phone      varchar(11)  not null check ( phone similar to '79[0-9]{9}' ) unique,
    password   varchar(256) not null,
    email      varchar(256) check ( email similar to '([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)' ) unique,
    first_name varchar(32)  not null check ( length(trim(first_name)) >= 2 ),
    last_name  varchar(32) check ( length(trim(last_name)) >= 2 ),
    created_at timestamp default now(),
    updated_at timestamp default now()
);

create table if not exists zhem.roles
(
    role_id serial primary key,
    title   varchar(32) not null check ( length(trim(title)) >= 2 ) unique
);

create table if not exists zhem.users_roles
(
    user_role_id bigserial primary key,
    user_id      bigint not null references zhem.users (user_id),
    role_id      int    not null references zhem.roles (role_id),
    constraint unique_user_id_role_id unique (user_id, role_id)
);

基础实体:

@Data
@MappedSuperclass
public class BaseEntity {

    @CreationTimestamp
    @Column(name = "created_at")
    private LocalDateTime createdAt;

    @UpdateTimestamp
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

}

用户:

@Data
@EqualsAndHashCode(callSuper = true)
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(schema = "zhem", name = "users")
public class User extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    private Long id;

    @CheckPhoneNumber
    @Column(name = "phone", unique = true)
    private String phone;

    @NotBlank
    @Size(max = 256)
    @Column(name = "password")
    private String password;

    @Email
    @Size(max = 256)
    @Column(name = "email", unique = true)
    private String email;

    @NotBlank
    @Size(min = 2, max = 32)
    @Column(name = "first_name")
    private String firstName;

    @Size(min = 2, max = 32)
    @NullOrNotBlank
    @Column(name = "last_name")
    private String lastName;

    @ManyToMany(fetch = FetchType.EAGER)
    private Set<Role> roles;

}

角色:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(schema = "zhem", name = "roles")
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "role_id")
    private Integer id;

    @NotBlank
    @Size(min = 2, max = 32)
    @Column(name = "title", unique = true)
    private String title;

}

application.yaml:

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/zhem-db
    username: postgres
    password: root
  flyway:
    schemas:
      - public
      - zhem
  jpa:
    hibernate:
      ddl-auto: validate

我尝试了不同的方法来链接实体 - 单向、双向、双向多对多与链接实体。没有任何变化,桌子仍然丢失。

java database hibernate jpa many-to-many
1个回答
0
投票

我认为你应该指定

@JoinTable
参数以使 Hibernate 识别你的关系表
users_roles

public class User extends BaseEntity {

//...

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
    schema = "zhem",
    name = "users_roles",
    joinColumns = @JoinColumn(name = "user_id"),
    inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;

//...
}
© www.soinside.com 2019 - 2024. All rights reserved.