gpt4 book ai didi

java - JPAController getReference BigDecimal 问题

转载 作者:行者123 更新时间:2023-12-02 00:38:17 25 4
gpt4 key购买 nike

我有一个 JPAController 类,它具有创建方法来在父表和详细表中保存记录。两个表中的主键均为 BigDecimal 类型,小数位数为 8,精度为 15。现在我的问题是,当我调用 create 方法时,我在 public T getReference(ClassEntityClass,ObjectprimaryKey)asBigDecimal 中得到错误,具有 3 个比例和 5 个精度,如果将 42.20112012 作为引用键,那么我得到的错误为,

Caused by: javax.persistence.EntityNotFoundException: Could not find entitiy for id: 42.201

我在JPAController类中的创建方法如下,我想在RuleApplGroupMst(父表)和RuleGroupRelation(明细表)表中插入记录,

        Collection<RuleGroupRelation> attachedRuleGroupRelationCollection = new ArrayList<RuleGroupRelation>();
for (RuleGroupRelation RuleGroupRelationCollectionRuleGroupRelationToAttach : RuleApplGroupMst.getRuleGroupRelationCollection()) {
RuleGroupRelationCollectionRuleGroupRelationToAttach = em.getReference(RuleGroupRelationCollectionRuleGroupRelationToAttach.getClass(),RuleGroupRelationCollectionRuleGroupRelationToAttach.getRgrSrgKey());
attachedRuleGroupRelationCollection.add(RuleGroupRelationCollectionRuleGroupRelationToAttach);
}

感谢您的帮助......

最佳答案

EntityManager.getReference依赖主键的相等性从数据库中获取相应的实体。当您选择 BigDecimal 作为主键时,您必须使用 BigDecimal 的精确表示来获取对实体的引用。您不能使用近似值。简而言之,按照 equals() 的定义BigDecimal的方法:

Compares this BigDecimal with the specified Object for equality. Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method).

EntityManager.getReference 中用作参数的主键值必须与存储的主键值的范围和值相匹配,以便允许 JPA 提供程序返回引用。

因此,您应该使用与数据库中最初存储的 42.201 相同的表示形式,以便成功检索实体。

关于java - JPAController getReference BigDecimal 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7156975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com