我正在研究 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
Stock.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;
}
StockDailyRecord.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;
}
Main.App
@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行中断
答案 0 :(得分:4)
第22行的stockDailyRecords设置可以为null。在添加值之前进行检查。
if(Objects.nonNull(stock.getStockDailyRecords())){
stock.getStockDailyRecords().add(stockDailyRecords);
} else {
Set setA = new HashSet();
set.add(stockDailyRecords);
stock.setStockDailyRecords(setA);
}
答案 1 :(得分:1)
阅读https://projectlombok.org/features/Builder上的 @Singular 部分,您将得到答案。
基本上,在调用 build()之后,基础集合将是不可变的,因此不允许添加更多数据。正确的方法是重新使用构建器,添加新的 stockDailRecords 并创建一个新对象。
不幸的是,由于数据库希望修改同一对象,因此可能会破坏持久性部分。 IMO将此类不可变对象用于此任务根本不明智。
UnsupportedOperationException is thrown with Lombok Builder annotation
也讨论了一个非常类似的问题