【发布时间】:2020-11-10 21:11:17
【问题描述】:
我正在使用 Java 1.8 和 SpringBoot 2.3.4.RELEASE。我正在尝试使用 AuditingEntityListener 和 @MappedSuperclass 功能来填充 created_date 和 updated_date 时间戳。
但是我得到了这个异常:java.lang.ClassCastException: Cannot cast java.lang.String to java.time.LocalDateTime
我尝试不使用 @MappedSuperclass 并且它可以工作.....但是 created_date 和 updated_date 字段没有被填充。
谁能帮我解决这个问题?
这里是sn-p的代码:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AuditableEntity implements Serializable {
@CreatedDate
@Column(name = "created_ts",nullable = false, updatable = false)
private LocalDateTime createdDate;
@LastModifiedBy
@Column(name = "updated_ts",nullable = false)
private LocalDateTime updatedDate;
@CreatedBy
@Column(name = "src_sys_cd",length = 15, updatable = false)
private String source;
@Version
@Column(name = "record_version_nb", length = 20, nullable = false)
private Long version;
}
----------------------------------
@Component
public class AuditorAwareImpl implements AuditorAware<String> {
@Value("${customer.service.source.code}")
private String auditor;
@Override
public Optional<String> getCurrentAuditor() {
return Optional.of(auditor);
}
}
----------------------------------
@Entity
@Setter
@Getter
@Builder
@Table(name = "customer_information")
@AllArgsConstructor
@NoArgsConstructor
public class CustomerEntity extends AuditableEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
}
【问题讨论】:
-
会不会和
@LastModifiedBy private LocalDateTime updatedDate有关?我想你的意思是@LastModifiedDate -
是的,你是对的......我的意思是@LastModifiedDate......我错过了。感谢识别。欣赏它。
标签: spring spring-boot hibernate java-8 spring-data-jpa