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

huangapple go评论80阅读模式
英文:

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(&quot;SELECT &quot;
		+ &quot;new com.app.model.RestaurantOrderPartial( &quot;
			+ &quot;o.orderId, o.orderedAt, o.orderType, o.orderState, o.orderValue, o.deliveryPrice, o.deliveredBy, o.driver) &quot;
		+ &quot;FROM Order o &quot;
		+ &quot;WHERE o.restaurant.restaurantId = ?1 &quot;
		+ &quot;AND o.orderedAt BETWEEN ?2 and ?3 &quot;
		+ &quot;ORDER BY o.orderedAt DESC &quot;)
List&lt;RestaurantOrderPartial&gt; 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 = &quot;cityId&quot;)	
private City city;

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

@JsonIgnore
@OneToMany(mappedBy=&quot;driver&quot;)
private List&lt;Order&gt; orders;

Sample Data:
JPQL构造函数忽略具有NULL值的行。

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

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

huangapple
  • 本文由 发表于 2020年8月12日 14:48:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63371271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定