简体   繁体   English

Spring 数据 JPA 保存 不要插入数据库

[英]Spring Data JPA save don't insert into database

I am using spring data jpa with a postgres database, i have this example: package fr.enedis.pepsa.kepler.onescreen.domain;我正在使用 spring 数据 jpa 和 postgres 数据库,我有这个例子:package

@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;

}

and the service that save this entity:以及保存该实体的服务:

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;
    }
}

When i call the savingTest service from a rest controller the save is done, but when i come from a kafka consumer the save is not done and i have this output log hibernate. When i call the savingTest service from a rest controller the save is done, but when i come from a kafka consumer the save is not done and i have this output log hibernate.

from the kafka consumer:来自卡夫卡消费者:

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')

from the rest controller:来自 rest controller:

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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Any help or suggestions please请有任何帮助或建议

Thank you谢谢

You are missing a transactional annotation, therefore your changes are not committed to the database: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Transactional.html You are missing a transactional annotation, therefore your changes are not committed to the database: 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;
 }
}

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

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