如何限制被调用的@PreUpdate EntityListener另一个实体保存方法

时间:2018-06-04 06:55:56

标签: spring spring-boot spring-data spring-data-jpa spring-annotations

我有具有@PreUpdate EntityListener

的Orderitem实体

但是当我首先调用更新Order方法时,它获取OrderItems然后更新顺序,但问题是orderItem也在通过@PreUpdate EntityListener更新订单后更新。

OrderItem.java

@Entity
@Data
@Table(name = "order_item")
@EntityListeners({AuditEntityListener.class})
public class OrderItem  implements Serializable {

    .
    .
    .

    @CreatedByName
    private String createdByName;

    @UpdatedByName
    private String updatedByName;
}

Order.java

@Entity
@Table(name = "\"order\"")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Order implements Serializable {
    private long id;

    .
    .
    .
    private List<OrderItem> orderItems;
}

AuditEntityListener.java

@Service
public class AuditEntityListener {
@PreUpdate
public void onPreUpdate(Object o) {
        AuditorAwareImpl impl = new AuditorAwareImpl();
        User user = impl.getCurrentAuditor();
        if (user != null) {
            auditModel.setUpdatedBy(String.valueOf(user.getId()));
            auditModel.setUpdatedByName(user.getFullName());
        }
    }
}

@Service
public class OrderServiceImpl implements OrderService {
    public Order updateOrder(long orderId) {
      Order order = orderRepository.findOne(orderId);
      if (order != null) {
        List<OrderItem> orderItems = orderItemService.findByOrderId(orderId);
        order.setOrderItems(orderItems);
        .
      }
       // set other fields
       orderRepository.save(order);
       return order;
     }
  }

当我调用updateOrder方法然后执行后,它还通过AuditEntityListener更新orderItem字段updatedBy和updatedByName,我不希望在调用订单更新时。请帮忙

0 个答案:

没有答案