0

In the following query I want to select only shopname from the order but when I execute the query it gives me an error of PersistentEntity must not be null I have searched this problem but I can't find any solution How can I get only selected rows from the Order table ?

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
    }
Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
SFAH
  • 624
  • 15
  • 35

2 Answers2

0

You may use the nativeQuery property of Spring by setting the nativeQuery value to 'true':

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

or you can cast it to an Object[] first while fetching, than take the value you want from this object.

ahmetcetin
  • 2,621
  • 1
  • 22
  • 38
0

You should use Projection and delete your custom method from the Repo.

@Projection(name = "onlyShopName", types = {Order.class})
public interface OnlyShopName { 

    String getShopName();
}

Then GET your orders like this:

GET http://localhost:8080/orders?projection=onlyShopName
Cepr0
  • 28,144
  • 8
  • 75
  • 101