I have the following qriteria query in JPA:
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. Is there something wrong with my code?
I tried this in TypedQuery, but it displayed the same error message.
The generated 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;
DemoOrder 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;
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:
Join<DemoUser, DemoOrder> join = root.join("demoUser", JoinType.INNER);