开发者

How should I implement Transaction database EJB 3.0

开发者 https://www.devze.com 2023-04-06 12:59 出处:网络
In the CustomerTransactions entity, I have the following field to record what the customer bought: @ManyToMany

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 开发者_StackOverflow中文版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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消