使用Hibernate锁定表

时间:2018-07-17 12:16:31

标签: sql hibernate spring-data

我需要制作锁表,例如(“祖先”:“后代”) 基于SQL代码:[代码示例] [https://i.stack.imgur.com/odr4C.png]

我的代码在Hibernate中: 评论实体:

@Entity
@Table(name = "comments")
public class Comments {

    @Id
    @Column(name = "comment_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long commentId;

    @Column(name = "author")
    private BigInteger author;

    @OneToMany(mappedBy = "descendant", fetch = FetchType.LAZY )
    private List<ThreePaths> descThreePaths;

    @OneToMany(mappedBy = "ancestor", fetch = FetchType.LAZY)
    private List<ThreePaths> ancThreePaths;

ThreePaths实体:

@Entity
@Table(name = "three_paths")
public class PipeTopologyLock implements Serializable {

    @Id
    @ManyToOne(optional = false, cascade = CascadeType.ALL)
    @JoinColumn(name = "ancestor")
    private Comments ancestor;

    @Id
    @ManyToOne(optional = false, cascade = CascadeType.ALL)
    @JoinColumn(name = "descendant")
    private Comments descendant;
}

但是,执行后,我没有在Comment实体上指定的外键。

Postgres的代码:

CREATE TABLE three_paths (
    descendant int8 NOT NULL,
    ancestor int8 NOT NULL,
    CONSTRAINT three_paths_pkey PRIMARY KEY (descendant, ancestor)
)

能告诉我我需要如何为祖先和后代添加外键。 谢谢!

0 个答案:

没有答案