gpt4 book ai didi

java - JPA 中外键的主详细信息 ID

转载 作者:太空宇宙 更新时间:2023-11-04 11:10:52 25 4
gpt4 key购买 nike

我有三张表,其中之一是(场景),它是主表,另外两个表(链接、节点)是它的详细信息。我希望 JPA 为我保留外键,同时在链接和节点表中保留场景,而无需我的干预,有没有人帮我做这个???

enter image description here

场景:

@Entity
@NamedQueries({ @NamedQuery(name = "Scenario.findAll", query = "select o from Scenario o") })
public class Scenario implements Serializable {
private String className;
@Id
@Column(nullable = false,name="ID")
private int id;
private String linkFromPortIdProperty;
private String linkToPortIdProperty;
private String UUID;
@OneToMany(mappedBy = "scenario", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private List<Link> linkDataArray;
@OneToMany(mappedBy = "scenario1", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private List<Node> nodeDataArray;

链接:

@Entity
@NamedQueries({ @NamedQuery(name = "Lnk.findAll", query = "select o from Link o") })
@Table(name = "LNK", schema = "DEV_SGMNT")

public class Link implements Serializable {
private static final long serialVersionUID = -5262332521707870082L;


@Transient
private String to;
private int fromPort;
@Id
@Column(nullable = false)
private int id;
private double[] points;
@Transient
private String from;
private int toPort;
@ManyToOne
@JoinColumn(name = "SCENARIO_ID")
private Scenario scenario;

节点:

@Entity
@NamedQueries({ @NamedQuery(name = "Node.findAll", query = "select o from Node o") })
public class Node implements Serializable {
private static final long serialVersionUID = -8401815303355236645L;
private String classname;
@Id
@Column(nullable = false)
private int id;
private BigDecimal inbound;
private String key;
private BigDecimal outbound;
private String properties;
private String tab;
private String text;
private String vizitems;
@ManyToOne
@JoinColumn(name = "SCENARIO_ID")
private Scenario scenario1;

最佳答案

在持久化主实体(场景)时,对要持久化的所有详细对象使用 CascadeType.ALL。

@OneToMany(mappedBy = "scenario",cascade = CascadeType.ALL, orphanRemoval = true) 
private List<Link> linkDataArray;

阅读 vladmihalcea 撰写的指南:

Reference

关于java - JPA 中外键的主详细信息 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46013378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com