简体   繁体   English

Spring Data JPA save() 返回实体

[英]Spring Data JPA save() return entity

I am using spring boot data jpa as below我正在使用弹簧启动数据 jpa 如下

 @Entity
  @Table(name = "invoice")
  @Getter
  @Setter
  @ToString
  public class Invoice {


   @Id
    @Column(name = "inv_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger invId;

    @Column(name = "external_id")
    private String externalInvoiceId;

    @Column(name = "amount")
    private double amount;

    @JsonIgnore
    @JsonIgnoreProperties
    @Column(name = "status")
    private int status;

    @JsonProperty("status")
    @Transient
    private String invoiceStatus;


    public String getInvoiceStatus() {
        switch (this.status){
            case 1:
                return "INITIATED";
            case 2:
                return "CANCELLED";
            case 3:
                return "SUCCESS";
            case 4:
                return "FAILURE";
            default:
                return "IN PROGRESS";

        }

    }

    @CreationTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_at")
    private Date createdAt;

    @UpdateTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "updated_at")
    private Date updatedAt;


    @PostPersist
    public void updateExternalID() {
        this.externalInvoiceId="G".concat(String.valueOf(this.invId.multiply(BigInteger.valueOf(1000))))
                                  .concat(String.valueOf(Instant.now().getEpochSecond()));

    }
}

Am accesing this entiry via repository as below我正在通过存储库访问此条目,如下所示

public interface InvoicesRepository extends JpaRepository<Invoice, BigInteger> {
}

At my @Service am performing the below operation在我的@Service执行以下操作

@Autowired
    private InvoicesRepository myInvoicesRepository;

    Invoice transactionInvoice = new Invoice();
    transactionInvoice.setAmount(200.0);
    transactionInvoice.setStatus(1);
    Invoice savedInvoice = myInvoicesRepository.save(transactionInvoice);

Am using savedInvoice and trying to update the status.正在使用savedInvoice并尝试更新状态。 Either it is not updating the status properly nor I could not find the record in database too.要么它没有正确更新状态,要么我也找不到数据库中的记录。

There are no rollback present不存在回滚

Below are the logs I could see insert statements are present以下是我可以看到存在插入语句的日志

[XNIO-1 task-1] DEBUG org.hibernate.SQL.logStatement -
    /* insert com.min.app.model.Invoice
        */ insert
        into
            invoice
            (amount, created_at, external_inv_id, status, updated_at)
        values
            (?, ?, ?, ?, ?)
Hibernate:
    /* insert com.min.app.model.Invoice
        */  insert
        into
            invoice
            (amount, created_at, external_inv_id, status, updated_at)
        values
            (?, ?, ?, ?, ?)

After the status updates I tried printing the savedInvoice could see the below in logs状态更新后,我尝试打印savedInvoice可以在日志中看到以下内容

Invoice(invId=58, externalInvoiceId=G580001575271905, amount=185.0 status=4, invoiceStatus=FAILURE, createdAt=Mon Dec 02 13:01:45 IST 2019, updatedAt=Mon Dec 02 13:01:45 IST 2019)

The above record I could not see in the table.上面的记录我在表中看不到。

What am I doing wrong?我究竟做错了什么?

you need to perform transactions in a function call as follows and put your @Autowired repository in Global level ass follows.您需要按如下方式在函数调用中执行事务,并将您的 @Autowired 存储库放在如下全局级别中。

class whatever{
@Autowired
    private InvoicesRepository myInvoicesRepository;

//call this function
void doSomething(){
    Invoice transactionInvoice = new Invoice();
    transactionInvoice.setAmount(200.0);
    transactionInvoice.setStatus(1);
    Invoice savedInvoice = myInvoicesRepository.save(transactionInvoice);
}
}

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

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