Query1(工作正常!!!):
em.createQuery(
"SELECT r FROM Route r WHERE r.start.x = :x"
, Route.class).setParameter("x", start.getX())
Query2(我真的很喜欢这个工作!):
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;
}
}