Is there any way to get a JPA2 CriteriaQuery to play nice with @NaturalId or does that just automatically happen?
I'm using Hibernate 3.6.5 with JPA2 type-safe queries, all of my objects have @NaturalId field(s) which are used quite a bit in the application to lookup the objects. I know for the Hibernate critery query you have to tell the query builder that you are doing a natural id query but I can't see a way to do that via the JPA2 CriteriaQuery API.
Data object snippet:
Code:
class PortletDefinitionImpl implements IPortletDefinition {
@Id
@GeneratedValue(generator = "UP_PORTLET_DEF_GEN")
@Column(name = "PORTLET_DEF_ID")
private final long internalPortletDefinitionId;
@NaturalId
@Column(name = "PORTLET_FNAME", length = 255, nullable = false)
private String fname;
...
}
DAO Query Creation snippet:
Code:
final CriteriaQuery<PortletDefinitionImpl> criteriaQuery = cb.createQuery(PortletDefinitionImpl.class);
final Root<PortletDefinitionImpl> definitionRoot = criteriaQuery.from(PortletDefinitionImpl.class);
criteriaQuery.select(definitionRoot);
criteriaQuery.where(
cb.equal(definitionRoot.get(PortletDefinitionImpl_.fname), this.fnameParameter)
);
Does hibernate just figure out that this is a query using the NaturalId or is there more I need to do? I don't see any related hints in QueryHints.