How to select just the foreign key value using Criteria Query?

You need to join to B and then fetch the id:

Path<Integer> bId = root.join("b").get("id");

You can declare the foreign key in class A where "B_ID" is the name of the foreign key column in table A. And then you can root.get("bId") in your criteriabuilder example above. I have the same problem as you and this is working for me.

@Column(name="B_ID", insertable=false, updatable=false)
private int bId;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "B_ID")
private B b;