Hi all,
I am new to hibernate forum (but not to hibernate, I am using it since last 5 years).
Recently, we are trying to migrate our code from hibernate 4.x to hibernate 5.2. After adjusting to new naming strategy changes in 5.x, and few JPQL specific changes, migration went smooth.
However, one issue is troubling us a lot. The code where it happens is complex but let me explain it with a simple example.
We have following entity:
Code:
@Entity
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// getter/setter and other fields
}
In one of our generic service, we have following code (this service works with all entities, but here I am showing it for Company entity only).
Code:
Object param = 1; // as it's generic service, we can pass any param type
Query query = em.createQuery("SELECT c FROM Company c WHERE c.id = :id");
try {
query.setParameter("id", param); // we try to pass INTEGER here, while "id" field is of type "Long"
} catch (IllegalArgumentException e) {
// in hibernate 4.x, we get here because of type mismatch
// and we adjust the type from parameter type info
query.setParameter("id", Long.parseLong(param.toString()));
}
However, with hibernate 5.2, it's not the case. The
Query#setParameter is not throwing
IllegalArgumentException if parameter type mismatch. According to JPA spec, it
should throw IllegalArgumentException.
Hibernate 5.2 throws
IllegalArgumentException only when we execute the query. I think this is a bug in hibernate 5.2 and should be fixed. Or may be I am missing something. Is there any way in hibernate 5.2 to force query param validation at the time of setting it?