[英]JPA CriteryQuery inner join - IllegalStateException
I have the following qriteria query in JPA: 我在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);
But it throws exception: 但这会引发异常:
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)
I generated these classes from Oracle database tables. 我从Oracle数据库表生成了这些类。 Is there something wrong with my code? 我的代码有问题吗?
I tried this in TypedQuery, but it displayed the same error message. 我在TypedQuery中尝试过此方法,但它显示了相同的错误消息。
The generated classes: 生成的类:
DemoUser.class 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;
DemoOrder class DemoOrder类
@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;
When it is required to join, you should always be aware to do it on an association . 当需要加入时,您应该始终知道要在关联上进行加入。 The association between DemoOrder and DemoUser is mapped by the field demoUser (as you have taken DemoUser as root ),so here is what you should do: DemoOrder和DemoUser之间的关联由字段demoUser映射(因为您已将DemoUser用作root ),因此应执行以下操作:
Join<DemoUser, DemoOrder> join = root.join("demoUser", JoinType.INNER);
You can not join to the attribute "userId" that is of a basic type(long). 您不能加入基本类型(长整数)的属性“ userId”。 For a singular attribute, you can join to an Entity or Embeddable type. 对于单数属性,可以加入Entity或Embeddable类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.