JPA CriteryQuery 内部联接 - IllegalStateException
JPA CriteryQuery inner join - IllegalStateException
我在 JPA 中有以下 qriteria 查询:
CriteriaBuilder cb2 = entityMager.getCriteriaBuilder();
CriteriaQuery<DemoUser> cqInnerJoin = cb2.createQuery(DemoUser.class);
Root<DemoUser> root = cqInnerJoin.from(DemoUser.class);
Join<DemoUser, DemoOrder> join = root.join("userId", JoinType.INNER);
但抛出异常:
Exception in thread "main" java.lang.IllegalStateException: CAN_NOT_JOIN_TO_BASIC (There is no English translation for this message.)
at org.eclipse.persistence.internal.jpa.querydef.FromImpl.join(FromImpl.java:354)
at org.eclipse.persistence.internal.jpa.querydef.FromImpl.join(FromImpl.java:513)
at jpaTest.jpaTest.main(jpaTest.java:144)
我从 Oracle 数据库表中生成了这些 classes。我的代码有问题吗?
我在 TypedQuery 中尝试过,但它显示相同的错误消息。
生成的classes:
DemoUser.class
@Entity
@Table(name="DEMO_USERS")
public class DemoUser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="USER_ID")
private long userId;
//bi-directional many-to-one association to DemoOrder
@OneToMany(mappedBy="demoUser")
//@JoinColumn(name = "USER_ID")
private List<DemoOrder> demoOrders;
演示订单class
@Entity
@Table(name="DEMO_ORDERS")
public class DemoOrder implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ORDER_ID")
private long orderId;
@Temporal(TemporalType.DATE)
@Column(name="ORDER_TIMESTAMP")
private Date orderTimestamp;
@Column(name="ORDER_TOTAL")
private BigDecimal orderTotal;
//bi-directional many-to-one association to DemoCustomer
@ManyToOne
@JoinColumn(name="CUSTOMER_ID")
private DemoCustomer demoCustomer;
//bi-directional many-to-one association to DemoUser
@ManyToOne
@JoinColumn(name="USER_ID")
private DemoUser demoUser;
//bi-directional many-to-one association to DemoOrderItem
@OneToMany(mappedBy="demoOrder")
private List<DemoOrderItem> demoOrderItems;
您不能加入基本类型(long)的属性"userId"。对于单个属性,您可以加入实体或可嵌入类型。
当需要加入时,您应该始终注意在 协会 上进行。 DemoOrder 和 DemoUser 之间的关联由字段 demoUser 映射(因为您已将 DemoUser 设为 root),所以您应该这样做:
Join<DemoUser, DemoOrder> join = root.join("demoUser", JoinType.INNER);
我在 JPA 中有以下 qriteria 查询:
CriteriaBuilder cb2 = entityMager.getCriteriaBuilder();
CriteriaQuery<DemoUser> cqInnerJoin = cb2.createQuery(DemoUser.class);
Root<DemoUser> root = cqInnerJoin.from(DemoUser.class);
Join<DemoUser, DemoOrder> join = root.join("userId", JoinType.INNER);
但抛出异常:
Exception in thread "main" java.lang.IllegalStateException: CAN_NOT_JOIN_TO_BASIC (There is no English translation for this message.)
at org.eclipse.persistence.internal.jpa.querydef.FromImpl.join(FromImpl.java:354)
at org.eclipse.persistence.internal.jpa.querydef.FromImpl.join(FromImpl.java:513)
at jpaTest.jpaTest.main(jpaTest.java:144)
我从 Oracle 数据库表中生成了这些 classes。我的代码有问题吗?
我在 TypedQuery 中尝试过,但它显示相同的错误消息。
生成的classes:
DemoUser.class
@Entity
@Table(name="DEMO_USERS")
public class DemoUser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="USER_ID")
private long userId;
//bi-directional many-to-one association to DemoOrder
@OneToMany(mappedBy="demoUser")
//@JoinColumn(name = "USER_ID")
private List<DemoOrder> demoOrders;
演示订单class
@Entity
@Table(name="DEMO_ORDERS")
public class DemoOrder implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ORDER_ID")
private long orderId;
@Temporal(TemporalType.DATE)
@Column(name="ORDER_TIMESTAMP")
private Date orderTimestamp;
@Column(name="ORDER_TOTAL")
private BigDecimal orderTotal;
//bi-directional many-to-one association to DemoCustomer
@ManyToOne
@JoinColumn(name="CUSTOMER_ID")
private DemoCustomer demoCustomer;
//bi-directional many-to-one association to DemoUser
@ManyToOne
@JoinColumn(name="USER_ID")
private DemoUser demoUser;
//bi-directional many-to-one association to DemoOrderItem
@OneToMany(mappedBy="demoOrder")
private List<DemoOrderItem> demoOrderItems;
您不能加入基本类型(long)的属性"userId"。对于单个属性,您可以加入实体或可嵌入类型。
当需要加入时,您应该始终注意在 协会 上进行。 DemoOrder 和 DemoUser 之间的关联由字段 demoUser 映射(因为您已将 DemoUser 设为 root),所以您应该这样做:
Join<DemoUser, DemoOrder> join = root.join("demoUser", JoinType.INNER);