簡體   English   中英

Spring 數據 JPA 保存 不要插入數據庫

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

我正在使用 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;

}

以及保存該實體的服務:

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.

來自卡夫卡消費者:

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

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

請有任何幫助或建議

謝謝

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