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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:34:24+00:00 2026-05-30T06:34:24+00:00

I use the Builder pattern for several classes of my project (multiple arguments, some

  • 0

I use the Builder pattern for several classes of my project (multiple arguments, some mandatory, some optional, etc.). These classes are immutable (no setters, deep copy for collections getters).

I am now trying to store those objects in a database using a persistence framework that constructs objects with default constructor + setters. It doesn’t like my Builders very much!

I don’t want to degrade that setup to POJOs and lose the advantages of the current design (flexibility, immutability, construction security).

I would welcome any feedback on workarounds that can be used in this situation (I could wrap each of these classes but that would double the number of classes and I would rather avoid that).

One post actually points that as a specific disadvantage of the Builder pattern.

EDIT

One answer proposes to use private constructor / setters but that only works if the fields of the class are not final, which is not my case.

FINAL EDIT

Thanks to all.
What I think will be my final solution looks like this and works fine (for the record, I’m using MongoDB + Morphia):

class AClass {
    private final String aField;
    private final AClass() {
        aField = "";
    }
    //Standard builder pattern after that - no setters (private or public)
}
  • 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-30T06:34:26+00:00Added an answer on May 30, 2026 at 6:34 am

    As I said in my comment: you can include a default constructor and all required setters but make them private. This way you maintain the immutability of your Objects but an ORM such as Hibernate will be able to access the methods/constructor when it needs to.

    Anybody else would be able to access these methods using reflection too, but then they can access the private member variables using reflection too. So there is no real downside to adding the private methods.

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

Sidebar

Related Questions

I use builder for my buildprocess. I have some java classes in my src
Anyone know of some tutorial material for how to use Interface Builder for iPhone
(By the way, I don't use Interface Builder) I have a little project consisting
What are some common , real world examples of using the Builder Pattern? What
I want to clarify my use of the builder pattern, in particular how the
We are using builder pattern to generate test data. These domain objects have relations
I use Java builder pattern introduced by Joshua Bloch. Sometimes, I find certain fields
I'm following a tutorial on how to use Interface Builder from Chapter 2 of
I'm using SQL Server 2008 (non-R2) and trying to use Report Builder 3.0. When
I want to use the new report builder 2.0, rather than the old VS2005

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.