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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:17:31+00:00 2026-05-19T14:17:31+00:00

this question is similar to this other question I’ve asked, but slightly different. I

  • 0

this question is similar to this other question I’ve asked, but slightly different.

I have this:

class A{

  private List<B> bs;

  ...
}

class B{

  private Long id;
  private C c;
  ...
} 

class C{

  private Long id;
  private String name;
  ...
} 

And I’d like to have this:

class A{

  // the map should have b.c.name as key
  private Map<String,B> bs;

  ...
}

class B{

  private Long id;
  private C c;
  private A a;
  ...
} 

class C{

  private Long id;
  private String name;
  ...
} 

I don’t know if it is clear what I’d like to do, but it is as simple as mapping a one to many relationship to a Map with the name of C as the key of the map and b as the value.

Thanks in advance,
Neuquino

  • 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-19T14:17:32+00:00Added an answer on May 19, 2026 at 2:17 pm

    Short answer is you can’t do this.

    But a here is solution that may be suitable for your problem:

    In entity A still defined relation to entity B as List (or even better as Set, so that the same B cannot be contained more than once).

    @OneToMany(mappedBy="a")
    private Set<B> bs;
    

    As you don’t want to expose the plain list, omit getter and setter for as.

    Then you can define a getter for you map that builds the map on the fly:

    // a transient field to cache the map
    private transient Map<String, B> bsMappedByCName;
    
    public Map<String, B> getBsMappedByCName() {
      if(bsMappedByCName == null) {
        bsMappedByCName = new HashMap<String, B>();
        for(B b : bs) {
          mapB(b);
        }
      }
      // return as unmodifiable map so that it is immutable for clients
      return Collections.unmodifiableMap(bsMappedByCName);
    }
    
    private void mapB(B b) {
      // we assume here that field c in class B and likely also field name in class C are not nullable. Further more both of this fields sould be immutable (i.e. have no setter).
      if(bsMappedByCName.put(b.getC().getName(), b) != null) {
        // multiple bs with same CName, this is an inconsistency you may handle 
      }      
    }
    

    The last issue to address is how do we add a new B to A or remove one. With the strategy to return the map as unmodifiable, we must provide some add and remover methods in class A:

    public void addB(B b) {
      bs.add(b);
      mapB(b);
    }
    
    public void removeB(B b) {
      bs.remove(b);
      bsMappedByCName.remove(b.getC().getName());
    }
    

    An other option is to replace return Collections.unmodifiableMap(...) with (inspired from ObservaleCollection from apache):

    return new Map<String, B>() {
      // implement all methods that add or remove elements in map with something like this
      public B put(String name, B b) {
        // check consistency
        if(!b.getC().getName().equals(name)) { 
          // this sould be handled as an error
        }
        B oldB = get(name);
        mapB(b);
        return oldB;
      }
    
      // implement all accessor methods like this
      public B get(String name) {
        return bsMappedByCName.get(name);
      }
    
      // and so on...
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question sounds similar to many posed here, but it's obnoxiously different. I have
Ok, I have previously asked a similar question to this but it was voted
This question is similar to this other post , but I'm new to iPhone
This question seems similar to those asked in a few other posts: here and
My question somewhat overlaps with this and several other similar ones. Those have some
This question is similar to the one i asked here . But its related
This question might be a bit long and specific, but I have been attempting
I've asked a question similar to this but thought I would ask a more
I am posting this question after reading other similar questions about my problem but
This question is similar to this other one , with the difference that the

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.