0
votes

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 :)

1
Has your table been recreated since you changed the ID value from Integer to Long? - wxkevin
no, in my table id is BigSerial. - Thiago Maltempi

1 Answers

1
votes

Postgres usually works with Serial (Integer) or BigSerial(Long) and also generates the sequence automatically

Try these approaches:

  1. Change your @GeneratedValue to @GeneratedValue(strategy=GenerationType.IDENTITY) and see if it works.
  2. Create a SequenceGenerator like this

For the second Option

SequenceGenerator

@SequenceGenerator(name = "tbl_costumer_id_gen", sequenceName = "tbl_costumer_id_gen",allocationSize=1) 
@Id 
@GeneratedValue(GenerationType.SEQUENCE, generator="tbl_costumer_id_gen") 
@Column(name="id") 
private Long id;