我正在实施基于[1]的审核,但我没有通过@createdBy
和@ModifiedBy
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "entity")
public class myEntity {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer idMyEntity;
@Column(unique=true)
private String nome;
@CreatedBy
private UserEntity userCreated;
@LastModifiedBy
private UserEntity userModified;
... getters and setters
}
我的AudityAware是:
public class AuditorAwareImp implements AuditorAware<UserEntity> {
public UserEntity getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated()) {
return null;
}
System.out.println((UserEntity) authentication.getPrincipal());
return ((UserEntity) authentication.getPrincipal());
}
}
我的豆子是:
@Bean
public AuditorAware<UserEntity> auditorAware() {
return new AuditorAwareImp();
}
当我调试时,我看到userEntity按预期加载,但是当jpa保存时,它会保存.toString(),而不是UserEntity @Id
,这是不正确的...给我一个错误:
Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'userCreated' at row 1
为什么会发生这种情况以及如何解决?
答案 0 :(得分:0)
问题出在实体之间的关系中。当我这样做时,它解决了我的问题。
在@CreatedBy
和@LastModifiedBy
后根据我的关系添加@OneToMany
并按预期工作