Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » [JPA] (@OneToMany java.util.Map attribute causes all values to load on get(key))
[JPA] [message #701679] Mon, 25 July 2011 12:41 Go to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
We are using;
Glassfish 3.1
Eclipselink 2.2.0.v20110202-r8913
JDK jdk1.6.0_24
Windows Server 2008 SP2



We have a number of OneToMany mappings that use java.util.Maps

e.g.
@Entity
public class Singleton {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "singleton")
@MapKey(name = "cachedKey")
private java.util.Map<java.lang.String, MapItem> mapItems = new java.util.HashMap<java.lang.String, MapItem>();
}

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn
public class MapItem implements IMapItem {

@SuppressWarnings("unused") //used by JPA
@ManyToOne
private Singleton singleton;

@Basic
private String cachedKey;

@Id
private String id;

@Version
private int version;
}

The persistence.xml file is vanilla. It does not override any weaving hints. So I am assuming that we will get the maximum lazy-load possible ( this is what I see in the server logs ). In a debugger I see that a org.eclipse.persistence.indirection.IndirectMap is used to hold the "mapItems" map attribue of the class. The IndirectMap starts with a delegate of type org.eclipse.persistence.internal.indirection.UnitOfWorkQueryValueHolder. When I perform a .get(key) on "mapItems" this causes a query to load ALL of the MapItems referenced by the Singleton "mapItems".

If we have a large number of items in the map this obviously gives us a performance overhead. We know we can restructure our code so this does not happen ( i.e. model things differently ). However before we embark on this...

1) Is there any way to set up this OneToMany so that only one item ( or a reduced set ) is retrieved at a time?

2) Has anyone any advice or pattern that we can use to change the representation of this map? For example explicitly split the keys and values into different Sets that we can join/manage on our own?


Thanks
Re: [JPA] [message #701883 is a reply to message #701679] Mon, 25 July 2011 17:51 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

1 - No, all elements will always be loaded.

2 - You can define each element as a separate attribute, either by not having the Map or using VIRTUAL access. You could map each using a OneToOne mapping, possibly with a selectCriteria Expression.

Or you could remove the attributes entirely and only query for the desired objects.


James : Wiki : Book : Blog : Twitter
Previous Topic:Adding new column in an exsisting table
Next Topic:(no subject)
Goto Forum:
  


Current Time: Wed Apr 24 23:08:52 GMT 2024

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

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

Back to the top