I understand that Hibernet by default has lazy fetching enabled for all many to one relationships, but in JPA that i optional. would it improve the overall performance if we by default mention below for all the ManyToOne relationships?
How do we decide if it should be Lazy or eager, which one is safe and performance effective?
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "column_name")
This topic has been locked.
2 replies Latest Post - 2010-12-06T18:52:47Z by MANT
Pinned topic OpenJPA - Lazy fetching in case of ManyToOne
Answered question This question has been answered.
Unanswered question This question has not been answered yet.
Updated on 2010-12-06T18:52:47Z at 2010-12-06T18:52:47Z by MANT
sutter 120000EB4G94 PostsACCEPTED ANSWER
Re: OpenJPA - Lazy fetching in case of ManyToOne2010-12-02T23:09:40Z in response to MANT> I understand that Hibernet by default has lazy fetching enabled for all many to one relationships, but in JPA that i optional. would it improve the overall performance if we by default mention below for all the ManyToOne relationships?
> How do we decide if it should be Lazy or eager, which one is safe and performance effective?
> @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
> @JoinColumn(name = "column_name")
I'm not sure about Hibernate's defaults, but the JPA specification indicates that single valued relationships are supposed to loaded eagerly. OpenJPA adheres to the spec and, by default, will load the ManyToOne single valued relationship eagerly. As you point out, you can override this and specify LAZY.
FYI, collection valued relationships (ie. OneToMany) will default to LAZY loading. But, since LAZY loading is only a hint to the provider (per the spec), it's up to the provider to honor the LAZY request. OpenJPA does support LAZY loading and, thus, the collection valued relationships will default to LAZY loading.
Analyzing your usage of the Object model (Entities) and specifying the LAZY vs EAGER loading attributes based on your app's usage can greatly affect the performance. It all depends on the app's usage of the attributes. For example, if your Entity has a large number of attributes, but you only access one or two fields from the Entity, then maybe these unused attributes should be marked LAZY. If a LAZY attribute is then touched, it will just require a database access to get the data. On the other hand, if a collection-based relationship is used every time the owning Entity is touched, then changing the Fetch type to EAGER will save an extra trip to the database. Database trips are expensive. That's why the use of the L2 cache is so beneficial...
MANT 120000EFS710 PostsACCEPTED ANSWER
Re: OpenJPA - Lazy fetching in case of ManyToOne2010-12-06T18:52:47Z in response to sutterThanks Kevin for the inputs,
recently what we are seeing is, for a single user test, for 1 iteration which is about 12 screens, the application is making 3000 round trips from app server to DB server and basically there are those many SQL calls getting executed. When we look into the code we for sure dont have those many calls, we may have somewhere around 600 to 800 calls..we are analyzing what we are seeing certain queries (same exact queries) getting executed 50, 60 times etc. We are a little clueless at this point in time so kind of looking in to the code as much as we can to find out if some developers might have left their footprints in the code :-) although they are not with the company anymore..but have you heard something like this, that same queries are getting executed multiple times?