@OneToMany关系上的UnsupportedOperationException保存尝试

时间:2018-08-03 22:01:03

标签: java hibernate spring-boot spring-data

我有一个与Posting具有OneToMany关系的实体(JournalEntry)。我正在检索JournalEntry,并尝试更新单个字段。但是我有以下异常:

  java.lang.UnsupportedOperationException
    at java.util.AbstractList.remove(AbstractList.java:161)
    at java.util.AbstractList$Itr.remove(AbstractList.java:374)
    at java.util.AbstractList.removeRange(AbstractList.java:571)
    at java.util.AbstractList.clear(AbstractList.java:234)
    at org.hibernate.collection.internal.PersistentBag.clear(PersistentBag.java:383)
    at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:512)

JournalEntry

  public class JournalEntry implements Serializable {

      @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;

      private String description;

      private Date exportDate;

      @OneToMany(cascade = CascadeType.ALL, mappedBy = "journalEntry")
      private List<Posting> postings;
  }

发布

  public class Posting implements Serializable {

      @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;

      private BigDecimal amount;

      @ManyToOne @JoinColumn(name = "journal_entry_id")
      private JournalEntry journalEntry;

  }

保存例程:

  public void updateJournalEntriesExportDate(Date firstEffectiveDate, Date lastEffectiveDate) {
          Date now = Date.from(LocalDateTime.now(ZoneId.of("UTC")).toInstant(ZoneOffset.UTC));
          List<JournalEntry> entries = journalEntryRepository.findByEffectiveDateBetween(firstEffectiveDate, lastEffectiveDate);
          entries.stream().forEach(entry -> {
              entry.setExportDate(now);
              journalEntryRepository.save(entry); // Exception !!!
          });
      }

我的代码有什么问题以及如何解决? Hibernate为什么引发该异常。我调试了代码,意识到它创建了一个带有不可变列表的bag对象,然后尝试从中删除。

0 个答案:

没有答案
相关问题