gpt4 book ai didi

java - Spring 数据休息:How to select specific field from the repository

转载 作者:行者123 更新时间:2023-11-29 18:47:41 24 4
gpt4 key购买 nike

在下面的查询中,我只想从订单中选择shopname,但是当我执行查询时,它给我一个错误PersistentEntity can not be null我已经搜索过这个问题但找不到任何解决方案如何仅从订单表中获取选定的行?

订单存储库

public interface OrderRepository extends JpaRepository<Order, Long> {
@Query("Select o.shopName from Order o where o.customer.id= :customerId ")
String selectUsersOrder(@Param("customerId") Long customerId );
}

订单.class

@Entity
@Table(name="CustomerOrder")
public class Order implements Serializable,OrderGetters {
private static final long serialVersionUID = 1L;

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

@Column( nullable = false, updatable = false, insertable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
@Temporal(TemporalType.TIMESTAMP)
private Date orderDate ;

@OneToMany(mappedBy="order",targetEntity=OrderItem.class)
private List<OrderItem> orderitem;

@Temporal(TemporalType.TIMESTAMP)
private Date completionDate ;

@Enumerated(EnumType.STRING)
private OrderStatus orderStatus;

@Enumerated(EnumType.STRING)
private DeliveryStatus deliveryStatus;



private String riderInstruction;

@Enumerated(EnumType.STRING)
private OrderType orderType;

@ManyToOne
private User customer;


@OneToOne
private User rider;

@OneToOne
private Address dropOffAddress;

@OneToOne
private Address pickupAddress;

private String receiverName;

private String promoCode;

private String shopName;

private float shoppingPrice;

private float orderPrice;

private float cashCollected;

..... Getter and setters
}

最佳答案

您可以通过将nativeQuery值设置为“true”来使用Spring的nativeQuery属性:

@Query("SELECT ShopName FROM CustomerOrder WHERE CustomerId = ?1", nativeQuery = true)
String selectUsersOrder(Long customerId);

或者您可以在获取时先将其转换为 Object[],然后从该对象中获取您想要的值。

关于java - Spring 数据休息:How to select specific field from the repository,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44515534/

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