繁体   English   中英

JPA BigInteger和长期发行

[英]JPA BigInteger and Long issue

我有一段这样的代码:

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

现在,这看起来不错,但给我带来了麻烦。 offer.companyID和company.id都很长。

这是日志显示我休眠状态的内容:

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

这是我得到的错误:

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

这可能是什么问题?

好吧,我想我已经找到了答案。

我显示的代码上方一行是

Company company = companyRepo.getUsersCompany(memberId);

因此,我查询中的company.getId()是从postgresql获取公司实体。 然后在该过程中,将ID隐式地转换为“ BigInteger”。

所以我要做的是显式转换为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();

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

在您的实体中,您Long得到数字。 您需要将其更改为BigInteger 您的JPA将返回BigInteger

更改以下内容:

private Long id;

private BigInteger id;

暂无
暂无

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

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