Regarding q2: em.merge() should do the trick. Also, IIRC, your case 1 should always fail; maybe we should make it throw instead since that's a pretty convoluted way to always return no results.
-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
