Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 876107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:22:48+00:00 2026-05-15T11:22:48+00:00

I am new to Hibernate and JPA. I wrote some functions, initially, I set

  • 0

I am new to Hibernate and JPA.

I wrote some functions, initially, I set fetch = FetchType.LAZY in the entity class. But it gave me error:

"org.hibernate.LazyInitializationException: could not initialize proxy – no Session"

@OneToMany(cascade = CascadeType.ALL, mappedBy = "logins", fetch=FetchType.LAZY,targetEntity=Invoice.class)
public List<Invoice> getInvoiceList() {
    return invoiceList;
}

public void setInvoiceList(List<Invoice> invoiceList) {
    this.invoiceList = invoiceList;
}

Then I changed it to fetch = FetchType.EAGER, and it worked fine.

I am wondering what happen if I don’t declare FetchType, does Hibernate determine itself which method to use? Or is it defaulted by EAGER?

@OneToMany(cascade = CascadeType.ALL, mappedBy = "logins", fetch=FetchType.EAGER,targetEntity=Invoice.class)
public List<Invoice> getInvoiceList() {
    return invoiceList;
}

public void setInvoiceList(List<Invoice> invoiceList) {
    this.invoiceList = invoiceList;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:22:49+00:00Added an answer on May 15, 2026 at 11:22 am

    I am wondering what happens if I don’t declare FetchType, does Hibernate determine itself which method to use? Or is it defaulted by EAGER??

    Actually, this behavior is not Hibernate specific but defined by the JPA specification and you’d find the answer in the spec or in the javadoc of the OneToMany annotation or the sources. From the sources:

    /** (Optional) Whether the association should be
     * lazily loaded or must be eagerly fetched. The
     * {@link FetchType#EAGER EAGER} strategy is a 
     * requirement on the persistenceprovider runtime 
     * that the associatedentities must be eagerly fetched. 
     * The {@link FetchType#LAZY LAZY} strategy is a hint 
     * to the persistence provider runtime.
     */
    FetchType fetch() default LAZY;
    

    That being said, while there are very legitimate use cases for FetchType.EAGER, using EAGER just to avoid the LazyInitializationException (which occurs when you try to load a lazy association on a detached object) is more a work around than a real solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I persist new created entity by JPA-Hibernate, some setter methods are called. Why?
im new in hibernate and JPA and i have some problems with annotations. My
I'm new to this hibernate/jpa stuff so bear with me.. I've generated an entity
I'm fairly new to Hibernate. I wrote a program with Hibernate and JPA. The
I am new to Hibernate world. It may be a silly question, but I
I'm terribly new to Hibernate. I've googled for two hours but I still can't
I am new to Hibernate/JPA and I am stuck at what I think is
I am new to JPA/Hibernate. Currently using EJB3, Hibernate/JPA. I have an inheritacnce structure
I'm have a lot of time using hibernate (JPA) in java, but i recently
I have following Hibernate 3 mapping for User class: <class name=org.test.User table=users> ... some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.