作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
@Entity
@Table(name = "ways", schema = "public", uniqueConstraints = @UniqueConstraint(columnNames = "gid"))
public class Ways implements java.io.Serializable {
private WaysId id;
public Ways() {
}
public Ways(WaysId id) {
this.id = id;
}
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "gid", column = @Column(name = "gid", unique = true)),
@AttributeOverride(name = "classId", column = @Column(name = "class_id", nullable = false)),
@AttributeOverride(name = "length", column = @Column(name = "length", precision = 17, scale = 17)),
@AttributeOverride(name = "name", column = @Column(name = "name")),
@AttributeOverride(name = "x1", column = @Column(name = "x1", precision = 17, scale = 17)),
@AttributeOverride(name = "y1", column = @Column(name = "y1", precision = 17, scale = 17)),
@AttributeOverride(name = "x2", column = @Column(name = "x2", precision = 17, scale = 17)),
@AttributeOverride(name = "y2", column = @Column(name = "y2", precision = 17, scale = 17)),
@AttributeOverride(name = "reverseCost", column = @Column(name = "reverse_cost", precision = 17, scale = 17)),
@AttributeOverride(name = "rule", column = @Column(name = "rule")),
@AttributeOverride(name = "toCost", column = @Column(name = "to_cost", precision = 17, scale = 17)),
@AttributeOverride(name = "maxspeedForward", column = @Column(name = "maxspeed_forward")),
@AttributeOverride(name = "maxspeedBackward", column = @Column(name = "maxspeed_backward")),
@AttributeOverride(name = "osmId", column = @Column(name = "osm_id")),
@AttributeOverride(name = "priority", column = @Column(name = "priority", precision = 17, scale = 17)),
@AttributeOverride(name = "theGeom", column = @Column(name = "the_geom")),
@AttributeOverride(name = "source", column = @Column(name = "source")),
@AttributeOverride(name = "target", column = @Column(name = "target"))})
//@AttributeOverride(name = "vitesse", column = @Column(name = "vitesse", precision = 17, scale = 17))})
public WaysId getId() {
return this.id;
}
public void setId(WaysId id) {
this.id = id;
}
}
@Embeddable
public class WaysId implements java.io.Serializable {
private Integer gid;
private int classId;
private Double length;
private String name;
private Double x1;
private Double y1;
private Double x2;
private Double y2;
private Double reverseCost;
private String rule;
private Double toCost;
private Integer maxspeedForward;
private Integer maxspeedBackward;
private Long osmId;
private Double priority;
private Serializable theGeom;
private Integer source;
private Integer target;
}
public double convertidTolat1(Integer id) {
Double lat = null;
Query query = entityManager
.createQuery("select n.id.x1 from Ways n where n.id.gid=:id");
query.setParameter("id", id);
lat = (Double) query.getSingleResult();
return lat;
}
@GET
@Path("lat/{id}")
@Produces(MediaType.APPLICATION_JSON)
public BigDecimal getlat(@PathParam(value = "id") long id) {
BigDecimal d = iservices.getLat(id);
return d;
}
大家好,这是我的代码,我想知道我的查询有什么问题?
最佳答案
您的查询没有问题。可嵌入对象的路径表达式在 JPA 中完全有效。你只是没有得到任何结果。NoResultException
的 Javadoc 描述性很强。
Thrown by the persistence provider when Query.getSingleResult() or TypedQuery.getSingleResult() is executed on a query and there is no result to return. This exception will not cause the current transaction, if one is active, to be marked for rollback.
我建议将 getSingleResult() 包装在 try-catch block 中以处理无结果情况
Object result = null;
try {
result = query.getSingleResult();
} catch (NoResultException e) {
log.debug("No result forund for... ");
}
或者总是使用 getResultList(),即
List results = query.getResultList();
if (results.isEmpty()) {
return null; // handle no-results case
} else {
return results.get(0);
}
如果您只期望一个结果,有时甚至可以更进一步检查数据库完整性。
if (results.size() > 1) {
log.warn("Found duplicated result for...");
}
关于postgresql - javax.persistence.NoResultException : No entity found for query JPQL Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25616374/
我是一名优秀的程序员,十分优秀!