HQL 加入意外令牌

HQL join unexpected token

我有一些桌子:

Order(orderID,orderDate, orderItem (OneToMany) )

OrderItem( id, order(manyToOne) , book (manyToOne) )

Book (id, title, cost)

User(id,username,password,email)

我有 orderID=42 ,我需要获取它的 Books 信息和 userId

这是我的查询以加入此表:

from Order or inner join or.orderItems ori inner join ori.book where or.orderID=37

但是出现这个错误:

 unexpected token: or near line 1, column 26 [from com.obs.model.Order or inner join or.orderItems ori inner join ori.book where or.orderID=37]

更新

Book实体:

@Entity
public class Book implements Serializable {

    @Id
    @GeneratedValue
    private Integer id;
    @Column(nullable = false)
    private String title;
    @Column(nullable = false)
    private String author;
    @Column(nullable = false)
    private String isbn;
    private String description;
    @Column(nullable = false)
    private double cost;

Order实体:

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

@Id
@GeneratedValue
private Integer orderID;
@Column(nullable = false)
private Date orderDate;
@ManyToOne(cascade = CascadeType.ALL)
private User user;

@OneToMany(cascade = CascadeType.ALL)
private Set<OrderItem> orderItems = new HashSet<OrderItem>();

OrderItem实体:

@Entity
public class OrderItem implements Serializable {

@Id
@GeneratedValue
private Integer id;

@ManyToOne(cascade = CascadeType.ALL)
private Order order; // F.K to Order table

@ManyToOne(cascade = CascadeType.ALL)
private Book book; // F.K to Book table

@Column(nullable = false)
private int quantity;
@Column(nullable = false)
private double totalPrice;

User实体:

@Entity
public class User implements Serializable {

@Id
@GeneratedValue
private Integer id;
@Column(nullable = false)
private String username;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private String email;

订单项:

订单:

Book

您的 Order 和 OrderItem 之间的 OneToMany 双向关联不正确。你实际上有两个独立的协会。 ManyToOne 使用 order_OrderID 连接列,OneToMany 使用您未显示的附加连接 table。

在双向关联中,一侧始终是另一侧的反面。在 OneToMany 双向关联中,一方必须是反方:

@OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
private Set<OrderItem> orderItems = new HashSet<OrderItem>();

此外,请注意,在 ManyToOne 关联上使用 CascadeType.ALL 没有任何意义。您不想在删除 OrderItem 时删除 OrderItem 的书。这无论如何都行不通,因为其他 OrderItems 引用了同一本书。