JPA Query.getResultList() – use in a generic way

General rule is the following: If select contains single expression and it’s an entity, then result is that entity If select contains single expression and it’s a primitive, then result is that primitive If select contains multiple expressions, then result is Object[] containing the corresponding primitives/entities So, in your case list is a List<Object[]>.

Why not to use Spring’s OpenEntityManagerInViewFilter

As you said, the OpenSessionInView filter is very convenient in web applications. Regarding the limitations you mentioned: 1) Loading several lazy associations will result in multiple database transactions, a possible hit on performance. Yes, going to the DB often might lead to performance problems. Ideally you want to fetch all the data you need in … Read more

Why is criteria query deprecated in Hibernate 5?

We are deprecating the Criteria API in lieu of JPA extension support. Consider this: CriteriaBuilder cb = entityManager.getCriteriaBuilder(); HibernateCriteria hc = cb.unwrap( HibernateCriteria.class ); … query.where( hc.someAwesomeThing( … ) ); List<SomeEntity> entities = entityManager.createQuery( query ).getResultList(); Contrary to comments, we do intend to continue to deliver Hibernate-specific features, but we want to introduce those through … Read more

How to rollback a database transaction when testing services with Spring in JUnit?

You need to extend transaction boundaries to the boundaries of your test method. You can do it by annotating your test method (or the whole test class) as @Transactional: @Test @Transactional public void testInsert(){ long id=myService.addPerson(“JUNIT”); assertNotNull(id); if(id<1){ fail(); } } You can also use this approach to ensure that data was correctly written before … Read more

Hibernate/Spring: failed to lazily initialize – no session or session was closed

I think you should not use the hibernate session transactional methods, but let spring do that. Add this to your spring conf: <bean id=”txManager” class=”org.springframework.orm.hibernate3.HibernateTransactionManager”> <property name=”sessionFactory” ref=”mySessionFactory” /> </bean> <bean id=”transactionTemplate” class=”org.springframework.transaction.support.TransactionTemplate”> <property name=”transactionManager” ref=”txManager”/> </bean> and then I would modify your test method to use the spring transaction template: public static void main(String[] … Read more

Best configuration of c3p0 [closed]

This is a configuration I am using which keeps resources to a minimum. Of course you’ll want to tailor your application to use the resources it needs… Reference: http://www.mchange.com/projects/c3p0/index.html testConnectionOnCheckin validates the connection when it is returned to the pool. testConnectionOnCheckOut, although would ensure active connections before use, would be too expensive to do. idleConnectionTestPeriod … Read more

Catch duplicate entry Exception

I use spring so we resolve it by org.springframework.dao.DataIntegrityViolationException try { ao_history_repository.save(new AoHistory(..)); } catch (DataIntegrityViolationException e) { System.out.println(“history already exist”); } But as @KevinGuancheDarias mention it: Please note that while this works. I suggest to solve the problem by issuing a findBy before the save, as this is messy, and I think it’s not … Read more

Spring Data JPA: How can Query return Non- Entities Objects or List of Objects?

You can do something like @NamedQuery(name=”findWhatever”, query=”SELECT new path.to.dto.MyDto(e.id, e.otherProperty) FROM Student e WHERE e.id = ?1″) Then the MyDto object would just need a constructor defined with the correct fields i.e. public MyDto(String id, String otherProperty) { this.id = id; this.otherProperty = otherProperty; }

Jackson: Multiple back-reference properties with name ‘defaultReference’

If you use @JsonBackReference on more than one getter/setter method in your project, you should distinguish them with a specific reference name. Maybe only one ‘defaultReference’ is allowed in the latest version? e.g In MovementView.java @JsonBackReference(value=”user-movement”) public User getUser() { return user; } In User.java @JsonManagedReference(value=”user-movement”) public MovementView getMovementView() { return movementView; }