0
我有兩個與一對多連接相關的實體。一個是Path另一個是Point,一個路徑可以有幾個點。我對使用連接表加入這些表的MySQL端有了一些看法。我需要獲得查詢結果到該視圖。這是第一如何在查詢視圖時返回實體列表
@Entity
@Table(name = "paths")
public class Path {
@JsonIgnore
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private Long pathID;
@Column(name="path_name")
private String pathName;
@Column(name="path_type")
private Long pathType;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="uid")
@JsonIgnore
private User owner;
@Column(name="path_status")
private Long pathStatus;
@Column(name="description")
private String pathDescription;
@Column(name="created")
private Long created;
@OneToMany(mappedBy = "primaryKey.point", cascade = CascadeType.ALL)
private Set<PathPoints> pathPoints = new HashSet<PathPoints>();
public Long getPathID(){
return this.pathID;
}
public void setPathID(Long pathID){
this.pathID = pathID;
}
public String getPathName(){
return this.pathName;
}
public void setPathName(String pathName){
this.pathName = pathName;
}
public Long getPathType(){
return this.pathType;
}
public void setPathType(Long pathType){
this.pathType = pathType;
}
public Long getPathStatus(){
return this.pathStatus;
}
public void setPathStatus(Long pathStatus){
this.pathStatus = pathStatus;
}
public String getPathDescription(){
return this.pathDescription;
}
public void setPathDescription(String pathDescription){
this.pathDescription = pathDescription;
}
public Long getCreated(){
return this.created;
}
public void setCreated(Long created){
this.created = created;
}
public Set<PathPoints> getPathPoints() {
return pathPoints;
}
public void setPathPoints(Set<PathPoints> pathPoints) {
this.pathPoints = pathPoints;
}
public User getOwner() {
return owner;
}
public void setOwner(User owner) {
this.owner = owner;
}
}
這裏是第二
@Entity
@Table(name = "path_points")
@AssociationOverrides({
@AssociationOverride(name = "primaryKey.point", joinColumns = @JoinColumn(name = "point_id")),
@AssociationOverride(name = "primaryKey.path", joinColumns = @JoinColumn(name = "path_id"))
})
public class PathPoints{
private PathPointID primaryKey = new PathPointID();
private Long endTime;
private Long startTime;
@Column(name="end_time")
public Long getEndTime() {
return endTime;
}
public void setEndTime(Long endTime) {
this.endTime = endTime;
}
@Column(name="start_time")
public Long getStartTime() {
return startTime;
}
public void setStartTime(Long startTime) {
this.startTime = startTime;
}
@JsonIgnore
@EmbeddedId
public PathPointID getPrimaryKey() {
return primaryKey;
}
public void setPrimaryKey(PathPointID primaryKey) {
this.primaryKey = primaryKey;
}
@Transient
public Point getPoint() {
return primaryKey.getPoint();
}
public void setPoint(Point point) {
this.primaryKey.setPoint(point);;
}
@JsonIgnore
@Transient
public Path getPath() {
return primaryKey.getPath();
}
public void setPath(Path path) {
this.primaryKey.setPath(path);;
}
}
這就是ID級別
@Embeddable
public class PathPointID implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Point point;
private Path path;
@ManyToOne(cascade = CascadeType.ALL)
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
@ManyToOne(cascade = CascadeType.ALL)
public Path getPath() {
return path;
}
public void setPath(Path path) {
this.path = path;
}
}
但我需要有權訪問Points作爲集合 – user3378876
據我記得 - 我無法做到這一點。如果你想訪問Points作爲集合 - 你需要有外鍵約束 - 並且在你的視圖中不會有任何FK約束。理解這個想法? –
好吧,我相信我理解,但我需要使用點和路徑名稱來搜索路徑,因此我決定使用視圖。那麼還有另一種方式嗎? – user3378876