I've spent the last couple of hours trying to figure this out, mostly by searching since I figure someone has already done it, but I'm not finding an answer that works for me.
Here's an on-the-fly translation of my code to something I can put in public.
@Entity @Table(name="result")
public class Result implements Serializable {
@Embeddable
public static class ResultPK implements Serializable {
@Column(name="result_date", nullable=false)
@Type(type="com.example.HibernateUTC$LocalDateType") // <- UserType
public LocalDate resultDate;
@Column(name="name", nullable=false)
public String name;
@Column(name="category", nullable=false)
public String category;
public ResultPK() {}
public ResultPK(Date resultDate, String name, String category) {
this.resultDate = resultDate;
this.name = name;
this.category = category;
}
// ...more code for hashCode/equals, no setters or getters...
}
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name="resultDate", column=@Column(name="result_date", nullable = false)),
@AttributeOverride(name="name", column=@Column(name="name", nullable=false)),
@AttributeOverride(name="category", column=@Column(name="category", nullable = false)),
})
private ResultPK resultId;
@Column(name="r_square")
private Double rSq;
@Column(name="p_value")
private pValue;
// ... more code for other fields, setters, getters, but nothing else; vanilla pojo...
}
I have a DAO where queries are hiding; the method that I'm calling is this
@Repository("resultDAO")
public class ResultDAOImpl extends AbstractBaseDAO<Result> implements ResultDAO {
// boilerplate for intializing base class and other queries
@Override
public List<Result> findDateRange(String category, String name, LocalDate begDate, LocalDate endDate) {
EntityManager em = entityManagerFactory.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Result> q = cb.createQuery(Result.class);
Root<Result> root = q.from(Result.class);
Predicate catMatch = cb.equal(root.get("resultId.category"), category);
Predicate nameMatch = cb.equal(root.get("resultId.name"), name);
Predicate dateRange = cb.between(root.get("resultId.resultDate"), begDate, endDate);
q.select(root).where(cb.and(catMatch, nameMatch, dateRange));
return em.createQuery(q).getResultList();
}
}
When I attempt to run the code that executes that query, I end up with an error
Exception in thread "main" java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [resultId.category] on this ManagedType [com.example.Result]
Some of the similar questions I've found made it look like I needed to use the resultPK
or ResultPK
in the query. I've tried those, no joy. I have no idea how to specify the fields in the key for the query, or if I need something totaly different to this. I really need a clue...
I'm using Spring 4.3.8.RELEASE and Hibernate 4.3.11.Final, Java 8 (hence the UserType to handle LocalDate).
Edited to correct some inconsistencies in my transcription of the actual code.
You should modify your predicates this way:
Predicate catMatch = cb.equal(root.get("resultId").get("category"), category);
Predicate nameMatch = cb.equal(root.get("resultId").get("name"), name);
Predicate dateRange = cb.between(root.get("resultId").get("resultDate"), begDate, endDate);
By the way no need to use cb.and
inside of where
statement. You can make the code a bit shorter using
q.select(root).where(catMatch, nameMatch, dateRange);