JPA – 使用 OR 条件的 OneToMany

huangapple 未分类评论46阅读模式
英文:

JPA - OneToMany with OR condition

问题

"Node"实体中,我想创建一个名为"edges"的"OneToMany"关系,用于获取与节点关联的所有边(无论是第一个节点还是第二个节点)。

以下是相应的SQL查询:

select *
from node n join edge e
on (n.id = e.node_1_id and n.type = e.node_1_type) or (n.id = e.node_2_id and n.type = e.node_2_type)
where n.id = 1 and n.type = 'type';

如何创建这样的关系?

英文:

I have 2 SQL tables, "node" and "edge".

The "node" table has a composite primary key, which consists of two columns: "id" and "type".

The "edge" table contains the following columns:

  • node_1_id: the id of the first node of the edge
  • node_1_type: the type of the first node of the edge
  • node_2_id: the id of the second node of the edge
  • node_2_type: the type of the second node of the edge

There are also two foreign key constraints that link ("node_1_id", "node_1_type") and ("node_2_id", "node_2_type") to the "id" and "type" of the "node" table.

I've created the following jpa entity:

@Entity
@Table(name = "edge")
public class Edge {
    @Id
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "description", nullable = true, length = 100)
    private String description;
    
    @ManyToOne(fetch=FetchType.LAZY)
	@JoinColumns({
		@JoinColumn(name = "node_1_id", referencedColumnName = "id"),
		@JoinColumn(name = "node_1_type", referencedColumnName = "type")
	})
    private Node node1;

    @ManyToOne(fetch=FetchType.LAZY)
	@JoinColumns({
		@JoinColumn(name = "node_2_id", referencedColumnName = "id"),
		@JoinColumn(name = "node_2_type", referencedColumnName = "type")
	})
    private Node node2;
}

In "Node" entity I would like to create a "OneToMany" relation called "edges" that fetches all the edges linked to a node (it doesn't matter if it is the first or the second node).

This is the correspondent SQL query:

select *
from node n join edge e
on (n.id = e.node_1_id and n.type = e.node_1_type) or (n.id = e.node_2_id and n.type = e.node_2_type)
where n.id = 1 and n.type = 'type';

How can I create such relation?

答案1

得分: 0

以下是翻译好的代码部分:

public class Node {

    @OneToMany(mappedBy="node1")
    private List<Edge> firstNodeEdges;

    @OneToMany(mappedBy="node2")
    private List<Edge> secondNodeEdges;

    public List<Edge> getEdges() {
        return Stream.concat(firstNodeEdges.stream(), secondNodeEdges.stream())
            .collect(Collectors.toList());
    }
}
英文:

Is it mandatory to be a single @OneToMany relationship? Otherwise you could add the counterparts to the two @ManyToOne relationships and create a getter that simply returns a single, merged list:

public class Node {

    @OneToMany(mappedBy=&quot;node1&quot;)
    private List&lt;Edge&gt; firstNodeEdges;

    @OneToMany(mappedBy=&quot;node2&quot;)
    private List&lt;Edge&gt; secondNodeEdges;

    public List&lt;Edge&gt; getEdges() {
        return Stream.concat(firstNodeEdges.stream(), secondNodeEdges.stream())
            .collect(Collectors.toList());
    }
}

huangapple
  • 本文由 发表于 2020年5月29日 18:00:07
  • 转载请务必保留本文链接:https://java.coder-hub.com/62083357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定