skip to Main Content

In Service

When I use print the keys that output null

but print the ids that is correct

please tell me why

I’m sure the database has data

orderBean

@EntityListeners(AuditingEntityListener.class)
@Entity
@Table(name = "`Order`")
public class OrderBean {

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

    private String account;


    public String getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(String orderNumber) {
        this.orderNumber = orderNumber;
    }
    //Getters and Setter ....

}

orderDAO

public interface OrderDAO extends JpaRepository<OrderBean, Long>{

    @Query(value = "SELECT * FROM `order` WHERE account=?", nativeQuery = true)
    public List<OrderBean> findOrderByAccount(String account);
}

orderService

public List<OrderBean> findOrderByAccount(String account) {

List<OrderBean> orderBeanList = orderDAO.findOrderByAccount(account);

for(OrderBean orderBean : orderBeanList) {
            
String key = orderBean.getOrderNumber();
//output:null   null
String id = orderBean.getId();
//output:1   2
}

DataBase

2

Answers


  1. You can have a look at this example, it looks like query should be:

    SELECT * FROM OrderBean ob WHERE ob.account=?1
    
    Login or Signup to reply.
  2. Try this Query.

    @Query("value = "SELECT * FROM `Order` WHERE account=?1", nativeQuery = true")
    public List<OrderBean> findOrderByAccount(String account);
    

    you can also try to change the Table name from `Order` to just Order

    orderService

    public List<OrderBean> findOrderByAccount(String account) {
    
    List<OrderBean> orderBeanList = orderDAO.findOrderByAccount(account);
    String id = "";
    String key = "";
    for(OrderBean orderBean : orderBeanList) {
                
      key = orderBean.getOrderNumber();
    // or you can try System.out.println(orderBean.getOrderNumber);
    //output:null   null
      id = orderBean.getId();
    // or you can try System.out.println(orderBean.getId);
    //output:1   2
    }
    
    

    try this it might solve your error.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search