Topic
2 replies Latest Post - ‏2010-12-06T18:52:47Z by MANT
MANT
MANT
10 Posts
ACCEPTED ANSWER

Pinned topic OpenJPA - Lazy fetching in case of ManyToOne

‏2010-12-02T21:54:15Z |
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")
Updated on 2010-12-06T18:52:47Z at 2010-12-06T18:52:47Z by MANT
  • sutter
    sutter
    94 Posts
    ACCEPTED ANSWER

    Re: OpenJPA - Lazy fetching in case of ManyToOne

    ‏2010-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...

    HTH,
    Kevin
    • MANT
      MANT
      10 Posts
      ACCEPTED ANSWER

      Re: OpenJPA - Lazy fetching in case of ManyToOne

      ‏2010-12-06T18:52:47Z  in response to sutter
      Thanks 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?