【问题标题】:Spring Data JPA save don't insert into databaseSpring Data JPA保存不插入数据库
【发布时间】:2020-07-09 08:45:47
【问题描述】:

我正在使用带有 postgres 数据库的 spring data jpa,我有这个例子: 包 fr.enedis.peepsa.kepler.onescreen.domain;

@Entity
@Table(indexes = { @Index(name = "hashcode", columnList = "code") })
@FieldDefaults(level= AccessLevel.PRIVATE)
@Getter
@Setter
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class Alert {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "alert_generator")
    @SequenceGenerator(name="alert_generator", sequenceName = "alert_seq")
    Long id;
    String code;
    @Enumerated(EnumType.STRING)
    Environment environment;
    @Enumerated(EnumType.STRING)
    Application nna;
    String alertName;
    String host;
    String cluster;
    String agentInfo;
    @Enumerated(EnumType.STRING)
    Publisher publisher;
    @Enumerated(EnumType.STRING)
    Criticality criticality;
    String alertInfo;
    @Transient
    long timestamp;
    Timestamp startDate;
    Timestamp endDate;

}

以及保存该实体的服务:

public void savingTest(AlertVM a) {
    Alert receivedAlert = new Alert(a);
    receivedAlert.setCode(receivedAlert.generateCode());
    //get DB alert if exist
    Alert alert = alertRepository.findByCodeAndEndDateIsNull(receivedAlert.getCode()).orElse(receivedAlert);
    switch (receivedAlert.getCriticality()) {
        case CRITICAL: {
            if(alert.getId() != null){
                break;
            }

            alert.setStartDate(new Timestamp(alert.getTimestamp()*1000));
            Alert as = alertRepository.save(alert);
            System.out.println("--------------------------------------> " + as.toString());

            break;
    }
}

当我从休息控制器调用 savingTest 服务时,保存完成,但是当我来自 kafka 消费者时,保存没有完成,我有这个输出登录休眠。

来自卡夫卡消费者:

Hibernate: select alert0_.id as id1_0_, alert0_.agent_info as agent_in2_0_, alert0_.alert_info as alert_in3_0_, alert0_.alert_name as alert_na4_0_, alert0_.cluster as cluster5_0_, alert0_.code as code6_0_, alert0_.criticality as critical7_0_, alert0_.end_date as end_date8_0_, alert0_.environment as environm9_0_, alert0_.host as host10_0_, alert0_.nna as nna11_0_, alert0_.publisher as publish12_0_, alert0_.start_date as start_d13_0_ from alert alert0_ where alert0_.code=? and (alert0_.end_date is null)
Hibernate: select nextval ('alert_seq')
Hibernate: select nextval ('alert_seq')

来自其余控制器:

Hibernate: select alert0_.id as id1_0_, alert0_.agent_info as agent_in2_0_, alert0_.alert_info as alert_in3_0_, alert0_.alert_name as alert_na4_0_, alert0_.cluster as cluster5_0_, alert0_.code as code6_0_, alert0_.criticality as critical7_0_, alert0_.end_date as end_date8_0_, alert0_.environment as environm9_0_, alert0_.host as host10_0_, alert0_.nna as nna11_0_, alert0_.publisher as publish12_0_, alert0_.start_date as start_d13_0_ from alert alert0_ where alert0_.code=? and (alert0_.end_date is null)

Hibernate: insert into alert (agent_info, alert_info, alert_name, cluster, code, criticality, end_date, environment, host, nna, publisher, start_date, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

请提供任何帮助或建议

谢谢

【问题讨论】:

  • 执行此操作时是否遇到任何错误?
  • savingTest 方法似乎不知道是谁在调用它,所以它必须是 receivedAlert.getCriticality() 不是 CRITICAL 所以它不会被调用。调试一下。
  • 我没有错误只是输出没有插入
  • @RobertNiestroj receivedAlert.getCriticality() 很好,我在CRITICAL 的情况下输入,然后在保存休眠中获取 nextVal 并且之后不要保存
  • 试试 saveAndFlush()

标签: java spring postgresql hibernate spring-data-jpa


【解决方案1】:

您缺少事务注释,因此您的更改不会提交到数据库: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Transactional.html

@Transactional
public void savingTest(AlertVM a) {
Alert receivedAlert = new Alert(a);
receivedAlert.setCode(receivedAlert.generateCode());
//get DB alert if exist
Alert alert = alertRepository.findByCodeAndEndDateIsNull(receivedAlert.getCode()).orElse(receivedAlert);
switch (receivedAlert.getCriticality()) {
    case CRITICAL: {
        if(alert.getId() != null){
            break;
        }

        alert.setStartDate(new Timestamp(alert.getTimestamp()*1000));
        Alert as = alertRepository.save(alert);
        System.out.println("--------------------------------------> " + as.toString());

        break;
 }
}

【讨论】:

  • 使用@Transactional("transactionManager") 我有这个错误:2020-07-09 11:47:59,902 ERROR [org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1] org.springframework.transaction.interceptor.TransactionAspectSupport: Application exception overridden by rollback exception org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
  • 2021-03-05
  • 2022-01-07
  • 2019-09-30
  • 1970-01-01
  • 1970-01-01
  • 2019-03-31
相关资源
最近更新 更多