I'm using Hibernate 4.2, and PostgreSQL.
I have this table in postgres:
id bigserial NOT NULL (chave primária)
name text
And my entity is:
@Entity @Table(name="customer") public class Customer { @Id @GeneratedValue() @Column(name="id") private Long id;
@Column(name="name") private String name; //getters and setters }
And in my class (CustomerDAO), i have a method that does a search by criteria.
public List<Customer> getByName(String name){
Session session = HibernateUtil.getSessionFactory().openSession();
Criteria selection = selection.createCriteria(Customer.class);
selection.add(Restrictions.eq("name", name));
return selection.list(); //error here!
}
When I wrote the entity, I've set the id as an Integer, but, I changed from int to Long, and began appearing this error:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
at org.hibernate.type.descriptor.java.LongTypeDescriptor.unwrap(LongTypeDescriptor.java:36)
at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$1.doBind(BigIntTypeDescriptor.java:57)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:93)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:275)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1969)
at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1940)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1875)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1816)
at org.hibernate.loader.Loader.doQuery(Loader.java:900)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
at org.hibernate.loader.Loader.doList(Loader.java:2526)
at org.hibernate.loader.Loader.doList(Loader.java:2512)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342)
at org.hibernate.loader.Loader.list(Loader.java:2337)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:124)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1662)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
at com.teste.hibernate.dao.CustomerDAO.getByName(CustomerDAO.java:77)
at Teste.main(Teste.java:44)
Thanks :)