Spring Boot JPA获取列表并添加项时抛出错误。

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

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&lt;String&gt; 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&lt;String&gt; transactionList = order.getTransactionRefs();
transactionList.add(&quot;transaction-ref&quot;);

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&lt;String&gt; transactionList = order.getTransactionRefs();
transactionList = new ArrayList&lt;&gt;(transactionList);
transactionList.add(&quot;transaction-ref&quot;);

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&lt;&gt;();
}

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&lt;String&gt; getTransactionRefs(){
    if (this.transactionRefs != null) {
        return Arrays.asList(this.transactionRefs.split(&quot;,&quot;));
    }
    return null;
}

答案1

得分: 2

这是您异常的原因:

return Arrays.asList(this.transactionRefs.split(","));

Arrays.asList 返回由数组支持的集合,无法使用 addaddAll 进行修改。您需要像在问题中所做的那样创建一个新的 List:

List<String> transactionList = order.getTransactionRefs();
transactionList = new ArrayList<>(transactionList);

更多示例:

英文:

This is the cause of your exception

return Arrays.asList(this.transactionRefs.split(&quot;,&quot;));

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&lt;String&gt; transactionList = order.getTransactionRefs();
transactionList = new ArrayList&lt;&gt;(transactionList);

For more examples:

huangapple
  • 本文由 发表于 2020年6月29日 18:25:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/62636093.html
匿名

发表评论

匿名网友

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

确定