Hi
I'm using JPA 2.0 with hibernate 3.5.3 and using hibernate filters to filter a formula query. When i use
Code:
em.find(TestEntity.class, 1L)
the filter is applied correctly but when i use
Code:
entityManager.createQuery("from TestEntity").getSingleResult()
filter parameters are not applied to the query and i get an error because generated native sql query expects two parameters(limit parameter to obtain one value and filter parameter) and hibernate only defines one.
I'm using log4jdbc to debug the queries that are being generated and i can confirm these problems.
Entity code:
Code:
@Entity
@FilterDef(name = "test", parameters = @ParamDef(name = "param", type = "java.lang.String"))
public class TestEntity {
@Id
private Long id;
@Formula("select id from TestEntity where :test.param like 'es'")
private Long value;
}
Test code:
Code:
public class MainTest {
@Test
public void testFail() {
final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("SamplePU");
final EntityManager entityManager = entityManagerFactory.createEntityManager();
((Session) entityManager.getDelegate()).enableFilter("test").setParameter("param", "param1");
entityManager.createQuery("from TestEntity").getSingleResult();
// entityManager.find(TestEntity.class, 1L);
}
}
persistence.xml
Code:
<persistence-unit name="SamplePU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="net.sf.log4jdbc.DriverSpy"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<!--<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:memdb"/>-->
<property name="hibernate.connection.url" value="jdbc:log4jdbc:h2:mem:memdb;DB_CLOSE_DELAY=-1"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
This is what log4jdbc prints:
Code:
22-jul-2010 9:06:58 net.sf.log4jdbc.Slf4jSpyLogDelegator methodReturned
INFO: 1. Connection.prepareStatement(select testentity0_.id as id0_, select testentity0_.id from TestEntity where ? like 'es' as formula0_ from TestEntity testentity0_ limit ?) returned net.sf.log4jdbc.PreparedStatementSpy@1d9e2c7
22-jul-2010 9:06:58 net.sf.log4jdbc.Slf4jSpyLogDelegator methodReturned
INFO: 1. PreparedStatement.setInt(1, 2) returned
22-jul-2010 9:06:58 net.sf.log4jdbc.Slf4jSpyLogDelegator sqlOccured
INFO: select testentity0_.id as id0_, select testentity0_.id from TestEntity where 2 like 'es' as
formula0_ from TestEntity testentity0_ limit ?
22-jul-2010 9:06:58 net.sf.log4jdbc.Slf4jSpyLogDelegator exceptionOccured
GRAVE: 1. PreparedStatement.executeQuery() select testentity0_.id as id0_, select testentity0_.id from TestEntity where 2 like 'es' as formula0_ from TestEntity testentity0_ limit ?
org.h2.jdbc.JdbcSQLException: Parametro "#2" no está fijado
Parameter "#2" is not set; SQL statement: