如何在数据库中存储Map

huangapple go评论49阅读模式
英文:

How to store Map<String,BigDecimal> in db

问题

我有一个实体账户,应该具有带有余额(Bigdecimal)的货币映射。

我该如何存储这样的信息,我想我需要一个单独的表,其中包含外键账户ID,欢迎提供任何建议,我正在学习,谢谢。

@Table(name = "account")
public class Account {

    @Id
    @GeneratedValue(strategy = AUTO)

    @Column(name = "account_id")
    private Long accountId;

    @Column(name = "customer_id")
    private Long customerId;

    // ->> 账户应该包含下面的映射或者是指向另一个表的外键
    // Map<String, BigDecimal> balances;
}
英文:

I have entity account that should have map of currencies with the balance(Bigdecimal)

How could i store such information, i would imagine i would need a separate table that contain foreign key account id, any suggestions would be appreciated , i am just learning, thank you

    @Table(name = &quot;account&quot;)
public class Account {

    @Id
    @GeneratedValue(strategy = AUTO)

    @Column(name = &quot;account_id&quot;)
    private Long accountId;

    @Column(name = &quot;customer_id&quot;)
    private Long customerId;

    // -&gt;&gt; account  should contain the map bellow or foreign key to another table
    //Map&lt;String, BigDecimal&gt; balances;
}

答案1

得分: 2

以下是翻译好的代码部分:

尝试以下代码

@ElementCollection
@CollectionTable(name = "balances_mapping", 
  joinColumns = {@JoinColumn(name = "account_id", referencedColumnName = "balance_id")})
@MapKeyColumn(name = "account_id")
@Column(name = "balances")
private Map<String, BigDecimal> balances;



或者使用联接表

  

@ElementCollection
@Immutable
@MapKeyColumn(name = "ACCOUNT")
@Column(name = "ACCOUNT_BALANCE")
@JoinTable(name = "ACCOUNT_BALANCES", joinColumns = @JoinColumn(name = "ACCOUNT_ID"))
private Map<String, BigDecimal> accounts;

create table ACCOUNT (
  ACCOUNT_ID NUMBER not null  constraint FR_ACCOUNT_PK primary key,
)

create table ACCOUNT_BALANCE  (
  ACCOUNT_ID      NUMBER        not null constraint  FK_ACCOUNT_ID references ACCOUNT,
  ACCOUNT_NAME    VARCHAR2(100) not null,
  ACCOUNT_BALANCE NUMBER(100) not null
)
英文:

Try following:

@ElementCollection
@CollectionTable(name = &quot;balances_mapping&quot;, 
  joinColumns = {@JoinColumn(name = &quot;account_id&quot;, referencedColumnName = &quot;balance_id&quot;)})
@MapKeyColumn(name = &quot;account_id&quot;)
@Column(name = &quot;balances&quot;)
private Map&lt;String, BigDecimal&gt; balances;

Or by using join table

@ElementCollection
@Immutable
@MapKeyColumn(name = &quot;ACCOUNT&quot;)
@Column(name = &quot;ACCOUNT_BALANCE&quot;)
@JoinTable(name = &quot;ACCOUNT_BALANCES&quot;, joinColumns = @JoinColumn(name = &quot;ACCOUNT_ID&quot;))
private Map&lt;String, BigDecimal&gt; accounts;

create table ACCOUNT (
  ACCOUNT_ID NUMBER not null  constraint FR_ACCOUNT_PK primary key,
)

create table ACCOUNT_BALANCE  (
  ACCOUNT_ID      NUMBER        not null constraint  FK_ACCOUNT_ID references ACCOUNT,
  ACCOUNT_NAME    VARCHAR2(100) not null,
  ACCOUNT_BALANCE NUMBER(100) not null
)

答案2

得分: 0

Hello, maybe your data model is wrong.

Have you tried changing your map to something like a List of another entity with 2 fields: currency and balance?

This will make your code more clear, and you will be able to add other values to this entity later.

@Table()
public class Balance {
  private String currency;
  private BigDecimal balanceAmount;
}

And if sometimes you need to use the property given by the map, you can use Stream and do something like this.

account.getBalances().stream().collect(Collectors.toMap(Balance::getCurrency, Balance::getBalanceAmount))
英文:

Hello maybe your data model is wrong.

Have you try to change your map to something like a List of another entity with 2 field currency and balance

This will make your code more clear and you will be able to add other value to this entity later.

@Table()
public class Balance{
  private String currency;
  private BigDecimal balanceAmount
}

And if sometimes you need to use the property given by the map you can use Stream and do something like this.

account.getBalances().stream().collect(Collectors.toMap(Balance::getCurrency, Balance::getBalanceAmount)

huangapple
  • 本文由 发表于 2020年7月29日 03:58:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63141886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定