Hibernate sequence on oracle, @GeneratedValue(strategy = GenerationType.AUTO)

These Annotations are no creating two sequences, only one. Is this correct/expected? That’s the expected behavior. When using @GeneratedValue(strategy = GenerationType.AUTO), the JPA provider will pick an appropriate strategy for the particular database. In the case of Oracle, this will be SEQUENCE and, since you did not specify anything, Hibernate will use a single global … Read more

Hibernate Criteria for Dates

Why do you use Restrictions.like(…)? You should use Restrictions.eq(…). Note you can also use .le, .lt, .ge, .gt on date objects as comparison operators. LIKE operator is not appropriate for this case since LIKE is useful when you want to match results according to partial content of a column. Please see http://www.sql-tutorial.net/SQL-LIKE.asp for the reference. … Read more

How to add Distinct in Hibernate Criteria

Use Projections.distinct. Criteria crit = session.createCriteria(Test.class).setProjection( Projections.distinct(Projections.projectionList() .add(Projections.property(“type”), “type”) ) .setResultTransformer(Transformers.aliasToBean(YourBean.class)); List lst = crit.list(); where YourBean.class has a property “type”. The returned list will be List<YourBean>.

Persist collection of interface using Hibernate

JPA annotations are not supported on interfaces. From Java Persistence with Hibernate (p.210): Note that the JPA specification doesn’t support any mapping annotation on an interface! This will be resolved in a future version of the specification; when you read this book, it will probably be possible with Hibernate Annotations. A possible solution would be … Read more

Spring data : CrudRepository’s save method and update

I wanted to know if the {save} method in CrudRepository do an update if it finds already the entry in the database The Spring documentation about it is not precise : Saves a given entity. Use the returned instance for further operations as the save operation might have changed the entity instance completely. But as … Read more

Spring JpaRepository – Detach and Attach entity

If you are using JPA 2.0, you can use EntityManager#detach() to detach a single entity from persistence context. Also, Hibernate has a Session#evict() which serves the same purpose. Since JpaRepository doesn’t provide this functionality itself, you can add a custom implementation to it, something like this public interface UserRepositoryCustom { … void detachUser(User u); … … Read more

When are connections returned to the connection pool with Spring JPA (Hibernate) Entity Manager?

It’s not complicated at all. First, you need to understand that the Spring transaction manager is only a transaction management abstraction. In your case, the actual transactions happen at the JDBC Connection level. All @Transactional service method calls are intercepted by the TransactionInterceptor Aspect. The TransactionIntreceptor delegates transaction management to the current configured AbstractPlatformTransactionManager implementation … Read more

Doing an “IN” query with Hibernate

The syntax of your JPQL query is incorrect. Either use (with a positional parameter): List<Long> ids = Arrays.asList(380L, 382L, 386L); Query query = em.createQuery(“FROM TrackedItem item WHERE item.id IN (?1)”); query.setParameterList(1, ids) List<TrackedItem> items = query.getResultList(); Or (with a named parameter): List<Long> ids = Arrays.asList(380L, 382L, 386L); Query query = em.createQuery(“FROM TrackedItem item WHERE item.id … Read more

hibernate could not get next sequence value

Hibernate’s PostgreSQL dialect isn’t very bright. It doesn’t know about your per-SERIAL sequences, and is assuming there’s a global database-wide sequence called “hibernate_sequence” that it can use. (UPDATE: It appears that newer Hibernate versions may use the default per-table sequences when GenerationType.IDENTITY is specified. Test your version and use this instead of the below if … Read more