You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Alejandro Abdelnur <tu...@gmail.com> on 2016/02/25 05:49:42 UTC

getting ID only of OneToOne and ManyToOne relationships

Hello,

I've spent quite a bit of time searching around without any luck.

With OpenJPA 2.x, is it possible to get the foreign key of a OneToOne
relationship in the source without loading the target entity?

For example:

public class A {
  @Id
  private String id;
  private String name;
 @OneToOne(fetch = FetchType.LAZY)
  private B b;
  ...getters/setters
}

public class B {
  @Id
  private String id;
  private String name;
  ...getters/setters
}

Even if 'A a = em.find(A.class, "1")' loads the columns 'ID, NAME, B_ID'
from A if I do 'a.getB().getId()' this triggers a new query on B to get ID.

The closest related thing I found is
http://stackoverflow.com/questions/2593722/hibernate-one-to-one-getid-without-fetching-entire-object
, but this does not seem to apply to OpenJpa.

Similarly for ManyToOne relationships?

Thanks in advance.

Alejandro

Re: getting ID only of OneToOne and ManyToOne relationships

Posted by Alejandro Abdelnur <tu...@gmail.com>.
Albert, thanks for the tip on FetchGroups. I will definitely use it, but it
does not solve my use case.

Following is a simplistic use case:

---------------------------------------------------------
ENTITIES:

@Entity
public class X {
  @Id
  private String id;
  private String name;
  @OneToOne(fetch = FetchType.LAZY)
  @LoadFetchGroup("idOnly")
  private Y y;
  ...setters/getters
}

@Entity
@FetchGroup(name = "idOnly", attributes = {@FetchAttribute(name = "id")})
public class Y {
  @Id
  private String id;
  private String name;
  @OneToOne(fetch = FetchType.LAZY)
  private X x;
  ...setters/getters
}

---------------------------------------------------------
Vanilla FIND:

X x = em.find(X.class, "x1");
x.getY();

triggers:

SELECT t0.name, t0.Y_ID FROM X t0 WHERE t0.id = ? [params=?]
SELECT t0.name, t0.X_ID FROM Y t0 WHERE t0.id = ? [params=?]

---------------------------------------------------------
FetchGroups FIND:

OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
X x = oem.find(X.class, "x1");
oem.getFetchPlan().removeFetchGroup("default");
x.getY();

SELECT t0.name, t0.Y_ID FROM X t0 WHERE t0.id = ? [params=?]
SELECT t0.id FROM Y t0 WHERE t0.id = ? [params=?]

---------------------------------------------------------
What I hoping was to have one SELECT only, the first one, as Y's ID is
already there:

SELECT t0.name, t0.Y_ID FROM X t0 WHERE t0.id = ? [params=?]
---------------------------------------------------------

Thanks.

On Thu, Feb 25, 2016 at 9:51 AM, Albert Lee <al...@gmail.com> wrote:

> Take a look in OpenJPA's FetchGroup construct to see if this meets your
> needs.
>
>
> http://openjpa.apache.org/builds/2.4.0/apache-openjpa/docs/manual.html#ref_guide_fetch
>
> Albert
>
> On Wed, Feb 24, 2016 at 10:49 PM, Alejandro Abdelnur <tu...@gmail.com>
> wrote:
>
> > Hello,
> >
> > I've spent quite a bit of time searching around without any luck.
> >
> > With OpenJPA 2.x, is it possible to get the foreign key of a OneToOne
> > relationship in the source without loading the target entity?
> >
> > For example:
> >
> > public class A {
> >   @Id
> >   private String id;
> >   private String name;
> >  @OneToOne(fetch = FetchType.LAZY)
> >   private B b;
> >   ...getters/setters
> > }
> >
> > public class B {
> >   @Id
> >   private String id;
> >   private String name;
> >   ...getters/setters
> > }
> >
> > Even if 'A a = em.find(A.class, "1")' loads the columns 'ID, NAME, B_ID'
> > from A if I do 'a.getB().getId()' this triggers a new query on B to get
> ID.
> >
> > The closest related thing I found is
> >
> >
> http://stackoverflow.com/questions/2593722/hibernate-one-to-one-getid-without-fetching-entire-object
> > , but this does not seem to apply to OpenJpa.
> >
> > Similarly for ManyToOne relationships?
> >
> > Thanks in advance.
> >
> > Alejandro
> >
>
>
>
> --
> Albert Lee.
>

Re: getting ID only of OneToOne and ManyToOne relationships

Posted by Albert Lee <al...@gmail.com>.
Take a look in OpenJPA's FetchGroup construct to see if this meets your
needs.

http://openjpa.apache.org/builds/2.4.0/apache-openjpa/docs/manual.html#ref_guide_fetch

Albert

On Wed, Feb 24, 2016 at 10:49 PM, Alejandro Abdelnur <tu...@gmail.com>
wrote:

> Hello,
>
> I've spent quite a bit of time searching around without any luck.
>
> With OpenJPA 2.x, is it possible to get the foreign key of a OneToOne
> relationship in the source without loading the target entity?
>
> For example:
>
> public class A {
>   @Id
>   private String id;
>   private String name;
>  @OneToOne(fetch = FetchType.LAZY)
>   private B b;
>   ...getters/setters
> }
>
> public class B {
>   @Id
>   private String id;
>   private String name;
>   ...getters/setters
> }
>
> Even if 'A a = em.find(A.class, "1")' loads the columns 'ID, NAME, B_ID'
> from A if I do 'a.getB().getId()' this triggers a new query on B to get ID.
>
> The closest related thing I found is
>
> http://stackoverflow.com/questions/2593722/hibernate-one-to-one-getid-without-fetching-entire-object
> , but this does not seem to apply to OpenJpa.
>
> Similarly for ManyToOne relationships?
>
> Thanks in advance.
>
> Alejandro
>



-- 
Albert Lee.

Re: getting ID only of OneToOne and ManyToOne relationships

Posted by ma...@live.de.

Hey,


I'm not an OpenJPA guy, but this is expected behavior.


You are using FetchType.LAZY which tells the JPA implementation to not read the relation immediate.


You can change this to EAGER and it will get loaded with parent.


The current behavior is as specified in the JPA spec.


Best,


Markus


.:: YAGNI likes a DRY KISS ::.






On Wed, Feb 24, 2016 at 8:50 PM -0800, "Alejandro Abdelnur" <tu...@gmail.com> wrote:





Hello,

I've spent quite a bit of time searching around without any luck.

With OpenJPA 2.x, is it possible to get the foreign key of a OneToOne
relationship in the source without loading the target entity?

For example:

public class A {
  @Id
  private String id;
  private String name;
 @OneToOne(fetch = FetchType.LAZY)
  private B b;
  ...getters/setters
}

public class B {
  @Id
  private String id;
  private String name;
  ...getters/setters
}

Even if 'A a = em.find(A.class, "1")' loads the columns 'ID, NAME, B_ID'
from A if I do 'a.getB().getId()' this triggers a new query on B to get ID.

The closest related thing I found is
http://stackoverflow.com/questions/2593722/hibernate-one-to-one-getid-without-fetching-entire-object
, but this does not seem to apply to OpenJpa.

Similarly for ManyToOne relationships?

Thanks in advance.

Alejandro