JPA 2“语法成员”不适用于超类的成员

时间:2011-02-02 15:12:41

标签: jpa eclipselink jpa-2.0 jpql

基本上我有一个命名查询“findServerWithNic”拒绝工作:

@Entity
@Table(name = "vnm_server")
@DiscriminatorValue("S")
@NamedQueries({
    @NamedQuery(
        name="findServerWithNic",
        query="SELECT s FROM Server s, Nic n "+
                "WHERE n.id = :nicId AND n member of s.nics"
    )
})
public class Server extends NetworkedDevice implements Serializable
{...}

nics未在服务器中定义,但在其超类NetedDevice中定义:

@Entity
@Table(name = "vnm_networked_device")
@DiscriminatorColumn(name = "c_device_type", discriminatorType = DiscriminatorType.CHAR)
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class NetworkedDevice extends AbstractIdentifyable implements Serializable
{
    @OneToMany(mappedBy = "connectedHost", cascade = CascadeType.ALL)
    @OrderColumn(name = "c_index")
    protected List<Nic> nics = new ArrayList<Nic>();

    public List<Nic> getNics() {
        return nics;
    }

    public void setNics(List<Nic> nics) {
        this.nics = nics;
    }
}

现在我有一个测试用例创建一个Nic实例,然后将它添加到Server实例(s.getNics()包含实例,我已经检查过了)但调用了查询

public Server findVirtualServerOfNic(Long nicId) {        
    final Server network = em.createNamedQuery("findServerWithNic", Server.class)
                                .setParameter("nicId", nicId)
                                    .getSingleResult();
    return network;
}

导致NoResultException

Caused by: javax.persistence.NoResultException: getSingleResult() did not retrieve any entities.
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.throwNoResultException(EJBQueryImpl.java:1246)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getSingleResult(EJBQueryImpl.java:750)
    at com.profitbricks.provisioning.vnm.jpa.impl.VNManagementJPAImpl.findVirtualServerOfNic(VNManagementJPAImpl.java:101)

最初nics-member是私有的,但即使将其设置为受保护也无效。 我们使用eclipselink 2.2.0-M4作为我们的JPA 2提供商。这是一个eclipselink错误还是查询错误?

1 个答案:

答案 0 :(得分:2)

您是否已提交或清除交易?如果没有,那么数据库可能没有值(取决于您的刷新模式),因此您的查询不会返回任何内容。

启用日志记录并包含SQL。如果SQL正确吗?

“成员”通常是旧的JPQL语法,现在通常使用“join”来代替

“SELECT s FROM Server s join s.mics n WHERE n.id =:nicId”