Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 尝试按crudrepository中关联实体的字段排序时出现spring数据错误_Jpa_Spring Boot_Spring Data_Spring Data Jpa - Fatal编程技术网

Jpa 尝试按crudrepository中关联实体的字段排序时出现spring数据错误

Jpa 尝试按crudrepository中关联实体的字段排序时出现spring数据错误,jpa,spring-boot,spring-data,spring-data-jpa,Jpa,Spring Boot,Spring Data,Spring Data Jpa,我在Mysql中使用springboot和springdata 我有两个实体,客户和订单: @Entity @Table(name = "customers") public class Customer { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) @Column(name="id", nullable = false) protected long id; @Column(name

我在Mysql中使用springboot和springdata

我有两个实体,客户和订单:

@Entity
@Table(name = "customers")
public class Customer {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="id", nullable = false)
    protected long id;

    @Column(name = "name")
    private String name;
}

@Entity
@Table(name = "orders")
public class Order {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="id", nullable = false)
    protected long id;

    @Column(name="customer_id")
    private long customerId;
}
我还有一个存储库:

@Repository
public interface OrdersRepository extends JpaRepository<Order, Long> {

    @Query("select o from Order o, Customer c where o.customerId = c.id")
    Page<Order> searchOrders(final Pageable pageable);
}
但是,按顺序字段排序效果良好:

Sort sort = new Sort(Sort.Direction.ASC, "id");
ordersRepository.search(new PageRequest(x, y, sort));
我得到的错误是c不是Order属性(但由于查询是实体的连接,我希望它能够工作)

您知道我如何按照联接实体的字段进行排序吗


谢谢您

在JPA中,您使用的排序对象必须是select语句中返回的对象,您不能使用未返回的属性进行排序

您收到了错误,因为关系建模不正确。在您的情况下,这是一种
manytone
关系。我可以推荐这本书进一步阅读

@Entity
@Table(name = "orders")
public class Order {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="id", nullable = false)
    protected long id;

    @ManyToOne
    @JoinColumn(name="customer_id", referencedColumnName = "id")
    private Customer customer;
}
不再需要查询,因为将获取客户

@Repository
public interface OrdersRepository extends PagingAndSortingRepository<Order, Long> {
}
@Entity
@Table(name = "orders")
public class Order {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="id", nullable = false)
    protected long id;

    @ManyToOne
    @JoinColumn(name="customer_id", referencedColumnName = "id")
    private Customer customer;
}
@Repository
public interface OrdersRepository extends PagingAndSortingRepository<Order, Long> {
}
Sort sort = new Sort(Sort.Direction.ASC, "customer.name");
ordersRepository.findAll(new PageRequest(x, y, sort));