在以下查询中,我只想从订单中选择shopname
但是当我执行查询时,它会给我一个错误PersistentEntity must not be null
我搜索过这个问题,但我找不到任何解决方案
如何只从Order表中获取选定的行?
OrderRepository
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 );
}
Order.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
}
答案 0 :(得分:0)
您可以通过将nativeQuery值设置为&#39; true&#39;来使用Spring的nativeQuery属性:
@Query("SELECT ShopName FROM CustomerOrder WHERE CustomerId = ?1", nativeQuery = true)
String selectUsersOrder(Long customerId);
或者您可以在获取时首先将其强制转换为Object [],而不是从此对象中获取所需的值。
答案 1 :(得分:0)
您应该使用Projection并从回购中删除自定义方法。
@Projection(name = "onlyShopName", types = {Order.class})
public interface OnlyShopName {
String getShopName();
}
然后按照以下方式获取您的订单:
GET http://localhost:8080/orders?projection=onlyShopName