[英]JPA Many to Two relationship
I'm new to JPA and was wondering if JPA contains a solution for my problem. 我是JPA的新手,想知道JPA是否包含针对我的问题的解决方案。 Els I will just need to create a ManyToMany relationship.
Els我将只需要创建一个ManyToMany关系。
My application contains roads and camera's. 我的应用程序包含道路和照相机。 A road starts and ends with a camera.
道路始于摄像机,始于摄像机。 I created this by creating a property
cameraPointA
and cameraPointB
in the RoadSegment
class. 我是通过在
RoadSegment
类中创建属性cameraPointA
和cameraPointB
来创建的。 This created a many to two relationship. 这创造了多对二的关系。 I thought I could define this as two many to one relationships but this seems not possible.
我以为可以将其定义为两个多对一的关系,但这似乎是不可能的。
CameraPoint.java
@Entity
public class CameraPoint implements Serializable {
@Id @GeneratedValue
private long id;
@OneToMany (mappedBy = "cameraPointA or cameraPointA") //<== The Problem
private List<RoadSegment> roads;
//...
}
RoadSegment.java
@Entity
public class RoadSegment implements Serializable {
@Id @GeneratedValue
private long id;
@ManyToOne(cascade = CascadeType.ALL)
private Region region;
@ManyToOne(optional=false)
private CameraPoint cameraPointA;
@ManyToOne(optional=false)
private CameraPoint cameraPointB;
//...
}
I don't know if this will work but maybe you can try adding to CameraPoint
another list of RoadSegments
, which indicate the Camera has a list of roads where the camera is the start and the other list indicates the camera is the last. 我不知道这是否会工作,但也许你可以尝试添加到
CameraPoint
的另一个列表RoadSegments
,这表明该相机具有道路的一个列表,其中相机的开始和其他列表显示的相机是最后一个。
@Entity
public class CameraPoint implements Serializable {
@Id @GeneratedValue
private long id;
@OneToMany (mappedBy = "cameraPointA")
private List<RoadSegment> roadsA;
@OneToMany (mappedBy = "cameraPointB")
private List<RoadSegment> roadsB;
//...
}
Is really necessary to use bidirectional relationship ? 使用双向关系真的必要吗? Maybe it is not and your model would be more easy.
也许不是,您的模型会更容易。
For example, if you always reach to the CameraPoint
from a RoadSegment
then you don't need the @OneToMany
relationship on CameraPoint
. 例如,如果你总是达不到的
CameraPoint
从RoadSegment
那么你不需要@OneToMany
的关系CameraPoint
。
The same applies for the inverse mode, if you always get a RoadSegment
from a previous CameraPoint
, then the @ManyToOne
relationship is not necessary. 这同样适用于反向模式下,如果你总是得到
RoadSegment
从以前的CameraPoint
,那么@ManyToOne
关系是没有必要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.