java - 我的实体加载速度超慢有什么问题?

标签 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 个实体...

最佳答案

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

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

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

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

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

关于java - 我的实体加载速度超慢有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39971793/

相关文章:

java - Android 中通过 TCP 进行实时音频流传输

java - 在 Java 中将 Windows 路径转换为 ​​URI?

java - 如何创建可选的外键引用?

mysql - 无法 Autowiring 字段 : com. srk.dao.UserDao com.srk.service.UserServiceImpl.userDao; org.springframework.orm.hibernate4.LocalSessionFactoryBuilder

java - 无法让我的 for 循环给我 HashMap 中最不常见的字母

java - 在 Kali 上使用 webgoat 登录时出错

java - MVC 和 MVC Model2 之间的实际区别是什么

java - Hibernate:如何使用 HQL 选择空对象?

java - Spring Boot获取请求: Failed to convert type java. lang.String到所需类型int

java - 如何启用 Hibernate HiLo 实体标识符优化器策略