一对多关系中的两个处于休眠状态

时间:2018-07-26 14:36:28

标签: java hibernate spring-boot spring-data-jpa spring-data

我有一些这样的关系对象: enter image description here

用户与促销活动具有一对多的关系,商店也具有相同的关系。

用户类别具有此代码

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "promotion_owner", joinColumns = @JoinColumn(name = "user_id", nullable = true), inverseJoinColumns = @JoinColumn(name = "promotion_id"))
private List<Promotion> promotions;

和商店类都有这个

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "promotion_owner", joinColumns = @JoinColumn(name = "store_id"), inverseJoinColumns = @JoinColumn(name = "promotion_id"))
private List<Promotion> promotions;

问题是当我尝试创建新促销时,它说user_id没有默认值,是否可以同时添加新促销的所有必要信息?

我这样做是通过PromotionController类中的方式来创建新促销:

public void addPromotion(Promotion promotion, Integer storeId, Integer userId) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    try {
        if(promotionRepository.exists(promotion.getPromotionId())) {
            logger.error("Promosyon ID = " + promotion.getPromotionId() + " is already created, should be unique !");
        } else {
            promotion.setDateCreated(dateFormat.format(date));
            promotionRepository.save(promotion);
            storeService.addPromotion(promotion, storeId);
            customUserDetailsService.addPromotion(promotion, userId);
        }
    } catch(PromotionServiceException exception) {
        throw new PromotionServiceException("Internal Server Exception while creating promotion !");
    }
}

此操作成功创建了促销对象,但无法在商店-促销与用户-促销之间创建关系

谢谢。

0 个答案:

没有答案