【发布时间】:2022-01-13 15:49:49
【问题描述】:
我有以下实体:
@Entity
@Getter
@Setter
@Table(name = "nomenclature", schema = "public")
public class Nomenclature implements Serializable {
@Id
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@OneToMany
@JoinTable(name="nomenclature_versions",
joinColumns= @JoinColumn(name="nomenclature_id", referencedColumnName="id"))
private List<NomenclatureVersion> version;
}
和
@Entity
@Setter
@Getter
@Table(name = "nomenclature_versions", schema = "public")
@NoArgsConstructor
public class NomenclatureVersion implements Serializable {
@Id
@Generated(GenerationTime.INSERT)
@Column(name = "id")
private Long id;
@Column(name = "nomenclature_id")
private Long nomenclatureId;
@Column(name = "user_id")
private Long userId;
@Column(name = "creation_date")
private LocalDateTime creationDate;
@Column(name = "puls_code")
private String pulsCode;
@Column(name = "pic_url")
private String picUrl;
@Column(name = "current")
private boolean current;
}
当我尝试使用 JPARepository getById(id) 方法获取命名法时,我得到了org.postgresql.util.PSQLException: ERROR: column version0_.version_id does not exist
感觉问题出在 Hibernate Naming Strategies 但我无法解决。
有没有其他方法可以让 Hibernate 知道它应该使用哪一列来连接表?
【问题讨论】:
-
JoinTable 告诉 JPA 使用关系表来包含映射,很像 M:M 情况。可以使用对主实体表的外键的约束来控制关系表的实际基数,因此关系表可以用于任何关系类型,甚至是 1:1。让 1:1 直接在实体中使用 FK 更为常见且通常更有效,从而减少了一些表连接和开销。您已将其设置为尝试将实体表用作关系表,这是一个坏主意 - 它适用于读取,但不适用于写入。