简体   繁体   中英

What is wrong with my entity that it is loading super slow?

I have the following entity:

@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;
}

Now, whenever I am running this query for the first time, using the following repository:

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);
}

It takes around 5 seconds to execute the following:

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

Where:

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;
}

NOTE

I have only 3 entities in my table...

Try to add "mappedBy" value in @OneToOne annotations. Actualy, look like you have something wrong with this:

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

Don't know why there is two classes OtherDetails and one class OtherDetails3, but look like you should do

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

And also, may be, you have too much data. Enable your hibernate logs, and execute query in your sql (oracle as I understand) server directly. May be sql server have no enough memory for result set, and it perform write to disk operation.

My problem was that I was using @Convert annotation on multiple fields, which slowed down the whole process. For example:

@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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM