gpt4 book ai didi

JPA 2 "member of"语法不适用于父类(super class)成员

转载 作者:行者123 更新时间:2023-12-02 02:28:14 25 4
gpt4 key购买 nike

基本上我有一个拒绝工作的命名查询“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 没有在 Server 中定义,而是在它的父类(super class) NetworkedDevice 中定义:

@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 实例的测试用例,并将其添加到服务器实例(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 是私有(private)的,但即使将其设置为 protected 也没有用。我们使用 eclipselink 2.2.0-M4 作为我们的 JPA 2-provider。这是 eclipselink 错误还是查询错误?

最佳答案

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

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

“member of”通常是旧的 JPQL 语法,现在通常使用“join”代替

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

关于JPA 2 "member of"语法不适用于父类(super class)成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4876093/

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