gpt4 book ai didi

java - org.springframework.dao.DataIntegrityViolationException : could not execute statement; SQL

转载 作者:行者123 更新时间:2023-12-01 19:47:50 26 4
gpt4 key购买 nike

这是我的实体类

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

我的表结构:

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 时,它不会打印 clated_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());

错误:

null value in column "created_at" violates not-null constraint

捕获通用异常:

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; 
constraint [created_at];
nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:296)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:536)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:746)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:714)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:534)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:305)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:138)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)

最佳答案

创建表时给出默认时间戳值

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

关于java - org.springframework.dao.DataIntegrityViolationException : could not execute statement; SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59108626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com