jpa存储为jsonb的对象集不能更新一个子对象

tyg4sfes  于 2021-07-23  发布在  Java
关注(0)|答案(0)|浏览(178)

虽然这个实体(使用lombok)使用jsonb来存储大量的段和层树,但是添加新的段可以很好地工作,但是当我想更新现有的段时,它永远不会更新。服务方法updatechild返回更新的段,但它从未持久化。从未调用数据库更新。
我错过什么了吗?为什么repository.save返回更新的实体,但它从未序列化到db中?
谢谢!

@Data
@Setter(AccessLevel.NONE)
@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "model_policymodel")
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
public class PolicyModel extends AbstractAggregateRoot<PolicyModel> {

    @Id
    @GeneratedValue
    private long policyModelId;

    @Type(type = "jsonb")
    @Column(columnDefinition = "jsonb")
    private Set<SegmentPolicy> segmentPolicies = new HashSet<>();

    public PolicyModel updatePolicy(SegmentPolicy segmentPolicy) {
        segmentPolicies.remove(segmentPolicy);
        segmentPolicies.add(segmentPolicy);
        return this.andEventsFrom(this)
                .andEvent(PolicySetManuallyOnSegmentEvent.of(this, segmentPolicy));
    }

}

以及政策部分

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class SegmentPolicy {

    @EqualsAndHashCode.Include
    private long segmentId;

    @Builder.Default
    private List<Tier> tiers = new ArrayList<>();

}

还有服务

@Service
@Transactional
public class PolicyModelService {

  @Autowired PolicyModelRepository repository;

  public PolicyModel updateChild(long id, SegmentPolicy policy) {
    PolicyModel model = repository.findById(id).orElseThow(() -> new RuntimeException("Ups"));
    return repository.save(model.updatePolicy(policy));
  }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题