Spring审计 - 在createdBy上数据太长

时间:2017-10-26 02:59:46

标签: java spring hibernate jpa auditing

我正在实施基于[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

为什么会发生这种情况以及如何解决?

[1] https://docs.spring.io/spring-data/data-jpa/docs/1.7.0.DATAJPA-580-SNAPSHOT/reference/html/auditing.html

1 个答案:

答案 0 :(得分:0)

问题出在实体之间的关系中。当我这样做时,它解决了我的问题。

@CreatedBy@LastModifiedBy后根据我的关系添加@OneToMany并按预期工作