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

  • Home
  • SEARCH
  • 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 1106053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:43:55+00:00 2026-05-17T01:43:55+00:00

I was reading an article in which the author had implemented an Entity class

  • 0

I was reading an article in which the author had implemented an Entity class like this:

@Entity
public class Product {

 @OneToMany
 private List<Part> parts; // note the missing "= new ArrayList<Part>();"

 public Product() {
 }

 // getters and setters

}

I always used to instantiate collection fields, in this case parts, either inline (private List<Part> parts = new ArrayList<Part>();) or inside the constructor because as far as I can remember not doing so would lead to all kinds of NPEs.

I thought that things have changed in JPA 2 and now the JPA runtime automatically instantiates the field using runtime bytecode enhancement or reflection, so I gave it another try, however I still can’t get it to work without instantiating the parts field, otherwise aProduct.getParts().add(aPart) would throw an NPE.

So my question is that is it possible to make this work without instantiating the parts field in both of Java SE and Java EE environments using Hibernate as the provider? If so, how?

  • 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-17T01:43:55+00:00Added an answer on May 17, 2026 at 1:43 am

    My understanding is that the JPA provider can only instantiate the field properly within an entity loaded from the DB. However, if you create a new (as yet transient) entity, you must ensure yourself that all fields are valid. Note that Hibernate/JPA is not aware of a newly created transient entity, until you actually attach it to a persistence context. And if you think about it, it is logical (at least to me): if you relied on JPA/Hibernate to instantiate your objects properly, you would build up a strong and intrusive implementation dependency to it, which would make it very difficult to impossible to ever work without it again.

    So leaving the collection property uninitialized may be fine for entities which are never created afresh, only loaded from the DB. For classes where you do create new instances too, the simplest way to resolve this would be to provide two constructors: a default for Hibernate/JPA, and a parametrized one for the creation “by hand”. In case you don’t have parameters to set, you could also create a static factory method to initialize all required fields with default values.

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

Sidebar

Related Questions

Reading an article called Increase LINQ Query Performance in July's MSDN magazine, the author
I was reading this article about Double-Checked locking and out of the main topic
I was reading an article on MSDN Magazine about using the Enumerable class in
I just finished reading this article on the advantages and disadvantages of exceptions and
I was reading through this article: http://aws.typepad.com/aws/2008/12/running-everything-on-aws-soocialcom.html And I was wondering if this was
After reading an article about the subject from O'Reilly , I wanted to ask
I was reading an article that shows how bad CodePlex uses UpdatePanels and how
I was reading an article on TheServerSide on ployglot programming on the Java platform
I was reading the great article about binding and unbinding events (because I am
I was reading the following article: http://msdn.microsoft.com/en-us/magazine/cc817398.aspx Solving 11 Likely Problems In Your Multithreaded

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.