Springboot 在 oneTOMany 关系中添加问题 [英] Springboot add problem in oneTOMany relation

查看:38
本文介绍了Springboot 在 oneTOMany 关系中添加问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按以下关系编写 3 个表:

I'm writing 3 tables in the following relation:

俱乐部课程:

@Setter
@Getter
@Entity
@Table(name = "Club")
public class Club {
    @Id
    @GeneratedValue
    private Long id;

    private String name;
    private String type;
    private String mainPage;
    private String logo;

    @OneToMany(mappedBy="clubProductKey.club", cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = "clubProductKey.club", allowSetters=true)
    private Set<ClubProduct> clubProducts;
...

产品类别:

@Setter
@Getter
@Entity
@Table(name = "Product")
public class Product {
    @Id
    @GeneratedValue
    private Long id;

    @OneToMany(mappedBy="clubProductKey.product", cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = "clubProductKey.product", allowSetters=true)
    private Set<ClubProduct> clubProducts;
...

ClubProduct 类:

ClubProduct class:

@Setter
@Getter
@Entity
@Table(name = "ClubProduct")
public class ClubProduct {
    @EmbeddedId
    private ClubProductKey clubProductKey;
...

ClubProductKey 类:

ClubProductKey class:

@Setter
@Getter
@Embeddable
public class ClubProductKey implements Serializable {
    @ManyToOne(cascade = {CascadeType.MERGE,CascadeType.REFRESH })
    @JoinColumn(name = "club_id", referencedColumnName = "id")
    @JsonIgnoreProperties(value = "clubProducts", allowSetters=true)
    private Club club;

    @ManyToOne(cascade = {CascadeType.MERGE,CascadeType.REFRESH })
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @JsonIgnoreProperties(value = "clubProducts", allowSetters=true)
    private Product product;
...

ClubProductRepository 类:

ClubProductRepository class:

public interface ClubProductRepository extends JpaRepository<ClubProduct, ClubProductKey> {
    public List<ClubProduct> findByClubProductKeyClub(Club club);
    public List<ClubProduct> findByClubProductKeyProduct(Product product);
}

我尝试像这样保存 clubProduct:

I try to save clubProduct like this:

@Service
public class ClubProductServiceImp implements ClubProductService {
    @Autowired
    private ClubProductRepository clubProductRepository;
    ...
    ClubProduct savedClubProduct = clubProductRepository.save(clubProduct);
    return savedClubProduct;
}

但是我发现clubProduct没有保存在club或product实体的clubProducts列表中,list为空.我必须添加诸如 club.getClubProducts.add(clubProduct) 之类的行,还是有其他方法可以让它自动添加?

However I find that the clubProduct is not saved in the clubProducts list in the club or product entity, the list is null. Must I add lines like club.getClubProducts.add(clubProduct) or is there any other way to make it added automatically?

谢谢.

推荐答案

Club 类中的 @OnetoMany 映射使用属性 mappedby 这意味着它代表拥有负责处理映射关系的一侧.但是,我们仍然需要让双方同步,否则会破坏领域模型关系的一致性,并且除非双方正确同步,否则无法保证实体状态转换工作.

The @OnetoMany mapping in your Club class uses the attribute mappedby which means that it represents the owning side of the relation responsible for handling the mapping. However, we still need to have both sides in sync as otherwise, we break the Domain Model relationship consistency, and the entity state transitions are not guaranteed to work unless both sides are properly synchronized.

答案是肯定的,您必须自己管理 java 关系,以便 clubProducts 持久化.您正在使用存储库类 club 的实例来保存数据,因此,您应该添加一个 setter 方法,例如:

The answer is yes, you have to manage the java relations yourself so that the clubProducts gets persisted. You are using an instance of the repository class club to persist the data so , you should add a setter method like :

  public void addClubProduct(ClubProduct clubProduct) {
     if (clubProduct!= null) {
        if (clubProduct== null) {
            clubProduct= new ArrayList<ClubProduct>();          
        }
        clubProducts.add(clubProduct);
        clubProduct.setClubProduct(this);
     }
  }

也是一种将其从列表中删除并在您的代码中使用这些方法在启动 save 之前正确设置列表中的值的方法.阅读相关文章

also a method to remove it from the list and use these method in your code to set the values to the list properly before initiating save . Read related article

这篇关于Springboot 在 oneTOMany 关系中添加问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆