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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:32:06+00:00 2026-05-13T18:32:06+00:00

I have models corresponding to database tables. For example, the House class has color,

  • 0

I have models corresponding to database tables. For example, the House class has “color”, “price”, “square_feet”, “real_estate_agent_id” columns.

It is very common for me to want to display the agent name when I display information about a house. As a result, my House class has the following fields:

class House {
  String color;
  Double price;
  Integer squareFeet;
  Integer realEstateAgentId;

  String realEstateAgentName;
}

I’ve been referring to realEstateAgentName as a virtual field, as it is pulled from a foreign table (join on real_estate_agent_id).

This doesn’t feel right to me, as it mixes actual database columns with foreign object’s properties. But it’s quick, and in many cases it really works out well.

Other times I find myself doing something like this:

class House {
  String color;
  Double price;
  Integer squareFeet;
  Integer realEstateAgentId;

  RealEstateAgent realEstateAgent;
}

As you can see, I’m storing the actual object corresponding to the ID that is stored in the House table.

I tend to make the decision to store the entire object vs some key information associated with the ID (e.g. Name) depending on the likelihood I see of needing to access other information about the object it represents.

I have a few questions:

Of the two methods I’ve been mixing and matching, which is best? I’m leaning towards storing the id + the object, rather than pulling out just the properties from the foreign object that I think I may need. Of the two, this seems more “correct.” But it’s not perfect, because in many cases I don’t have any need to hydrate the entire foreign object, and doing so would cause undue waste of resources or would not be feasible because of the amount of data or the number of joins that would be required when I don’t have any use for all the info being brought in. Given that this is the case, it seems like a poor design choice because I will have lots of null fields that aren’t really null in my database, but are so in memory simply because there was no need to populate them — now I have to keep track of which ones I populated.

But is it best practice to store an ID alongside the object it represents? Should I even be storing the object as a property, or should it live externally in some map, with the ID being the key?

In an Object world it seems like the ID shouldn’t even be stored as a property, with the foreign Object it represents being the logical replacement. But with everything being tightly coupled with a relational database it doesn’t seem very feasible.

Is this frustrating impurity of my models/classes something I just have to live with, or are there patterns out there that address this by having some kind of fork or parent/child subclassing going on where one is a “pure” object while the other is flat like the database?

EDIT: I am looking for design suggestions here rather than specific ORM frameworks like Hibernate/nHibernate/etc. The particular language I’m working in does not have an ORM solution for my language version that I am satisfied with, and the examples were Java-esque but that’s not what my source code is written in.

  • 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-13T18:32:06+00:00Added an answer on May 13, 2026 at 6:32 pm

    I can tell about Hibernate, because this is the ORM tool I am most familiar with. I believe that other ORM tools also support similar behaviour to some extent.

    Hibernate solves your problem with lazy loading. You add your agent as a property to the house, and by default, when the house object is loaded, the agent is represented by a proxy object generated by Hibernate, which contains only the ID. If you query some other property of the agent, Hibernate loads the full object in the background:

    class House {
        String color;
        Double price;
        Integer squareFeet;
        RealEstateAgent realEstateAgent;
        // getters, setters,...
    }
    
    House house = (House) session.load(House.class, new Long(123));
    // at this point, house refers to a proxy object created by Hibernate
    // in the background - no house or agent data has been loaded from DB
    house.getId();
    // house still refers to the proxy object
    RealEstateAgent agent = house.getRealEstateAgent();
    // house is now loaded, but agent not - it refers to a proxy object
    String name = agent.getName(); // Now the agent data is loaded from DB
    

    OTOH if you are sure that for a specific class you (almost) always need a specific property, you can specify eager loading in the ORM mapping for that property, in which case the property is loaded as soon as the containing object. In the mapping you can also specify whether you want a join query or a subselect query.

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

Sidebar

Ask A Question

Stats

  • Questions 309k
  • Answers 309k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Apache POI. It's free but you cannot evaluate the formalas… May 13, 2026 at 9:50 pm
  • Editorial Team
    Editorial Team added an answer "bleeping?" What are you expecting to see happen? Edit: If… May 13, 2026 at 9:50 pm
  • Editorial Team
    Editorial Team added an answer Your checkElements() function is not marked as const so you… May 13, 2026 at 9:50 pm

Related Questions

Does someone have any tips/advice on database design for a web application? The kind
I have the following Django models: Bar, Bistro and Restaurant Each of the above
When programming in PHP I always try to create meaningful 'models' (classes) that correspond
I'm using an AJAX form to update an item to the database. When it
I'm trying to write a webapp with CakePHP, and like most webapps I would

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.