英文:
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 = "account")
public class Account {
@Id
@GeneratedValue(strategy = AUTO)
@Column(name = "account_id")
private Long accountId;
@Column(name = "customer_id")
private Long customerId;
// ->> account should contain the map bellow or foreign key to another table
//Map<String, BigDecimal> 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 = "balances_mapping",
joinColumns = {@JoinColumn(name = "account_id", referencedColumnName = "balance_id")})
@MapKeyColumn(name = "account_id")
@Column(name = "balances")
private Map<String, BigDecimal> balances;
Or by using join table
@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
)
答案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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论