外键上的 JPQL JOIN 导致 SQLSyntaxErrorException
JPQL JOIN on Foreign Key causes SQLSyntaxErrorException
我有两个实体 Order
和 Customer
。 Order
与 Customer
有 @ManyToOne
关系。但是,当我声明以下 JPQL 查询时,我得到一个 SQLSyntaxErrorException
,因为生成的 SQL 缺少 ON
条件。
JPQL:
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
。我觉得很奇怪,因为这些是我的实体:
Order(为了简洁省略不相关的属性)
@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;
}
}
Customer(为简洁起见省略不相关的属性)
我的用例不要求我在 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;
}
}
这两个实体之间的关系是正确的,因为它在通过正常方式插入、更新和检索时按预期工作。我在这里遗漏了什么吗?
尝试使用以下jpql:
select o from Order o join o.customer
另见 documantation。
我有两个实体 Order
和 Customer
。 Order
与 Customer
有 @ManyToOne
关系。但是,当我声明以下 JPQL 查询时,我得到一个 SQLSyntaxErrorException
,因为生成的 SQL 缺少 ON
条件。
JPQL:
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
。我觉得很奇怪,因为这些是我的实体:
Order(为了简洁省略不相关的属性)
@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;
}
}
Customer(为简洁起见省略不相关的属性)
我的用例不要求我在 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;
}
}
这两个实体之间的关系是正确的,因为它在通过正常方式插入、更新和检索时按预期工作。我在这里遗漏了什么吗?
尝试使用以下jpql:
select o from Order o join o.customer
另见 documantation。