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

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

Spring boot JPA fetch a list and add an item to it throws error

问题

我正在使用 JPA,我有一个名为 Order 的实体/类。我有一个用于通过 id 获取订单的 rest GET 端点。它工作得非常好。订单实体如下所示:

  1. @Entity
  2. public class Order {
  3. @Id
  4. private Long id;
  5. @Column
  6. private List<String> transactionRefs;
  7. }

现在,在一个特定的场景中,我需要从数据库中获取订单并向 transactionRefs 添加另一项并保存。所以我按照下面的方式进行:

  1. Order order = orderRepository.findById(1).get();
  2. List<String> transactionList = order.getTransactionRefs();
  3. transactionList.add("transaction-ref");

当我这样做时,我会得到以下错误:

  1. java.lang.UnsupportedOperationException: null\n\tat java.util.AbstractList.add(AbstractList.java:148)

如果我按照下面的方式做,问题就会得到解决:

  1. Order order = orderRepository.findById(1).get();
  2. List<String> transactionList = order.getTransactionRefs();
  3. transactionList = new ArrayList<>(transactionList);
  4. transactionList.add("transaction-ref");

所以,我需要知道我是否走在正确的方向上,这是否是一个预期的错误场景。

更新:

每当我们向列表中添加项目时,我们有以下条件:

  1. if (transactionRefs == null) {
  2. transactionRefs = new ArrayList<>();
  3. }

因此,每当第一次保存 transactionref 时,我们会将其转换为 ArrayList。

更新 2:

以下是 transactionRef 的 getter 方法:

  1. public List<String> getTransactionRefs(){
  2. if (this.transactionRefs != null) {
  3. return Arrays.asList(this.transactionRefs.split(","));
  4. }
  5. return null;
  6. }
英文:

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:

  1. @Entity
  2. public class Order {
  3. @Id
  4. private Long id;
  5. @Column
  6. private List&lt;String&gt; transactionRefs;
  7. }

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:

  1. Order order = orderRepository.findById(1).get();
  2. List&lt;String&gt; transactionList = order.getTransactionRefs();
  3. transactionList.add(&quot;transaction-ref&quot;);

I get the below error when I do that:

  1. java.lang.UnsupportedOperationException: null\n\tat java.util.AbstractList.add(AbstractList.java:148)

If I do as below, that fixes the problem:

  1. Order order = orderRepository.findById(1).get();
  2. List&lt;String&gt; transactionList = order.getTransactionRefs();
  3. transactionList = new ArrayList&lt;&gt;(transactionList);
  4. 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 :

  1. if (transactionRefs == null) {
  2. transactionRefs = new ArrayList&lt;&gt;();
  3. }

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:

  1. public List&lt;String&gt; getTransactionRefs(){
  2. if (this.transactionRefs != null) {
  3. return Arrays.asList(this.transactionRefs.split(&quot;,&quot;));
  4. }
  5. return null;
  6. }

答案1

得分: 2

这是您异常的原因:

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

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

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

更多示例:

英文:

This is the cause of your exception

  1. 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:

  1. List&lt;String&gt; transactionList = order.getTransactionRefs();
  2. 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:

确定