gpt4 book ai didi

java - JPQL 构造函数忽略具有 NULL 值的行

转载 作者:行者123 更新时间:2023-12-01 14:31:33 26 4
gpt4 key购买 nike

我有以下声明:

@Query("SELECT "
+ "new com.app.model.RestaurantOrderPartial( "
+ "o.orderId, o.orderedAt, o.orderType, o.orderState, o.orderValue, o.deliveryPrice, o.deliveredBy, o.driver) "
+ "FROM Order o "
+ "WHERE o.restaurant.restaurantId = ?1 "
+ "AND o.orderedAt BETWEEN ?2 and ?3 "
+ "ORDER BY o.orderedAt DESC ")
List<RestaurantOrderPartial> getRestaurantOrdersCompressed(long restaurantId, LocalDateTime dateBeforePeriod, LocalDateTime now); //12 Month

表中 o.driverNULL 的行,构造函数不构造对象的条目并且不包含它 在结果列表中。

为什么会有这种行为?以及如何让构造函数包含 Driver 为 null 的条目,在自定义对象 RestaurantOrderPartial 中,根据 Column 中的值,Driver 对象将为 null。

这是我的 POJO:

public class RestaurantOrderPartial {

private long orderId;
private LocalDateTime orderedAt;
private OrderType orderType;
private OrderState orderState;
private Long orderValue;


private int deliveryPrice;
private DeliveredBy deliveredBy;
private Driver driver;

}

public class Driver {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long driverId;

@Column
private String email;

@Column
private String password;

@Column
private String fullName;

@Column
private String phoneNumber;

@Column
private String secondaryPhoneNumber;

@Column
private String fullAddress;

@Column
private boolean isActive;

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "cityId")
private City city;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "locationId")
private Location location;

@JsonIgnore
@OneToMany(mappedBy="driver")
private List<Order> orders;

示例数据: sample data

感谢您的帮助。

最佳答案

如果o.driver为null则要取出来,必须指定left outer join; o.driver 始终是内部联接,因此会从结果中过滤掉空值。

尝试更像

"SELECT "
+ "new com.app.model.RestaurantOrderPartial( "
+ "o.orderId, o.orderedAt, o.orderType, o.orderState, o.orderValue, o.deliveryPrice, o.deliveredBy, driver) "
+ "FROM Order o LEFT OUTER JOIN o.driver driver"
+ "WHERE o.restaurant.restaurantId = ?1 "
+ "AND o.orderedAt BETWEEN ?2 and ?3 "
+ "ORDER BY o.orderedAt DESC ")

关于java - JPQL 构造函数忽略具有 NULL 值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63371271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com