Hi, I am getting a exception when trying to use query by example for a simple component. I haven't get this before, and I could not see where is the error. I am using Hibernate Annotations support for tests purpose. The information about error is below:
Hibernate version: 3.0.5
Hibernate Annotations: 3 beta2
Mapping documents:
Code:
@Entity( access = AccessType.FIELD )
@Inheritance( strategy = InheritanceType.JOINED )
@NamedQueries( {
@NamedQuery( name = "People.findByEmail",
queryString = "select p from People p where p.info.email = ?" ),
@NamedQuery( name = "People.findByState",
queryString = "select p from People p where p.address.state.id = ?" ),
@NamedQuery( name = "People.findByCity",
queryString = "select p from People p where p.address.city like ?" )
}
)
public class People implements Identifiable<Long> {
@Id( generate = GeneratorType.AUTO )
@Column( name = "people_id" )
private Long id;
private String cpf;
private String nickname;
@Embedded
private ContactInfo info;
@Embedded
private Address address;
// equals, hashCode, toString, contructor, get and sets
}
Code between sessionFactory.openSession() and session.close():The DAO method:
Code:
public List<People> find( final People people ) throws DataAccessException {
HibernateCallback callback = new HibernateCallback() {
public Object doInHibernate( Session s ) throws HibernateException {
Criteria criteria = s.createCriteria(People.class);
Example example = Example.create(people);
example.enableLike(MatchMode.ANYWHERE);
example.excludeZeroes();
criteria.add(example);
if (people.getInfo() != null) {
Example infoExample = Example.create(people.getInfo());
infoExample.enableLike(MatchMode.ANYWHERE);
infoExample.excludeZeroes();
criteria.createCriteria("info").add(infoExample);
}
if (people.getAddress() != null) {
Example addrExample = Example.create(people.getAddress());
addrExample.enableLike(MatchMode.ANYWHERE);
addrExample.excludeZeroes();
criteria.createCriteria("address").add(addrExample);
}
return criteria.list();
}
};
List<People> result;
result = CallbackExecutor.executeFind(getHibernateTemplate(), callback);
return result;
}
And test case:
Code:
public void testFind() throws DataAccessException {
People example = new People();
example.setNickname("benz");
ContactInfo ci = new ContactInfo();
ci.setName("bruno");
Address a = new Address();
a.setDistrict("madalena");
example.setInfo(ci);
example.setAddress(a);
List<People> peoples = peopleDao.find(example);
assertNotNull(peoples);
Assert.assertFalse(peoples.isEmpty());
assertNotNull(peoples.get(0).getInfo());
}
Full stack trace of any exception that occurs:Code:
java.lang.ClassCastException: br.ufpe.liber.theses.model.ContactInfo
at org.hibernate.criterion.Example.getEntityMode(Example.java:234)
at org.hibernate.criterion.Example.toSqlString(Example.java:164)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:314)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1303)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
at br.ufpe.liber.theses.dao.hibernate.HibernatePeopleDao$1.doInHibernate(HibernatePeopleDao.java:93)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:312)
at org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:292)
at br.ufpe.liber.dao.hibernate.CallbackExecutor.executeFind(CallbackExecutor.java:48)
at br.ufpe.liber.theses.dao.hibernate.HibernatePeopleDao.find(HibernatePeopleDao.java:100)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:288)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:155)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy25.find(Unknown Source)
at br.ufpe.liber.theses.test.dao.PeopleDAOTest.testFind(PeopleDAOTest.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Name and version of the database you are using:MySQL 4.0.24, for Win32
Someone has a idea about what is going on?
best regards