分享
三行代码  ›  专栏  ›  技术社区  ›  MorganGlam

外键上的JPQL联接导致SQLSyntaxErrorException

  •  0
  • MorganGlam  · 技术社区  · 1 周前

    我有两个实体 Order Customer . 命令 有一个 @ManyToOne 与…的关系 客户 SQLSyntaxErrorException 因为生成的SQL缺少 ON 条件。

    SELECT o.id,
           o.incrementId,
           o.externalId,
           o.state,
           o.status,
           o.couponCode,
           o.totalDiscount,
           o.total,
           o.originChannel,
           o.branchOffice,
           o.createdAt,
           o.updatedAt,
           c.fullName
    FROM Order o
             JOIN FETCH Customer c;
    

    生成的SQL:

    select order0_.id             as col_0_0_,
           order0_.increment_id   as col_1_0_,
           order0_.external_id    as col_2_0_,
           order0_.state          as col_3_0_,
           order0_.status         as col_4_0_,
           order0_.coupon_code    as col_5_0_,
           order0_.total_discount as col_6_0_,
           order0_.total          as col_7_0_,
           order0_.origin_channel as col_8_0_,
           order0_.branch_office  as col_9_0_,
           order0_.created_at     as col_10_0_,
           order0_.updated_at     as col_11_0_,
           customer1_.full_name   as col_12_0_
    from orders order0_
             inner join customers customer1_ on;
    

    如你所见,它缺少条件 on

    命令

    @Entity
    @Table(name = "orders")
    public class Order {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;
    
        @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
        @JoinColumn(name = "customer_id", referencedColumnName = "id", table = "orders")
        private Customer customer;
    
        public Customer getCustomer() {
            return customer;
        }
    }
    

    客户 (为简洁起见,省略无关属性)

    我的用例不要求我有 List<Order> 实体

    @Entity
    @Table(name = "customers")
    public class Customer {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    }
    

    1 回复  |  直到 1 周前
        1
  •  1
  •   SternK    1 周前

    尝试使用以下jpql:

    select o from Order o join o.customer
    

    documantation