英文:
JPQL Constructor ignoring Rows whose have a NULL value
问题
以下是您要翻译的代码部分:
// 在表中`o.driver`为`NULL`的行中,构造函数不会将条目构造为对象,并且**不包括**在结果列表中。
// 为什么会有这种行为?如何让构造函数在自定义对象`RestaurantOrderPartial`中包括Driver为null的条目,根据列中的值,Driver对象将为null。
// 这是我的POJOs:
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;
}
希望这对您有所帮助。如果您有任何其他问题,请随时提出。
英文:
I have following statement:
@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
The rows in table where o.driver
is NULL
, the constructor doesn´t contruct the entry to an object and doesn´t include it in the result list.
Why does it have this behavior? and how let the constructor include the entries where Driver is null, in the custom object RestaurantOrderPartial
the Driver object would be null according to value in Column.
Here are my POJOs:
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;
Thanks for your help.
答案1
得分: 2
如果您希望在 o.driver 为空时获取它,您必须指定左外连接;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 ")
英文:
If you want o.driver to be fetched if it is null, you must specify a left outer join; o.driver is always an inner join and so filters out null values from the results.
Try something more like
"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 ")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论