作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在下面的查询中,我只想从订单中选择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/
我是一名优秀的程序员,十分优秀!