gpt4 book ai didi

postgresql - javax.persistence.NoResultException : No entity found for query JPQL Query

转载 作者:行者123 更新时间:2023-11-29 14:05:03 29 4
gpt4 key购买 nike

@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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com