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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:55:56+00:00 2026-05-19T06:55:56+00:00

An application I’m working on uses Hibernate exclusively to fetch a bunch of persistent

  • 0

An application I’m working on uses Hibernate exclusively to fetch a bunch of persistent objects from a database to memory. The application is to refresh this in-memory snapshot from the database every now and then, and that should be the only communication with the database.

The in-memory objects are then used for a bunch of calculations. The calculations must not modify these objects. Except some class somewhere accidentally did, and I had to spend a day hunting down the bug. Now I’m wondering what the best way to make the entire object tree immutable is.

Suppose the class hierarchy looks like this:

public class Building { // persistent entity
    private String name; // hibernate-mapped property
    private Set<Person> inhabitants; // hibernate-mapped collection

    // getters
}

public class Person { // persistent entity
    private String name; // hibernate-mapped property

    // getters
}

I’ve prevented clients from accessing the database by:

  • eagerly fetching all entities and collections
  • marking all entities and collections with mutable=false in Hibernate mappings
  • not providing any instances of the Hibernate session, or state-changing dao methods.

Now the error I’d like to prevent is, someone accidentally going building.getInhabitants().clear();. I can think of these options:

  1. Getter wrapping: Make getInhabitants first wrap inhabitants in a Collections.unmodifiableSet() call, then return it.

    • Pros: Least work, least extra code
    • Cons: Feels hacky
  2. Wrapper classes: Rename Building to MutableBuilding, Person to MutablePerson, and provide immutable classes Building and Person. Since my application has a clear snapshot point, I can fetch the records as mutable objects (as I do now), make deeply immutable copies and present that object tree to clients.

    • Pros: Straight java, no Hibernate magic. I get to use my favorite keyword: final
    • Cons: More code to write and maintain. Also, will Hibernate keep the mutable instances in memory?
  3. Hibernate mapping magic: Use that one magic keyword to instruct Hibernate to wrap the collections it sets on my entity objects in Collections.unmodifiableSet() or equivalent. (Note: I use an xml mapping file)

    • Pros: Elegant, no extra code
    • Cons: Such keyword may not exist
  4. Hibernate extension: Use that one Hibernate extension point to write my own object instantiator, and in there wrap the set in Collections.unmodifiableSet() before returning it.

    • Pros: More elegant than hacking my getters
    • Cons: Such extension point may not exist

Right now I’m leaning towards #2, mostly because I don’t know if 3 and 4 are possible.

What is the best way?

  • 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-19T06:55:58+00:00Added an answer on May 19, 2026 at 6:55 am

    Option 1, for sure. It’s not “hacky”, and that’s exactly why you abstracted the property access into methods, in the first place 🙂 Just note that you should use “field access” with Hibernate, instead of “method”, so that you don’t risk providing an unmodifiable collection to Hibernate.

    Unfortunately, Hibernate doesn’t provides a way to place unmodifiable collections, but I think you can do a @PostLoad event listener to modify all collections once the object is loaded.

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

Sidebar

Related Questions

Application : HTA (therefore IE) This is an application that uses SendKeys to populate
Application uses Entity Framework 4.1 with database first approach. I have in database a
Application.Run(form); Actually I tried to call this from my project. I got this exception.
Application.Run(new Main()); This line gives TypeInitializationException was unhandled after I switched from 3.5 to
Application requests KML data through AJAX from server. This data is stored in javascript
Application is running in my local system app.config file <add key=dbconn value=Data Source=(local);database=VTech;User ID=sa;Password=sa;
Application stores configuration data in custom section of configuration file. This information is used
Application : I am working on one mid-large size application which will be used
My application is a single page app (backbone.js) and it uses google maps that
Application: This is a workshop proposal system for a conference. A user can create

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.