Im implementing the auditing based on [1] but im not getting success with @createdBy
and @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
}
And my AudityAware is:
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());
}
}
And my bean is:
@Bean
public AuditorAware<UserEntity> auditorAware() {
return new AuditorAwareImp();
}
When i debug, i see the userEntity loaded as expected, but when jpa saves, it saves the .toString(), instead UserEntity @Id
, which is not correct... giving me an error:
Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'userCreated' at row 1
Why is it happening and how to fix?
The problems was in the relationShip between the entities. When i did it, it solve my problem.
After @CreatedBy
and @LastModifiedBy
i add @OneToMany
according my relationship and worked as expected