java—同一类的多个外键

pn9klfpd  于 2021-07-14  发布在  Java
关注(0)|答案(2)|浏览(299)

我有那两门课

@Data
@Entity
@Table(name="arrow")
public class Arrow {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false, updatable = false)
    private Integer id;

    @Column(name = "id_source")
    private Integer idSource;

    @Column(name = "id_destination")
    private Integer idDestination;

    @Column(name = "capacity")
    private Integer capacity;

    @OneToMany(mappedBy="id")
    private List<Edge> edges;
}

@Data
@Entity
@Table(name="edge")
public class Edge {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false, updatable = false)
    private Long id;

    @Column(name = "latitude")
    private Double latitude;

    @Column(name = "longitude")
    private Double longitude;

    @ManyToOne
    @JoinColumn(name="id_source", nullable=false)
    private Arrow idSource;

    @ManyToOne
    @JoinColumn(name="id_destination", nullable=false)
    private Arrow idDestination;
}

我希望idsource从边缘Map到id,也希望idDestination从边缘Map到id。。。
我试着把他们联系起来,但我不知道怎么。。。它们之间的正确关系是什么?或者我应该改变表的逻辑吗?
谢谢!

mcvgt66p

mcvgt66p1#

你应该吃点

@Data
@Entity
@Table(name="arrow")
public class Arrow {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false, updatable = false)
    private Integer id;

    @Column(name = "capacity")
    private Integer capacity;

    @OneToMany(mappedBy="arrow")
    private List<Edge> edges;
}

@Data
@Entity
@Table(name="edge")
public class Edge {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false, updatable = false)
    private Long id;

    @Column(name = "latitude")
    private Double latitude;

    @Column(name = "longitude")
    private Double longitude;

    @ManyToOne
    @JoinColumn(name="arrow_id", nullable=false)
    private Arrow arrow;
}

p、 在实体中使用@data有点异味,可能会引起问题

voj3qocg

voj3qocg2#

用这样的方法:

@Data
@Entity
@Table(name="arrow")
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Arrow {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false, updatable = false)
    @EqualsAndHashCode.Include
    private Integer id;

    @Column(name = "id_source")
    private Integer idSource;

    @Column(name = "id_destination")
    private Integer idDestination;

    @Column(name = "capacity")
    private Integer capacity;

    @OneToMany(mappedBy="idDestination")
    private Set<Edge> incomingEdges;

    @OneToMany(mappedBy="idDestination")
    private Set<Edge> outgoingEdges;
}

@Data
@Entity
@Table(name="edge")
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Edge {
    @EmbeddedId
    @EqualsAndHashCode.Include
    private EdgeId id;

    @Column(name = "latitude")
    private Double latitude;

    @Column(name = "longitude")
    private Double longitude;

    @ManyToOne
    @JoinColumn(name="id_source", nullable=false, insertable = false, updatable = false)
    private Arrow idSource;

    @ManyToOne
    @JoinColumn(name="id_destination", nullable=false, insertable = false, updatable = false)
    private Arrow idDestination;
}

@Data
@Embeddable
public class EdgeId {

    @Column(name="id_source", nullable=false)
    private Long idSource;

    @Column(name="id_destination", nullable=false)
    private Long idDestination;
}

相关问题