JPAController getReference BigDecimal 问题

发布于 2024-12-01 10:21:04 字数 1090 浏览 3 评论 0 原文

我有一个 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);
        }

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

I have one JPAController Class that having create method to save record in Parent and Detail Table. The Primary key is of BigDecimal type with 8 scale and 15 precision in both the tables. Now my Question is When i call the create method i get the error in public T getReference(Class entity Class, Object primary Key) as BigDecimal with 3 scale and 5 Precision,if take 42.20112012 as reference key then i got error as,

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

My create method in JPAController Class is as below , in which i want to insert record in RuleApplGroupMst(Parent Table) and RuleGroupRelation(Detail Table) Tables,

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

Thanks for any help.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

影子是时光的心 2024-12-08 10:21:04

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

比较此 BigDecimal 与指定的 Object 是否相等。
与compareTo不同,此方法认为两个BigDecimal对象相等
仅当它们的值和比例相等时(因此 2.0 不等于
2.00(用此方法比较)。

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

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

EntityManager.getReference relies on the equality of primary keys to fetch the corresponding entity from the database. When you choose BigDecimals as your primary key, then you must use the exact representation of the BigDecimal to obtain a reference to an entity. You cannot use an appromixation. In simpler words, going by the definition of the equals() method of 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).

the primary key values used as the argument in EntityManager.getReference must match the stored primary key values in scale and in value, in order to allow the JPA provider to return the reference.

Therefore, you ought to use the same representation for 42.201 as originally stored in the database, in order to retrieve the entity successfully.

长安忆 2024-12-08 10:21:04

我得到了答案,

在我的例子中,我在 JPAController 类中创建了用户定义的方法,如下所示,

public void create_custom(RuleApplGroupMst RuleApplGroupMst) {
    EntityManager em = null;
    try {
        utx.begin();
        em = getEntityManager();
        em.persist(RuleApplGroupMst);
        utx.commit();
    }  catch (Exception ex) {}

然后在实体 Bean 中声明 OneToMany 关系,

@OneToMany(cascade = CascadeType.ALL,mappedBy = "ragmSrgKey")

然后我通过托管 bean 调用 create_custom 方法。

它适用于所有类型(包括 BigDecimal)的主键/引用键。

I got the answer,

In My Case, i have created the user defined method in JPAController Class as below,

public void create_custom(RuleApplGroupMst RuleApplGroupMst) {
    EntityManager em = null;
    try {
        utx.begin();
        em = getEntityManager();
        em.persist(RuleApplGroupMst);
        utx.commit();
    }  catch (Exception ex) {}

and then declare OneToMany Relationship in Entity Bean as,

@OneToMany(cascade = CascadeType.ALL,mappedBy = "ragmSrgKey")

and then i call create_custom method by managed bean.

It works with all type(including BigDecimal) of Primary/Reference Key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文