gpt4 book ai didi

java - 由 : java. lang.UnsupportedOperationException: null Lombok with List 引起

转载 作者:行者123 更新时间:2023-11-29 06:48:44 24 4
gpt4 key购买 nike

我正在研究 Spring Data JPA + Postgres lombok 示例。在这个例子中,我遇到了以下错误。

java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:782) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:763) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:318) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.example.StockApplication.main(StockApplication.java:18) [classes/:na]
Caused by: java.lang.UnsupportedOperationException: null
at java.util.AbstractCollection.add(AbstractCollection.java:262) ~[na:1.8.0_171]
at org.hibernate.collection.internal.PersistentSet.add(PersistentSet.java:211) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.example.StockApplication.run(StockApplication.java:40) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
... 5 common frames omitted

股票.java

@Builder
@Getter
@AllArgsConstructor
@Entity
@Table(name = "stock", catalog = "mkyongdb", uniqueConstraints = { @UniqueConstraint(columnNames = "STOCK_NAME"),
@UniqueConstraint(columnNames = "STOCK_CODE") })
public class Stock implements java.io.Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "STOCK_ID", unique = true, nullable = false)
private Integer stockId;

@Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10)
private String stockCode;

@Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20)
private String stockName;


@Singular
@OneToMany(fetch = FetchType.LAZY, mappedBy = "stock")
private final Set<StockDailyRecord> stockDailyRecords;
}

股票日报.java

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "stock_daily_record", catalog = "mkyongdb", uniqueConstraints = @UniqueConstraint(columnNames = "DATE"))
public class StockDailyRecord implements java.io.Serializable {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "RECORD_ID", unique = true, nullable = false)
private Integer recordId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "STOCK_ID", nullable = false)
private Stock stock;

@Column(name = "PRICE_OPEN", precision = 6)
private Float priceOpen;

@Column(name = "PRICE_CLOSE", precision = 6)
private Float priceClose;

@Column(name = "PRICE_CHANGE", precision = 6)
private Float priceChange;

@Column(name = "VOLUME")
private Long volume;

@Temporal(TemporalType.DATE)
@Column(name = "DATE", unique = true, nullable = false, length = 10)
private Date date;
}

主程序

@SpringBootApplication
public class StockApplication implements CommandLineRunner{

public static void main(String[] args) {
SpringApplication.run(StockApplication.class, args);
}

@Autowired
private StockRepository stockRepository;
@Autowired
private StockDailyRecordRepository stockDailyRecordRepository;

@Override
public void run(String... args) throws Exception {
Stock stock = Stock.builder().stockCode("7052").stockName("PADINI").build();
stockRepository.save(stock);

StockDailyRecord stockDailyRecords = StockDailyRecord.builder()
.priceOpen(new Float("1.2"))
.priceChange(new Float("10.0"))
.priceClose(new Float("1.1"))
.date(new Date())
.volume(3000000L)
.build();

stockDailyRecords.setStock(stock);
stock.getStockDailyRecords().add(stockDailyRecords); //Line-22
stockDailyRecordRepository.save(stockDailyRecords);
}
}

注意:代码在第 22 行中断

最佳答案

阅读 https://projectlombok.org/features/Builder 上的@Singular 部分你应该有你的答案。

基本上在调用 build() 之后,底层集合将是不可变的,因此不允许添加更多数据。正确的方法是重用构建器,添加新的 stockDailRecords 并创建一个新对象。

不幸的是,在您的情况下,它可能会破坏持久性部分,因为数据库希望修改同一对象。 IMO 使用这种不可变对象(immutable对象)来完成这项任务是不明智的。

UnsupportedOperationException is thrown with Lombok Builder annotation 也讨论了一个非常相似的问题

关于java - 由 : java. lang.UnsupportedOperationException: null Lombok with List 引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56831757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com