asinkxcoswt
asinkxcoswt

Reputation: 2544

JPA use Map<ID, Entity> instead of List<Entity>

I am using Spring JPA to design an e-commerce application. Suppose I have 2 classes Product and Option with one-to-many relationship, i.e. a Product will have many Option and an Option belongs to only one Product. With this, the typical JPA mapping would be

@Entity(name = "products")
class Product {
    @Id
    @Column(name = "product_id")
    private Long productId;

    @OneToMany(cascade = ALL, orphanRemoval = true)
    @JoinColumn(name = "product_id")
    private List<Option> options;

    // getters, setters
}

Please notice that the type of options is List<Option>. I am thinking of using Map instead of list here.

    @OneToMany(cascade = ALL, orphanRemoval = true)
    @JoinColumn(name = "product_id")
    @MapKey(name = "optionId")
    @MapKeyClass(Long.class)
    private Map<Long, Option> options;

Using Map, I think it will be useful for updating and deletion options. For example, by using List<Option>, when I want to delete an Option from a Product given a option id, I would do

public void deleteOption(Long productId, Long optionId) {
    Product p = productRepository.findByProductId(productId);
    List<Option> options = p.getOptions();
    Option toBeRemoved = null;
    for (Option o : options) {
       if (o.getOptionId == optionId) {
          toBeRemoved = o;
       }
    }
    options.remove(toBeRemoved);
    productRepository.save(p);
}

But if I use Map, it will be easier.

public void deleteOption(Long productId, Long optionId) {
    Product p = productRepository.findByProductId(productId);
    p.getOptions.remove(optionId);
    productRepository.save(p);
}

The Question

In general, is there a reason to use List<Entity> over Map<Long, Entity> for a one-to-many association? Can I always use Map?

Upvotes: 2

Views: 621

Answers (1)

Hadi
Hadi

Reputation: 17289

I think you can use List and for delete option use removeIf() method. also it's better use Set to avoid duplicate option as @JB Nizet said.

p.getOptions().removeIf(option->option.getOptionId().equals(optionId));

Upvotes: 1

Related Questions