openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kevin Sutter <kwsut...@gmail.com>
Subject Re: [NEWBIE] OpenJPA Cannot get id field returned
Date Tue, 08 Sep 2009 22:14:07 GMT
Here's another idea...  When did these entities get persisted to the
database?  And, are they still part of the persistence context that is being
used for the query?  Generated identity fields are not available in the
Entity objects until they are retrieved from the database.  But, if the
Entities were already part of the persistence context when the query was
performed, then we might be pulling these Entity instances from the
persistence context and not going to the database.  This would more likely
happen with application-managed persistence contexts since they are not
automatically cleared when a transaction completes.

If this scenario sounds similar to yours, you could force the cleaning of
the persistence context by doing an EM.clear() prior to your query
invocation.  This will remove all Entity instances from the persistence
context and force us to go to the database.

Another potential source of confusion is the DataCache and/or QueryCache.
These are not turned on by default though, so let's start with the simple
Persistence Context L1 cache first...

Kevin

On Tue, Sep 8, 2009 at 1:37 PM, C N Davies <cnd@cndavies.com> wrote:

> Yes I just turn on trace logging and then test the query in my SQL tool to
> work our what the issue is.
>
> -----Original Message-----
> From: ashish paliwal [mailto:ashishpaliwal.bit@gmail.com]
> Sent: Wednesday, 9 September 2009 2:25 AM
> To: users@openjpa.apache.org; cnd@cndavies.com
> Subject: Re: [NEWBIE] OpenJPA Cannot get id field returned
>
> Yes, id I too believe should be without quotes. In any case the query
> generated should provide with sufficient information about what is going
> wrong.
>
> On Tue, Sep 8, 2009 at 11:16 PM, C N Davies <cnd@cndavies.com> wrote:
>
> > Seems like id is an int and your quoting it, so that shouldn't work.
> >
> >
> > > Try turning on SQL trace to see what SQL is being generated from your
> > > JPQL.
> > >
> > > Rick
> > >
> > >
> > >
> > > On Sep 8, 2009, at 3:54 AM, "B.L. Zeebub"
> > > <roger.varley@googlemail.com> wrote:
> > >
> > > >
> > > >
> > > >
> > > > Jean-Baptiste BRIAUD -- Novlog wrote:
> > > >>
> > > >> Did you try GenerationType.IDENTITY ?
> > > >> @GeneratedValue(strategy = GenerationType.IDENTITY)
> > > >>
> > > >> I think it should work with int but I prefer long instead.
> > > >>
> > > >
> > > > GenerationType.IDENTITY appears to make no difference. However,
> > > > suprisingly
> > > > (to me anyway)
> > > >
> > > > @Test
> > > >    public void getPlatformById() {
> > > >        Platform platform = (Platform) em.find(Platform.class, 8);
> > > >        assertNotNull(platform);
> > > > }
> > > >
> > > > works and returns the correct platform entity although platform.getId
> > > > ()
> > > > still returns 0.
> > > >
> > > > @Test
> > > >    public void getPlatformByQueryById() {
> > > >        String jpql = "Select c from Platform c where c.id = '8'";
> > > >        Platform platform = (Platform) em.createQuery
> > > > (jpql).getSingleResult();
> > > >        assertNotNull(platform);
> > > > }
> > > >
> > > > This fails with a no instance found exception from OpenJPA
> > > >
> > > > Regards
> > > >
> > > > On Sep 8, 2009, at 10:56 , B.L. Zeebub wrote:
> > > >
> > > >>
> > > >> Hi
> > > >>
> > > >> I have attached my entity definition below. This was created over
a
> > > >> pre-existing database. I create the EntityManagerFactory to obtain
> > > >> the
> > > >> entitymanager (em) as normal and execute the code;
> > > >>
> > > >> String jpql = "select c from Platform c";
> > > >>        em.getTransaction().begin();
> > > >>        entities = (List) em.createQuery(jpql).getResultList();
> > > >>        em.getTransaction().commit();
> > > >>
> > > >> I get the list of Platform entities as expected. Looping through the
> > > >> list,
> > > >> getPlatformName() returns the text as expected, but getId() always
> > > >> returns 0
> > > >> (zero) for all members of the list. Any ideas as to what I've
> missed.
> > > >>
> > > >> Regards
> > > >>
> > > >> @Entity
> > > >> @Table(name="Platform")
> > > >> public class Platform implements Serializable {
> > > >>    private static final long serialVersionUID = 1L;
> > > >>
> > > >>    @Id
> > > >>    @GeneratedValue(strategy=GenerationType.AUTO)
> > > >>    @Column(name="id", unique=true, nullable=false)
> > > >>    private int id;
> > > >>
> > > >>    @Column(name="PlatformName", length=32)
> > > >>    private String platformName;
> > > >>
> > > >>    //bi-directional many-to-one association to PEcu
> > > >>    @OneToMany(mappedBy="platform")
> > > >>    private Set<PEcu> pecus;
> > > >>
> > > >>   public Platform() {
> > > >>   }
> > > >>
> > > >>    public int getId() {
> > > >>        //TODO: Remove
> > > >>        System.out.println("Platform.getId() called - " + this.id);
> > > >>        return this.id;
> > > >>    }
> > > >>
> > > >>    public void setId(int id) {
> > > >>        this.id = id;
> > > >>    }
> > > >>
> > > >>    public String getPlatformName() {
> > > >>        //TODO: Remove
> > > >>        System.out.println("Platform.getPlatform() called - " +
> > > >> this.platformName);
> > > >>        return this.platformName;
> > > >>    }
> > > >>
> > > >>    public void setPlatformName(String platformName) {
> > > >>        this.platformName = platformName;
> > > >>    }
> > > >>
> > > >>    public Set<PEcu> getPecus() {
> > > >>        return this.pecus;
> > > >>    }
> > > >>
> > > >>    public void setPecus(Set<PEcu> pecus) {
> > > >>        this.pecus = pecus;
> > > >>    }
> > > >>
> > > >> }
> > > >> --
> > > >> View this message in context:
> > > >>
> >
>
> http://n2.nabble.com/NEWBIE-OpenJPA-Cannot-get-id-field-returned-tp3601995p3
> 601995.html<http://n2.nabble.com/NEWBIE-OpenJPA-Cannot-get-id-field-returned-tp3601995p3%0A601995.html>
> > > >> Sent from the OpenJPA Users mailing list archive at Nabble.com.
> > > >>
> > > >
> > > >
> > > >
> > > >
> > > > --
> > > > View this message in context:
> >
>
> http://n2.nabble.com/NEWBIE-OpenJPA-Cannot-get-id-field-returned-tp3601995p3
> 602236.html<http://n2.nabble.com/NEWBIE-OpenJPA-Cannot-get-id-field-returned-tp3601995p3%0A602236.html>
> > > > Sent from the OpenJPA Users mailing list archive at Nabble.com.
> > >
> >
> >
>
>
> --
> Ashish Paliwal
> Mobile : 9305128367
> IIT Kanpur
> India
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message