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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:36:54+00:00 2026-05-26T00:36:54+00:00

I need to store a customer ‘s currently selected payment info ,so that this

  • 0

I need to store a customer‘s currently selected payment info ,so that this info can be used in processing a purchase.Customer can have many credit cards.So I modelled it as a OneToMany relation.I am doubtful about storing the currently selected payment info as a different field(as shown below).Having a field of Set<Payment> and Payment side by side ,looks not quite right.

Is there a better way to do this?May be use some flag to mark that this is the currently selected card info

@Entity
class Customer{
...
@OneToMany( cascade=CascadeType.ALL,orphanRemoval=true)
Set<Payment> payments;

Payment currentlySelectedPayment;//?

}

@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"cardType","cardNumber"}))
public class Payment{
...
   String cardType;
   String cardNumber;
   String nameOnCard;
   ...
   Date dateOfExpiry;//day,month,year of expiry

}

I implemented the business logic as

void addNewCard(...){
  Payment payment = get_from_db_or_create_new_payment(...);
  if(!customer.getPayments().contains(payment) ){
      customer.getPayments().add(payment);
  }
 ...
}

void setCurrentlySelectedPaymentForCustomer(Long paymentId,..){
    Payment payment = getPaymentFromDB(paymentId);
    if(!customer.getCurrentlySelectedPayment().equals(payment)){
        customer.setCurrentlySelectedPayment(payment);
    }
...
}
  • 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-26T00:36:55+00:00Added an answer on May 26, 2026 at 12:36 am

    I don’t see any problem with the design. You need to add the payment to the payments set. After that you need to assign the selected payment to the Customer.

    If you are using a set you don’t need to use:

    if(!customer.getPayments().contains(payment) ){
    

    because a Set doesn’t permit to add duplicated elements.

    Also I don’t see a reason to do this:

    if(!customer.getCurrentlySelectedPayment().equals(payment)){
    

    You could just set it and don’t need to evaluate if the currentlySelectedPayment is not equal.

    I have came across with similar situations where you need to first add the elements to the parent and then assign one of those elements to one of the parent’s attributes. The way I’ve solved it is the way you did instead of having a flag (true/false attribute) in the childs.

    I think the way you are doing it is the right way to do. I think a flag like for example in this case an attribute named selectedPayment in Payment is not an appropriate attribute for the object. Why? Because first is an attribute of the Customer and not the Payment itself. Second when you want to change the selectedPayment you would need to obtain the selectedPayment from the child object, delete the flag and assign the flag to the new child, many steps that could have been avoided if you just create the attribute in the parent.

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

Sidebar

Related Questions

I have id values for products that I need store. Right now they are
I need to store basic data of customer's and cars that they bought and
We need to store customer credit card and personal info. Id like to look
I have been thinking about the best way to store product / customer /
A customer need a document managment system and I'm building information about this. I
All, I have Core Data implemented in my program to store customer information. Let's
I am building an e-store and I need to have the ability to add
I have a web page where users need to enter customer contact information. They
Can I send to my customer a beta version of my iOS app that
I have a customer with a very small set of data and records that

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.