Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA 2 "member of" syntax doesn't work with members of superclass

Basically i have a named query "findServerWithNic" which refuses to work:

@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 is not defined in Server but in it's superclass 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;
    }
}

now i have a testcase that creates a Nic instances a adds it to a Server instance (s.getNics() contains the instance, i checked) but the invocation of the query

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

results in an 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)

originally the nics-member was private but even setting it to protected didn't work. We use eclipselink 2.2.0-M4 as our JPA 2-provider. Is this an eclipselink bug or is the query wrong?

like image 245
user600148 Avatar asked Dec 31 '25 07:12

user600148


1 Answers

Did you commit or flush the transaction? If you did not, then the database may not have the value (depending on your flush mode), so your query will not return anything.

Enable logging and include the SQL. If the SQL correct?

"member of" is generaly old JPQL syntax, normally "join" is used now instead

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

like image 173
James Avatar answered Jan 06 '26 02:01

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!