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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:02:57+00:00 2026-05-11T20:02:57+00:00

I have an object with a field that can be a number of object

  • 0

I have an object with a field that can be a number of object types. This object is encoded in a single table with a discriminator column for the field’s subtypes. Each of these subtypes have their fields mapped to a column in the parent objects table. I cannot seem to model this in hibernate. The code bellow will return null for getSubfield() regardless of what subtype data is in the table.

Schema

  id   type   whosit   whatsit  
+----+------+--------+---------+
| 1  | "A"  | "test" | null    |
| 2  | "B"  | null   | "test"  |
+----+------+--------+---------+

Domain Objects

@Entity
public class Parent {
    protected @Id @GeneratedValue int id;
    protected Subfield subfield;

    public Subfield getSubfield() {return subfield;} 
}


@Embeddable
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.STRING)
public abstract class Subfield {}


@DiscriminatorValue("A")
public class TypeA extends Subfield {
    public String whosit;
}


@DiscriminatorValue("B")
public class TypeB extends Subfield {
    public String whatsit;
}

“SELECT p FROM parent p”

{id=1,subfield=null}
{id=2,subfield=null}

Is it possible to accomplish what I want with this object model, or do I need to get a bit more creative (this is a legacy database, changing the schema is not preferred)

  • 1 1 Answer
  • 1 View
  • 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-11T20:02:57+00:00Added an answer on May 11, 2026 at 8:02 pm

    Ok you can’t easily change the schema, but how about adding a couple of views?

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

Sidebar

Related Questions

I have a field that may be blank, so I need to use Object.try,
I have a Grails command object that contains an emailAddresses field, e.g. public class
I have a message definition file that looks like this struct1 { field=name type=string
I have clients and contacts, and each client can have any number of contacts
I have a user a object that looks like this. $user = new User();
We have a hosted application that manages pages of content. Each page can have
I have a bunch of objects that have a value and a date field:
I have async methods that returns an objects public static IEnumerable<Users.User> GetUsers(IEnumerable<string> uids, Field
I have an object Field field . I'd like to check if field is
I Have a object which the following field : boost::unordered_map<std::string, std::shared_ptr<Foo> > m_liste_; I

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.