Hibernate generates negative id values when using a sequence

The new behaviour is the followings: AllocationSize is a range of primary key values reserved for Hibernate. And the select seq.nextval from dual will be done only after hibernate consumed this range of primary keys. So you must declare the same value on both allocationSize (Hibernate) and sequence increment by (DB) When explicitly set allocationSize=500, … Read more

JPA Criteria builder IN clause query

This criteria set-up should do the trick: CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Employee> q = cb.createQuery(Employee.class); Root<Employee> root = q.from(Employee.class); q.select(root); List<String> parentList = Arrays.asList(new String[]{“John”, “Raj”}); Expression<String> parentExpression = root.get(Employee_.Parent); Predicate parentPredicate = parentExpression.in(parentList); q.where(parentPredicate); q.orderBy(cb.asc(root.get(Employee_.Parent)); q.getResultList(); I have used the overloaded CriteriaQuery.where method here which accepts a Predicate.. an in predicate in this case.

JPA 2.0: Adding entity classes to PersistenceUnit *from different jar* automatically

There are several way to solve it: As described in Do I need <class> elements in persistence.xml?, you can set hibernate.archive.autodetection property and Hibernate should be able to look up all annotated classes from classpath. However, that’s not JPA spec compliant. If you are using Spring, from Spring 3.1.2 (or probably even a bit earlier), … Read more

Silently ignored remove()

Take a look at this answer. Basically, JPA specification mandates that a removed entity becomes managed again if the persist operation is applied to it. To verify that this is really happening, enable the trace log level for org.hibernate package and search for log entries like: un-scheduling entity deletion … To avoid any unpredictable behaviour, … Read more

How do I properly cascade save a one-to-one, bidirectional relationship on primary key in Hibernate 3.6

Specification says that derived entity should be the owning side of the relationship: 2.4.1 Primary Keys Corresponding to Derived Identities The identity of an entity may be derived from the identity of another entity (the “parent” entity) when the former entity (the “dependent” entity) is the owner of a many-to-one or one-to-one relationship to the … Read more

In JPA 2, using a CriteriaQuery, how to count results

A query of type MyEntity is going to return MyEntity. You want a query for a Long. CriteriaBuilder qb = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = qb.createQuery(Long.class); cq.select(qb.count(cq.from(MyEntity.class))); cq.where(/*your stuff*/); return entityManager.createQuery(cq).getSingleResult(); Obviously you will want to build up your expression with whatever restrictions and groupings etc you skipped in the example.

JPQL Constructor Expression – org.hibernate.hql.ast.QuerySyntaxException:Table is not mapped

according to the book “Pro EJB 3 Java Persistence API” Constructor Expressions A more powerful form of SELECT clause involving multiple expressions is the constructor expression, which specifies that the results of the query are to be stored using a user-specified object type. Consider the following query: SELECT NEW example.EmployeeDetails(e.name, e.salary, e.department.name) FROM Employee e … Read more