这是我的实体类
@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时,它没有打印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());
错误:
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)
1条答案
按热度按时间kqhtkvqz1#
在创建表时给出一个默认的时间戳值,如下所示
或者为特定于数据库提供程序的@Column注解设置ColumnDefinition属性并使用@Temporal。前PSQL: