1

In the CustomerTransactions entity, I have the following field to record what the customer bought:

@ManyToMany
private List<Item> listOfItemsBought;

When I think more about this field, there's a chance it may not work because merchants are allowed to change item's information (e.g. price, discount, etc...). Hence, this field will not be able to record what the customer actually bought when the transaction occurred.

At the moment, I can only think of 2 ways to make it work.

  1. I will record the transaction details into a String field. I feel that this way would be messy if I need to extract some information about the transaction later on.
  2. Whenever the merchant changes an item's information, I will not update directly to that item's fields. Instead, I will create another new item with all the new information and keep the old item untouched. I feel that this way is better because I can easily extract information about the transaction later on. However, the bad side is that my Item table may contain a lot of rows.

I'd be very grateful if someone could give me an advice on how I should tackle this problem.

1 Answer 1

1

I would try a third option something like this.

public class Item {
    private String sku;

    private double currentPrice;
}

public class Customer {
    private String name;

    private List<Transaction> transactions;
}

public class Transaction {
    private Item item;

    private Customer customer;

    private double pricePerItem;

    private double quantity;

    private String discountCode;
}

I will leave you to work out the JPA mappings.

1
  • Thanks for your suggestion! I have a strong feeling it would work nicely :D
    – Mr.J4mes
    Sep 22, 2011 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.