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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:59:51+00:00 2026-05-13T00:59:51+00:00

I am trying to model such situation – there is a cash transfer (I

  • 0

I am trying to model such situation – there is a cash transfer (I mean a car that carries money), that has required amounts of each currency, and also an actual amount for each currency. And it seems to me pointless to create two separate classes, one for required amount and another for actual amount. So the implementation would look like this:

@Entity
public class CashTransferCurrencyAmount {
    // id, version and so on

    @Column(length = 3)
    private String currencyCode;

    @Basic
    private BigDecimal amount;

    @ManyToOne
    private CashTransfer cashTransfer;
}

@Entity
public class CashTransfer {
    // id, version and so on

    @OneToMany(mappedBy="cashTransfer")
    private Set<CashTransferCurrencyAmount> requiredCurrencyAmountSet = new HashSet<CashTransferAmountCurrency>();

    @OneToMany(mappedBy="cashTransfer")
    private Set<CashTransferCurrencyAmount> actualCurrencyAmountSet = new HashSet<CashTransferAmountCurrency>();
}

But how is a CashTransferCurrencyAmount instance to know to which collection it belongs? I have two ideas:

1 – add a discriminator field to CashTransferCurrencyAmount:

public enum RequestType {
    ACTUAL,
    REQUIRED
}

@Basic
@Enumerated(EnumType.STRING)
private RequestType requestType;

and add @WHERE annotations to collections in CashTransfer. This is preferable for me.

2 – create two join tables. one for mapping requested amounts and one for mapping actual amounts. I dislike this one as I don’t want too many tables in my DB.

Are there any other ways to achieve this? I this approach correct?
And please don’t tell me to put both requested and actual amounts in one entity. The real case is more complicated, each CashTransferCurrencyAmount has it’s own collections so it can’t be solved that way.

EDIT
As for requests for complete story – there used to be two values in CashTransferCurrencyAmount – required (I think it should be ‘requested’) and actual, but now each amount has it’s own collection – how this amount is split into denominations. So I need a collection of amounts, each one having a collection of denominations. The type of CurrencyAmount and CurencyDenomination seems to be the same for requested ones and for actual ones.

  • 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-13T00:59:51+00:00Added an answer on May 13, 2026 at 12:59 am

    Since you want CashTransferCurrencyAmount instance to know which collection it belongs to, I assume you want to have some logic based on that. The way I would model your situation would be using inheritance.

    You’re saying “it seems to me pointless to create two separate classes”, I would however try to convince you that you should. You could use a “Single Table” inheritance type, so that you don’t introduce additional tables in your DB, which is what you’re trying to accomplish.

    My shot would look something like:

    
    @Entity
    @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(name = "request_type", discriminatorType = DiscriminatorType.STRING)
    public abstract class CashTransferCurrencyAmount {
        // id, version and so on
    
        @Column(length = 3)
        private String currencyCode;
    
        @Basic
        private BigDecimal amount;
    
        @ManyToOne
        private CashTransfer cashTransfer;
    }
    
    @Entity
    @DiscriminatorValue("REQUIRED")
    public class CashTransferCurrencyAmountRequired extends CashTransferCurrencyAmount {
        // required anount specific stuff here
    }
    
    @Entity
    @DiscriminatorValue("ACTUAL")
    public class CashTransferCurrencyAmountActual extends CashTransferCurrencyAmount {
        // actual anount specific stuff here
    }
    
    @Entity
    public class CashTransfer {
        // id, version and so on
    
        @OneToMany(mappedBy="cashTransfer")
        private Set requiredCurrencyAmountSet = new HashSet();
    
    //Stackoverflow deleting my generic sets! But it's exactly the same as in your code...
    
        @OneToMany(mappedBy="cashTransfer")
        private Set actualCurrencyAmountSet = new HashSet();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to model a class that has a method with a variable argument
I am trying to model a tree relationship in a table. For instance, there
I am trying to model a transfer function block for say 1/(s+1). What is
I'm trying to write some unit tests for a JPA model that I've built
I am trying to bind model received from Ajax call but that do not
I'm trying to leverage NH to map to a data model that is a
I have a situation where, in a model's afterSave callback, I'm trying to access
I am trying to pass a link that contains variables to a mailer model.
I'm trying to model this constraint in GMPL. As far as I can understand,
I am trying to model a simple domain by using DDD. Database layer is

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.