如何在Spring Data中实现包含两个相同类型的实体列表的实体

时间:2018-10-12 15:10:08

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

我有实体订单。它包含玻璃零件清单和非玻璃零件清单。零件是具有属性PartType(玻璃/非玻璃)的单个实体。请问这对Spring Data可行吗?怎么样?我正在将Spring Boot 1.3.1.RELEASE与Hibernate结合使用。

@Entity
@Table(name="order")
public class Order {

    ...

    @OneToMany(...)
    private List<Part> glassParts;

    @OneToMany(...)
    private List<Part> otherParts;
}

@Entity
@Table(name="part")
public class Part {

  ...

  @Column(...)
  private PartType partType;

  @Column(...)
  private String code;

  @Column(...)
  private String description;
}

1 个答案:

答案 0 :(得分:0)

我相信在您的情况下,创建两个列表不是一个好主意。为了获得结果,有两种常用方法。第一个是创建双向关系,并在适当的条件下实现存储库和管理器层:

@Entity
@Table(name="order")
public class Order {

    ...

    @OneToMany(mappedBy = "order", fetch = FetchType.LAZY, cascade = { ... })
    private List<Part> parts;
}

@Entity
@Table(name="part")
public class Part {

  ...

  @JoinColumn(name = "r_order_id", referencedColumnName = "id")
  @ManyToOne(...)  
  private Order order;
  @Column(...)
  private PartType partType;

  @Column(...)
  private String code;

  @Column(...)
  private String description;
}

public interface PartInterface implements CrudRepository<Part, Long> {

  List<Part> findByOrderIdAndPartType(Long orderId, PartType partType);
}

@Component
public class PartManager {

  @Autowired
  private PartInterface partInterface;

  public List<Part> getGlassPartsByOrderId(Long orderId) {
    return List<Part> partInterface.findByOrderIdAndPartType(orderId, PartType.GLASS);
  }

  public List<Part> getNonGlassPartsByOrderId(Long orderId) {
    return List<Part> partInterface.findByOrderIdAndPartType(orderId, PartType.NON_GLASS);
  }
}

第二个是按以下类型过滤订单的所有部分:

public interface OrderInterface implements CrudRepository<Order, Long> {

  @Override
  Order findOne(Long orderId);
}

@Component
public class OrderManager {

  @Autowired
  private OrderInterface orderInterface;

  public List<Part> getGlassPartsByOrderId(Long orderId) {    
    return getOrderParts(orderId).stream().filter(part -> Objects.equals(PartType.GLASS, part.getPartType())).collect(Collectors.toList());    
  }

  public List<Part> getNonGlassPartsByOrderId(Long orderId) {
    return getOrderParts(orderId).stream().filter(part -> Objects.equals(PartType.NON_GLASS, part.getPartType())).collect(Collectors.toList());    
  }

  private List<Part> getOrderParts(Long orderId) {
    Order order = orderInterface.findOne(Long orderId);
    if (Objects.isNull(order) || Objects.isNull(order.getParts())) {
      return Collections.emptyList();
    } else {
      return order.getParts();
    }
  }
}

实际上还有另一种选择-动态过滤实体内部的零件:

@Entity
@Table(name="order")
public class Order {

    @OneToMany(...)
    private List<Part> parts;

    public List<Part> getGlassParts{
      if (Objects.nonNull(parts)) {
        return parts.stream().filter(part -> Objects.equals(PartType.GLASS, part.getPartType())).collect(Collectors.toList());
      } else {
        return Collections.emptyList();
      }
    }

    public List<Part> getNonGlassParts{
      if (Objects.nonNull(parts)) {
        return parts.stream().filter(part -> Objects.equals(PartType.NON_GLASS, part.getPartType())).collect(Collectors.toList());
      } else {
        return Collections.emptyList();
      }
    }
}

更新 由于您使用的是Hibernate,因此有一个依赖于ORM的选项来获得所需的结果,它是@JoinColumnOrFormula的注释,仅适用于Hibernate

@Entity
@Table(name="order")
public class Order {

    @OneToMany(...)
    @JoinColumnOrFormula(
      column = @JoinColumn(name = "id", referencedColumnName = "r_order_id", insertable = false, updatable = false)
      ,formula = @JoinFormula(value = PartType.GLASS_STR_VALUE, referencedColumnName = "partType"))
    private List<Part> glassParts;

    @OneToMany(...)
    @JoinColumnOrFormula(
      column = @JoinColumn(name = "id", referencedColumnName = "r_order_id", insertable = false, updatable = false)
      ,formula = @JoinFormula(value = PartType.NON_GLASS_STR_VALUE, referencedColumnName = "partType"))
    private List<Part> nonGlassParts;    
}

请注意@JoinFormula子句中的 value 参数必须为String