查詢1(工作正常!):JPA JPQL查詢,其中object1 = Object2的
em.createQuery(
"SELECT r FROM Route r WHERE r.start.x = :x"
, Route.class).setParameter("x", start.getX())
QUERY2(ID真的很喜歡這個工作!):
em.createQuery(
"SELECT r FROM Route r WHERE r.start = :x"
, Route.class).setParameter("x", start)
.setMaxResults(20)
拋出: TransientObjectException :對象引用未保存的瞬態實例 - 在沖刷前保存瞬態實例
路由實體:
@Entity
@XmlRootElement(name="route")
@XmlAccessorType(XmlAccessType.NONE)
public class Route {
private Long id;
private User user;
private Location start;
private Location finish;
public Route() {
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne
@JoinColumn
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@OneToOne(cascade=CascadeType.PERSIST)
public Location getStart() {
return start;
}
public void setStart(Location start) {
this.start = start;
}
@OneToOne(cascade=CascadeType.PERSIST)
public Location getFinish() {
return finish;
}
public void setFinish(Location finish) {
this.finish = finish;
}
}
地點:
@Entity
public class Location {
@Id
@GeneratedValue
private Long id;
private double x;
private double y;
public Location() {
}
public Location(double x, double y) {
this.x = x;
this.y = y;
}
@XmlTransient
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public boolean equals(Object o) {
if ((o instanceof Location)
&& (((Location)o).getX() == this.x)
&& (((Location)o).getY() == this.y))
{
return true;
}
else return false;
}
}
什麼是「開始」變量?你爲什麼不在第二個例子中調用'getX()'方法? – fracz
「開始」是從HTTP POST數據收到的位置類實例...例如。與Location start = new Location(「1.0」,「1.0」)相同; ...我沒有使用getx(),因爲我想能夠測試複雜對象的相等性...... –