Java JPA BigInteger 和 Long 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20929979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:23:09  来源:igfitidea点击:

JPA BigInteger and Long issue

javahibernatejpalong-integerbiginteger

提问by Zlatko

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?

这里可能有什么问题?

采纳答案by Zlatko

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.

因此,我查询中的 company.getId() 是从 postgresql 中获取公司实体。在这个过程中,它隐式地将 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();

回答by Sabuj Hassan

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

在您的实体中,您拥有Long数字。您需要将它们更改为BigInteger. 您的 JPA 正在回报您BigInteger

Change these things like below:

像下面这样更改这些内容:

private Long id;

with

private BigInteger id;

回答by Neeraj

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.

这取决于您在实体中指定的类型以及表列中的内容。它必须相同。检查两者是否相同。