简体   繁体   English

Spring Boot-Jpa-SaveAll(Entity <S>)数据库中缺少实体</s>

[英]Spring boot - Jpa - SaveAll(Entity <S>) missing entities in the database

I am having an issue when I am trying to save a List of 21,300 entities, however in the database only [SELECT COUNT(id) = ] 10,506 are being saved. 我在尝试保存21,300个实体的列表时遇到问题,但是在数据库中仅保存了[SELECT COUNT(id)=] 10,506

The console output demonstrates the size of the list ready to be saved is correct so the code is "correct". 控制台输出显示准备保存的列表大小正确,因此代码“正确”。 If I make it loop only once with freq==30 for instance, this works fine so it seems linked somehow to the for each loop when it loops several times. 例如,如果我使用freq == 30使其仅循环一次,则此方法工作正常,因此当它循环多次时,似乎以某种方式链接到for循环。 The missing entities are somewhat dispersed in the population. 失踪的实体散布在人群中。

Any idea of why this particular behavior? 知道为什么会有这种特殊行为吗?

public void loadTickAll(int conid) {
    System.out.println(">>>> MERGING <<<<<< Querying ticks for " + conid);
    List<CandleTick> candlesHisto = histoCandleTickRepo.findByConidAll(conid, LocalDate.parse("2016-11-01"));
    List<CandleTick> candlesLive = new ArrayList<>();

    List<CandleTick> candles = Stream.of(candlesHisto, candlesLive).flatMap(Collection::stream)
            .collect(Collectors.toList());

     freqList.forEach((freq) -> {
     if(freq>=30) {
     System.out.println("Working on freq " + freq);

     FlowMerger flowMerger = new FlowMerger(freq, contracts.get(conid));
     List<CandleMerge> flow = flowMerger.createFlow(new ArrayList<>(candles));
     Collections.reverse(flow);

     System.out.println("Start saving " + freq + " - " + flow.size());
     histoCandleMergeRepo.saveAll(flow);

     System.out.println(freq + "/" + conid + " has been delivered and saved");
     }
     });

}

Console output 控制台输出

>>>> MERGING <<<<<< Querying ticks for 5 Working on freq 30 Start saving 30 - 21300 30/5 has been delivered and saved

application.properties application.properties

# jdbc.X
jdbc.driverClassName=org.postgresql.Driver

histo.jdbc.url=jdbc:postgresql://localhost:5432/trading2018
live.jdbc.url=jdbc:postgresql://localhost:5433/trading2018

jdbc.user=...
jdbc.pass=...


## hibernate.X
hibernate.default_schema=trading
hibernate.jdbc.lob.non_contextual_creation=true
hibernate.show_sql=false
hibernate.temp.use_jdbc_metadata_defaults=false
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.hbm2ddl.auto=validate
#hibernate.cache.use_second_level_cache=false
#hibernate.cache.use_query_cache=false

logging.level.org.springframework.web=ERROR
logging.level.com=DEBUG
logging.file=C:/tmp/application.log

I managed to trace down the issue. 我设法找出问题所在。 I showed the SQL and noticed I was updating instead of inserting. 我显示了SQL,并注意到我正在更新而不是插入。

My entity was missing some annotations... 我的实体缺少一些注释...

@GeneratedValue(generator = "merge_id_seq")
@SequenceGenerator(name = "candleid_generator_generator", sequenceName = "merge_id_seq", initialValue = 1)
@Id
private int id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM