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 3349824
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:42:44+00:00 2026-05-18T01:42:44+00:00

I have a POJO class, say Foo, which has a Set of other entity

  • 0

I have a POJO class, say Foo, which has a Set of other entity instances, say bars.
Also there are standart misc classes for such project: service and dao for both Foo and Bar.

I want BarService to get the Set of Bar instances associated with some Foo. Now I have the following code, wich I believe is conceptually bad.

 
public class Foo {
    Set<Bar> bars;

    public Set<Bar> getBars() {
        if (bars == null)
            return ( bars = new HashSet() );
        return bars;
    }
}
 
public class BarServiceImpl {
    public List<Bar> getListOfBars(Foo foo) {
        return new ArrayList(foo.getBars());
    }
}

3 questions:
Where it is better to initialize Foo’s Set?
What specific Sets and Lists are better for such purposes?
What conceptual issues has my current implementation, and how to do better?

Thanks in advance.

  • 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-18T01:42:45+00:00Added an answer on May 18, 2026 at 1:42 am

    Where it is better to initialize Foo’s Set?

    Most of time, I initialize a collections when declaring it, which is what Hibernate recommends. Quoting the documentation:

    6.1. Persistent collections

    Hibernate requires that persistent
    collection-valued fields be declared
    as an interface type. For example:

    public class Product {
        private String serialNumber;
        private Set parts = new HashSet();
    
        public Set getParts() { return parts; }
        void setParts(Set parts) { this.parts = parts; }
        public String getSerialNumber() { return serialNumber; }
        void setSerialNumber(String sn) { serialNumber = sn; }
    }
    

    The actual interface might be
    java.util.Set,
    java.util.Collection,
    java.util.List, java.util.Map,
    java.util.SortedSet,
    java.util.SortedMap or anything you
    like (“anything you like” means you
    will have to write an implementation
    of
    org.hibernate.usertype.UserCollectionType.)

    Notice how the instance variable was
    initialized with an instance of
    HashSet. This is the best way to
    initialize collection valued
    properties of newly instantiated
    (non-persistent) instances. When you
    make the instance persistent, by
    calling persist() for example,
    Hibernate will actually replace the
    HashSet with an instance of
    Hibernate’s own implementation of
    Set.

    If leaving it null is part of your business, my suggestion would be to initialize it in a (common) link management methods:

    public class Foo {
        ...
        private Set<Bar> bars;
        ...
        public void addBar(Bar bar) {
            if (this.bars == null) {
                this.bars = new HashSet<Bar>();
            }
            this.bars.add(bar);
        }
    }
    

    What specific Sets and Lists are better for such purposes?

    It all depends on the semantics you need. A Set doesn’t allow duplicates, a List allows duplicates and introduces positional indexing.

    What conceptual issues has my current implementation, and how to do better?

    1. I wouldn’t perform an assignment in the getter.
      • If a collection is supposed to be null at that point, let it be null.
    2. I don’t see the added value of your service
      • why not just calling foo.getBars()?
      • why converting the collection?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a simple POJO class: public void Foo { public int
I have a hibernate pojo class and it has some field. Assume that there
Let's say I have following POJO's using Hibernate. public class User { private String
I have one Pojo class in which I create one field which is not
I have a Java class which represents the correlation between two elements (typical POJO):
Let's say we have two tables, ORDERS and OFFERS Order POJO @Entity @Table(name =
I have a pojo class class Foo{ String a String b . . //
I have developed below which makes the use of comparator.. this my pojo.. class
I have a Product class which has a 1-to-many association to itself (composite pattern).
I have simple POJO say User Object; public class User{ private String userid; private

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.