查询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(),因为我想能够测试复杂对象的相等性...... –