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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:31:20+00:00 2026-05-26T05:31:20+00:00

I have a general OO design question that stems from a Hibernate Model. Example

  • 0

I have a general OO design question that stems from a Hibernate Model.

Example

payment – Base (SuperType)

@Entity
@Table(name = "PAYMENT")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn( name = "type", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("BASE")

public class Payment{

 private Product product;
 private Date date;
 private Customer Customer;
 getters/setters
}

CreditCard

@Entity
@DiscriminatorValue("CC")
public class CreditCard extends Payment {
  private String Account
  getters/setters
}

Cash

 @DiscriminatorValue("CASH")
public class Cash extends Payment {

  private String Paper
  private String Coins
  getters/setters

}

Working with hibernate is not a problem (table per class hierarchy). Since Hibernate is able to accept a generic Object instance and still persist the correct instance.

My question is centered around working with the Payment polymorhically in other parts of the code. Since each subclass adds unique instance fields does that mean I need to stub out these subclass fields on Payment itself just to buy the benefit of polymorphism. This does not seem correct in that each time I add a new field to a subclass or a new type of payment I am going back and adding metohods to Payment.

Is there something I am missing, a pattern or somthing inherent to Java I can use?

Thanks

-J

  • 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-26T05:31:21+00:00Added an answer on May 26, 2026 at 5:31 am

    Purely from OO pov (point of view), you should be trying to extract interface to CreditCard or Cash types of transactions. So ideally you shouldn’t have any accessors to variables of sub-class X if you can not implement it for sub-class Y (or better to say at least one other sub-class).

    E.g. in this case I would say you could have methods like these in Payment:

    String getPaymentType();
    double getPaymentAmount();
    

    which are implemented by all sub-classes. Differently.

    Then you get true polymorphism.

    If you must access something specific like say Paper or Coin “in other parts of the code”, then those parts of code should import Cash instead of Payment. This is because that piece of code is specific to Cash transactions and not related to CreditCard, so no point in extracting some specific methods in Payment.

    PS: You said “…Since Hibernate is able to accept a generic Object instance and still persist the correct instance.” ==> This of course is not polymorphism. I donno anything abt Hibernate but I’d guess that it would use RTTI to accomplish this, and that’s like exactly the opposite of polymorphism. 🙂

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

Sidebar

Related Questions

I have a general question that is rather open-ended (i.e. depends on platform, application
I have a general question about the way that database indexing works, particularly in
This is a general design question about how to make a web application that
I've a general design question: I have a mailserver, written in C#. Then I
I have a general question about the design of JavaScript Libraries. I am trying
Just a very general question, that not only applies to this example. Let's say
just a general design question that I'd like to hear some of your opinions
This is more of a design question that others may have had similar experience
I have a base class and several concrete classes that derive from it. Lets
I have the following database design question: I have a record that contains 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.