Question

I have a piece of code like this:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Offer> query = cb.createQuery(Offer.class);
Root<Offer> root = query.from(Offer.class);
query = query.select(root).where(cb.equal(root.get(Offer_.companyID), company.getId()));
return em.createQuery(query).getResultList()

Now, this looks nice and dandy, but it gives me problems. Both offer.companyID and company.id are long.

Here's the what log shows me that hibernate is doing:

 Hibernate: SELECT C.id FROM company C INNER JOIN company_operators cm
 ON cm.company_id = c.id WHERE cm.operators_id = '503'

And this is the error I get:

12:47:00,581 ERROR [org.jboss.ejb3.invocation] (http--0.0.0.0-9080-5) JBAS014134: EJB Invocation failed on component OfferRepository for method public java.util.List org.jboss.tools.example.richfaces.data.OfferRepository.getOffersbyMemberId(java.lang.Long) throws java.lang.Exception: javax.ejb.EJBException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

What could be the problem here?

Was it helpful?

Solution

Well, I think I have found the answer.

One line above the code I have shown was

Company company = companyRepo.getUsersCompany(memberId);

Hence, company.getId() in my query was fetching company entity from postgresql. And in that process, it cast id into 'BigInteger', implicitly.

So what I had to do was explicit cast to Long, and now my method looks like this:

    Company company = companyRepo.getUsersCompany(memberId);
    if (company == null) return null;
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Offer> query = cb.createQuery(Offer.class);
    Root<Offer> root = query.from(Offer.class);
    Long companyId = company.getId();
    query = query.select(root).where(cb.equal(root.get(Offer_.companyID), companyId));
    return em.createQuery(query).getResultList();

OTHER TIPS

It depends on what type you have specified in your entity and in what is in table column. It must be same. Check if both are same.

In your entity, you have Long for the numbers. You need to change those into BigInteger. Your JPA is returning you BigInteger.

Change these things like below:

private Long id;

with

private BigInteger id;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top