Hi, Yes, they must be managed instances. When using a persistent type in a query, you are either doing object equality (in which case we must have a managed instance to understand how to handle object identity) or you are accessing persistent fields (in which case we must be hooked into the object in order to dig into it).
IIRC, this behavior is not defined in the spec -- I think the spec does not permit queries that directly compare entities. -Patrick On 9/17/07, frankca <[EMAIL PROTECTED]> wrote: > > Hi, > > I have simple query with Foo and FooType are persistent classes and name is > string type: > (1) @NamedQuery( > (2) name = "Foo.findByTypeAndName", > (3) query = "SELECT e " + > (4) "FROM Foo e " + > (5) "WHERE e.fooType = :fooType " + > (6) "AND e.name = :name" > (7) ) > > Case 1: if the given fooType is detached then if I create a named query and > invoke getSingleResult() returns null. > Case 2: if I change the named query line (5) as following and the given > fooType is detached then it works: > (5) "WHERE e.fooType.id = :fooTypeId " + > Case 3: if I "attach" the fooType prior to pass it to the query then it > works. > > Q1: Can someone please confirm is it true that any persistent entities pass > as parameters to JPQL queries MUST be attached? > > Q2: What is the best way to "attach" a persistent entity without knowing > it's detached or not? (I invoke EntityManager.contains() and perform > EntityManager.find() is this recommended?) > > TIA, > Frank > > -- > View this message in context: > http://www.nabble.com/Persistent-entities-pass-as-params-to-JPQL-queries-MUST-be-attached--Query.getSingleResult%28%29-bug-or-expected-behavior--tf4471259.html#a12748830 > Sent from the OpenJPA Users mailing list archive at Nabble.com. > > -- Patrick Linskey 202 669 5907
