javax.persistence.NoResultException:找不到查询JPQL查询的实体

时间:2014-09-02 05:21:58

标签: postgresql rest java-ee jpa jboss

@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;
}

大家好,这是我的代码,我想知道我的查询有什么问题?

3 个答案:

答案 0 :(得分:6)

您的查询没有任何问题。 Embeddables的路径表达式在JPA中完全有效。你只是没有得到任何结果。 NoResultException的Javadoc非常具有描述性。

  

在查询上执行Query.getSingleResult()或TypedQuery.getSingleResult()并且没有返回结果时,由持久性提供程序抛出。此异常不会导致当前事务(如果一个处于活动状态)标记为回滚。

我建议将trySingleResult()包装在try-catch块中以处理无结果的情况

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...");
}

答案 1 :(得分:0)

我更喜欢这样做

<ControlTemplate x:Key="BT_Kids" TargetType="{x:Type Button}">
    <Grid>
        <Ellipse HorizontalAlignment="Left" Height="44.8" Stroke="Black" VerticalAlignment="Top" Width="71.2">
            <Ellipse.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0"/>
                    <GradientStop Color="#FF4363EA" Offset="1"/>
                </LinearGradientBrush>
            </Ellipse.Fill>
        </Ellipse>
        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</ControlTemplate>

<Button 
        HorizontalAlignment="Left" 
        VerticalAlignment="Top" 
        Width="75" Margin="189.6,24.6,0,0"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center"
        Template="{DynamicResource BT_Kids}" FontSize="30">OK</Button>

}

答案 2 :(得分:0)

我认为这个可以。它对我有用

public Optional<UserDO> findByUserId(String userId) {
    List<UserDO> userDOList = list(namedQuery("User.findByUserId").setParameter("theUserId", userId));
    return (userDOList.isEmpty()) ? Optional.empty() : Optional.of(userDOList.get(0));
}