我的实体加载速度超慢的问题是什么?

时间:2016-10-11 06:50:09

标签: java hibernate

我有以下实体:

@Entity
@Table(name="\"Order\"")
public class Order {
    @Id
    @SequenceGenerator(name="order_id_seq",
            sequenceName="order_id_seq",
            allocationSize=1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE,
            generator="order_id_seq")
    private long id;
    @OneToOne
    private Customer customerDetails;
    @OneToOne
    private ProductDetails productDetails;
    @OneToOne
    private TransportDetails transportDetails;
    @OneToOne
    private OtherDetails otherDetails;
    @OneToOne
    private OtherDetails otherDetails2;
    @OneToOne
    private OtherDetails3 otherDetails3;

    private LocalDateTime dateOrderPlaced;

    private LocalDateTime dateOrderPaid;

    private Float totalPrice;

    @Size(max = 1000)
    private String orderComment;
}

现在,每当我第一次运行此查询时,请使用以下存储库:

public interface OrderRepository extends CrudRepository<Order, Long> {

    @Transactional
    @Modifying
    @Query("UPDATE Order o SET o.totalPrice = (:price) WHERE o.id= (:id)")
    void updateTotalPrice(@Param("id") Long id, @Param("price") Float price);

    @Override
    List<Order> findAll();

    @Override
    Order save(@Valid Order order);
}

执行以下操作大约需要5秒钟:

public List<OrderDto> getOrders(){
    return orderDtoMapper.fromDomain(orderRepository.findAll());
}

其中:

public List<OrderDto> fromDomain(List<Order> orders){
    return orders
            .stream()
            .map(order -> fromDomain(order))
            .collect(Collectors.toList());
}

public OrderDto fromDomain(Order order){
    OrderDto orderDto = new OrderDto();
    orderDto.setTransportDetails(transportDetailsDtoMapper.fromDomain(order.getTransportDetails()));
    orderDto.setTotalPrice(order.getTotalPrice());
    orderDto.setDateOrderPaid(order.getDateOrderPaid());
    orderDto.setDateOrderPlaced(order.getDateOrderPlaced());
    orderDto.setId(order.getId());

    return orderDto;
}

注意

我桌上只有3个实体......

2 个答案:

答案 0 :(得分:1)

尝试在"mappedBy"注释中添加@OneToOne值。 Actualy,看起来你有问题:

    @OneToOne
    private OtherDetails otherDetails;
    @OneToOne
    private OtherDetails otherDetails2;
    @OneToOne
    private OtherDetails3 otherDetails3;

不知道为什么有两个类OtherDetails和一个类OtherDetails3,但看起来应该这样做

@OneToMany(...)
List<OtherDetails> otherDetails

而且,也许,你有太多的数据。启用你的hibernate日志,并直接在你的sql(我理解的是oracle)服务器中执行查询。可能是sql server没有足够的内存用于结果集,并且它执行写入磁盘操作。

答案 1 :(得分:0)

我的问题是我在多个字段上使用@Convert注释,这会减慢整个过程。例如:

@Data
@Entity
@Table(name="\"Customer\"")
public class Customer {
    @Id
    @SequenceGenerator(name="customer_id_seq",
            sequenceName="customer_id_seq",
            allocationSize=1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE,
            generator="customer_id_seq")
    private long id;
    private String firstName;
    @Convert(converter = LastNameEncryption.class)
    private String lastName;
}