【问题标题】:Conflicting getters for com.google.type.Money - Javacom.google.type.Money 的 getter 冲突 - Java
【发布时间】:2021-11-30 15:43:42
【问题描述】:

我正在构建一个与 Firestore 一起运行的应用程序,但我遇到了资金问题。

由于 Firebase 不支持 BigDecimal,我正在尝试使用 Money 类。但是显示错误java.lang.RuntimeException: Found conflicting getters for name getDefaultInstanceForType on class com.google.type.Money

我访问了 Money 类的 Decompiled 文件,实际上有两个 getter:

In the decumentation这个类只有一个方法,所以我不知道如何进行。

这是从我的 IntelliJ IDEA 反编译的 Money 类中的问题:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.google.type;

.... HIDDEN FIELDS ....

public final class Money extends GeneratedMessageV3 implements MoneyOrBuilder {


 .... HIDDEN FIELDS BY ME ....

    public Money getDefaultInstanceForType() {
        return DEFAULT_INSTANCE;
    }

.... MORE HIDDEN FIELDS ....

        public Money getDefaultInstanceForType() {
            return Money.getDefaultInstance();
        }
       
      
.... AND MORE HIDDEN FIELDS ...

    }
}

如果你想看全班,here is a link to a notion page

我该怎么办?

哦,这是我的模型(根据隐私条款,我隐藏了一些部分):

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Document(collectionName = "ModelClassCollection")
public class MyModelClass {

    @DocumentId
    private String id;

 .... HIDDEN FIELDS  ....

    private Money total;

    private Money municipalTax;

    private Money federalTax;
}

【问题讨论】:

  • 请编辑您的问题并添加您的Money 课程的内容。请编辑您的问题并将您的数据库结构添加为屏幕截图。
  • @AlexMamo 你好,我已经应用了更改。可以看看吗?
  • 那里发生了太多事情。您应该隔离问题。
  • @AlexMamo 好吧,我已经隔离了它。可以看看吗?
  • 你的数据库是什么样子的?

标签: java firebase google-cloud-platform google-cloud-firestore types


【解决方案1】:

唯一的问题是您不会在class Money 中添加注释,也不会扩展final class Money。这意味着,您不能只学习一些听起来很相似的随机课程,然后期望它可以施放。 Firestore 没有类型转换器,但仍然可以覆盖 DocumentSnapshot.toObject()... 并且货币值通常可以安全地存储,例如。如Integer cents,按需转换为BigDecimal

【讨论】:

  • 马丁,我不明白。我不想覆盖 Money 类。您是想说 Money 类不能用于处理金钱吗?在这里,我们试图不以美分存储钱,但如果没有任何效果,我们将使用它。我的问题是为什么 Money 类中有两种具有相同签名的方法?而如果不推荐 Money 类来存储 Money,那它为什么存在呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 2014-04-25
  • 2012-12-23
  • 1970-01-01
  • 1970-01-01
  • 2012-10-06
  • 1970-01-01
相关资源
最近更新 更多