简体   繁体   English

JPA BigInteger和长期发行

[英]JPA BigInteger and Long issue

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. offer.companyID和company.id都很长。

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? 这可能是什么问题?

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. 因此,我查询中的company.getId()是从postgresql获取公司实体。 And in that process, it cast id into 'BigInteger', implicitly. 然后在该过程中,将ID隐式地转换为“ BigInteger”。

So what I had to do was explicit cast to Long, and now my method looks like this: 所以我要做的是显式转换为Long,现在我的方法如下所示:

    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();

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. 在您的实体中,您Long得到数字。 You need to change those into BigInteger . 您需要将其更改为BigInteger Your JPA is returning you BigInteger . 您的JPA将返回BigInteger

Change these things like below: 更改以下内容:

private Long id;

with

private BigInteger id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM