org.springframework.dao.DataIntegrityViolationException:无法执行语句; SQL

时间:2019-11-29 16:55:55

标签: java spring hibernate spring-boot

这是我的实体类

@Entity
@Table(name = "sofort_payment")
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class SofortPaymentEntity extends BasicEntity {

    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    @Column(name = "id", updatable = false, nullable = false)
    private UUID id;
    @Column(name = "referenced_transaction")
    private UUID referencedTransaction;
    @Column(name = "status")
    private String status;
    @Column(name = "reason")
    private String reason;
    @Column(name = "external_payment_id")
    private String externalPaymentId;
    @Column(name = "currency")
    private String currency;
    @Column(name = "amount")
    private BigDecimal amount;
    @Column(name = "iban")
    private String iban;
    @Column(name = "back_name")
    private String bankName;
    @Column(name = "sender_holder")
    private String senderHolder;
    @Column(name = "bic")
    private String bic;
    @Column(name = "bank_code")
    private String bankCode;
    @Column(name = "country_code")
    private String countryCode;

}

这是我的基本实体类

@MappedSuperclass
@Data
@Accessors(chain = true)
public abstract class BasicEntity {
  @Column(name = "created_at")
  @CreationTimestamp
  private LocalDateTime createdAt;
  @Column(name = "updated_at")
  @UpdateTimestamp
  private LocalDateTime updatedAt;
}

and this is my table

create table sofort_payment
(
    id                          UUID         NOT NULL
        constraint sofort_payment_pkey primary key,
    referenced_transaction      UUID references transaction(id) NOT NULL,
    status                      varchar(255) NOT NULL,
      reason                      text,
    external_payment_id         varchar(255) NOT NULL,
    currency                    varchar(255) NOT NULL,
    amount                      numeric(19, 2) NOT NULL,
      iban                        varchar(255) NOT NULL,
      back_name                   varchar(255) NOT NULL,
    sender_holder               varchar(255) NOT NULL,
    bic                         varchar(255) NOT NULL,
    bank_code                   varchar(255) NOT NULL,
    country_code                varchar(255) NOT NULL,
    created_at                  timestamp without time zone NOT NULL,
    updated_at                  timestamp without time zone NOT NULL
);

但是当我尝试打印SofortPaymentEntity时,它不打印crated_dt和update_dt,当我尝试更新时,抛出以下异常。该表已经具有条目,这意味着它是首先创建的,现在我正尝试对其进行更新

sofortPaymentRepository.save(sofortPaymentEntity.builder()
                                                 .id(sofortPaymentEntity.getId())
                                                 .referencedTransaction(sofortPaymentEntity.getReferencedTransaction())
                                                 .externalPaymentId(statusNotification.getTransId())
                                                 .amount(BigDecimal.valueOf(paymentTransactionDetails.getAmount()))
                                                 .currency(paymentTransactionDetails.getCurrencyCode())
                                                 .reason(paymentTransactionDetails.getStatusReason().toString())
                                                 .bankCode(paymentTransactionDetails.getSender().getBankCode())
                                                 .bankName(paymentTransactionDetails.getSender().getBankName())
                                                 .bic(paymentTransactionDetails.getSender().getBic())
                                                 .iban(paymentTransactionDetails.getSender().getIban())
                                                 .countryCode(paymentTransactionDetails.getSender().getCountryCode())
                                                 .senderHolder(paymentTransactionDetails.getSender().getHolder())
                                                 .status(paymentTransactionDetails.getStatus().toString()).build());

错误:列“ created_at”中的空值违反了非空约束

Generic exception caught: 

org.springframework.dao.DataIntegrityViolationException:无法执行语句; SQL [n / a];约束[created_at];嵌套的异常是org.hibernate.exception.ConstraintViolationException:无法执行语句 在org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:296) 在org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253) 在org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:536) 在org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:746) 在org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:714) 在org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:534) 在org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:305) 在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) 在org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) 在org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor $ CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:138)处 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) 在org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) 在org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)

1 个答案:

答案 0 :(得分:0)

在创建类似

的表时提供默认的时间戳记值
created_at timestamp without time zone default CURRENT_TIMESTAMP NOT NULL

或为特定于数据库提供程序的@Column注释设置columnDefinition属性,并使用@Temporal。前psql:

@Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private java.util.Date createdAt