简体   繁体   中英

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, ie 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 ?

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM