英文:
Spring boot JPA fetch a list and add an item to it throws error
问题
我正在使用 JPA,我有一个名为 Order 的实体/类。我有一个用于通过 id 获取订单的 rest GET
端点。它工作得非常好。订单实体如下所示:
@Entity
public class Order {
@Id
private Long id;
@Column
private List<String> transactionRefs;
}
现在,在一个特定的场景中,我需要从数据库中获取订单并向 transactionRefs 添加另一项并保存。所以我按照下面的方式进行:
Order order = orderRepository.findById(1).get();
List<String> transactionList = order.getTransactionRefs();
transactionList.add("transaction-ref");
当我这样做时,我会得到以下错误:
java.lang.UnsupportedOperationException: null\n\tat java.util.AbstractList.add(AbstractList.java:148)
如果我按照下面的方式做,问题就会得到解决:
Order order = orderRepository.findById(1).get();
List<String> transactionList = order.getTransactionRefs();
transactionList = new ArrayList<>(transactionList);
transactionList.add("transaction-ref");
所以,我需要知道我是否走在正确的方向上,这是否是一个预期的错误场景。
更新:
每当我们向列表中添加项目时,我们有以下条件:
if (transactionRefs == null) {
transactionRefs = new ArrayList<>();
}
因此,每当第一次保存 transactionref 时,我们会将其转换为 ArrayList。
更新 2:
以下是 transactionRef 的 getter 方法:
public List<String> getTransactionRefs(){
if (this.transactionRefs != null) {
return Arrays.asList(this.transactionRefs.split(","));
}
return null;
}
英文:
I am using JPA and I have an entity/class named Order. I have a rest GET
endpoint to fetch an order by an id. It works perfectly fine. The order entity looks like below:
@Entity
public class Order {
@Id
private Long id;
@Column
private List<String> transactionRefs;
}
Now, in one particular scenario, I need to fetch the order from the database and add another item to the transactionRefs and save it. So I do as below:
Order order = orderRepository.findById(1).get();
List<String> transactionList = order.getTransactionRefs();
transactionList.add("transaction-ref");
I get the below error when I do that:
java.lang.UnsupportedOperationException: null\n\tat java.util.AbstractList.add(AbstractList.java:148)
If I do as below, that fixes the problem:
Order order = orderRepository.findById(1).get();
List<String> transactionList = order.getTransactionRefs();
transactionList = new ArrayList<>(transactionList);
transactionList.add("transaction-ref");
So, I need to know if I am in the right direction here and is this an expected error scenario.
Update:
Whenever we are adding an item to the list, we have the below condition :
if (transactionRefs == null) {
transactionRefs = new ArrayList<>();
}
So, whenever the transactionref is saved for the first time, we cast it to a ArrayList.
Update 2 :
Below is the getter for the transactionRef:
public List<String> getTransactionRefs(){
if (this.transactionRefs != null) {
return Arrays.asList(this.transactionRefs.split(","));
}
return null;
}
答案1
得分: 2
这是您异常的原因:
return Arrays.asList(this.transactionRefs.split(","));
Arrays.asList 返回由数组支持的集合,无法使用 add
或 addAll
进行修改。您需要像在问题中所做的那样创建一个新的 List:
List<String> transactionList = order.getTransactionRefs();
transactionList = new ArrayList<>(transactionList);
更多示例:
-
https://stackoverflow.com/questions/18389012/how-to-add-elements-in-list-when-used-arrays-aslist
-
https://stackoverflow.com/questions/25447502/regarding-immutable-list-created-by-arrays-aslist
英文:
This is the cause of your exception
return Arrays.asList(this.transactionRefs.split(","));
Arrays.asList returns a collection backed by the array and it can't be modified with add
or addAll
. You need to create the List just like you are doing in the question:
List<String> transactionList = order.getTransactionRefs();
transactionList = new ArrayList<>(transactionList);
For more examples:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论