Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Load only part of object graph
Load only part of object graph [message #676257] Fri, 03 June 2011 12:41 Go to next message
Matti Hansson is currently offline Matti HanssonFriend
Messages: 68
Registered: July 2009
Member
Hi!
Is there a way to make a database query that only loads part of an object's graph?

For example, I have three classes with class A holding references to B and C like so:

@Entity
public class A {
  @Id
  @Column(name="OBJECTID)
  private int id;

  @Column(name="VALUE")
  private String value;

  @OneToMany(mappedBy="refToA")
  private List<B> bees;

  @OneToMany(mappedBy="refToA")
  private List<C> seas;

  // getters & setters...
}

@Entity
public class B {
  @Id
  @Column(name="OBJECTID)
  private int id;

  @Column(name="VALUE")
  private String value;

  @ManyToOne
  @JoinColumn(name="FKEY", referencedColumnName="OBJECTID", insertable=false, updateable=false)
  private A refToA;

  // getters & setters...
}

@Entity
public class C {
  @Id
  @Column(name="OBJECTID)
  private int id;

  @Column(name="VALUE")
  private String value;

  @ManyToOne
  @JoinColumn(name="FKEY", referencedColumnName="OBJECTID", insertable=false, updateable=false)
  private A refToA;

  // getters & setters...
}


So, what I would like to do is make a query that fetches an instance of A and its bees, but not its seas. Is this possible? Oh, and lazy loading probably won't work since I want instanceOfA.getSeas() to return null or an empty list, even if instanceOfA actually does have a few seas in the database.
Thanks!
/Matti
Re: Load only part of object graph [message #676843 is a reply to message #676257] Mon, 06 June 2011 17:35 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

EclipseLink supports fetch-groups, which allows you to only fetch a specific set of attributes. Fetch groups can be set through query hint and the FetchGroup class.

But, fetch groups basically leave the uninstantiated attributes as LAZY, so may not be what you want.

You could just copy your objects, and null out the attributes that you do not want. EclipseLink also supports a copy() API on its JpaEntityManager that allows any entity to be copied with a CopyGroup of the desired attributes.

EclipseLink also support partial object reading, which leaves not fetch attributes as null, but this is being replaced by fetch groups.




James : Wiki : Book : Blog : Twitter
Previous Topic:automatic schema update
Next Topic:Criteria API and grouping by a truncated date
Goto Forum:
  


Current Time: Sun Dec 21 21:42:43 GMT 2014

Powered by FUDForum. Page generated in 0.08664 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software