AttributeConverter convertToEntityAttribute循环记录并始终获取空值?

时间:2018-08-13 11:00:37

标签: java spring spring-boot spring-data-jpa

我有一个类似于以下的Auditable类:

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)

public abstract class Auditable<U> {

    @JsonIgnore
    @CreatedBy
    @Column(nullable = false, updatable = false)
    @Convert(converter = UserConvert.class)
     protected HiUser createdby;

    @CreatedDate
    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable = false, updatable = false)
    protected Date created;

    @JsonIgnore
    @LastModifiedBy
    @Convert(converter = UserConvert.class)
    protected HiUser updatedby;

    @JsonIgnore
    @LastModifiedDate
    @Temporal(TemporalType.TIMESTAMP)
    protected Date updated;

    // Getter Setter
}

具有如下所示的转换器:

@Converter
public class UserConvert implements AttributeConverter<HiUser, Integer>{

private static final Logger Log = LoggerFactory.getLogger(UserConvert.class);
@Autowired AppUserRepository repository;

@Override
public Integer convertToDatabaseColumn(HiUser attribute) {
    int userId = 1;

    if(null != attribute)
        userId = Integer.parseInt(String.valueOf(attribute.getHiUserId()));

    return userId;
}

@Override
public HiUser convertToEntityAttribute(Integer dbData) {
    HiUser user;

    Log.info("dbData : {}", dbData);

    try {
        user = repository.findByHiUserId(Long.getLong(String.valueOf(dbData)));
        return user;
    } catch (Exception e) {
        user = null;
    }
    return user;
}

}

当我保存createdby时,它的工作原理与我想要的一样,但是当我检索数据时,它返回 null 。 让我感到困惑的是,当我选择数据时,可审核的执行不止一次,就像下面的日志中所示:

2018-08-13 17:43:32.463 TRACE 32753 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [24]
2018-08-13 17:43:32.467  INFO 32753 --- [nio-8080-exec-6] i.co.trial.util.UserConvert  : dbData : 11
2018-08-13 17:43:32.468  INFO 32753 --- [nio-8080-exec-6] i.co.trial.util.UserConvert  : dbData : 11
2018-08-13 17:43:32.468  INFO 32753 --- [nio-8080-exec-6] i.co.trial.util.UserConvert  : dbData : 1
2018-08-13 17:43:32.469  INFO 32753 --- [nio-8080-exec-6] i.co.trial.util.UserConvert  : dbData : 1
2018-08-13 17:43:32.469  INFO 32753 --- [nio-8080-exec-6] i.co.trial.util.UserConvert  : dbData : 1
2018-08-13 17:43:32.469  INFO 32753 --- [nio-8080-exec-6] i.co.trial.util.UserConvert  : dbData : 1

Ps:11是我的用户ID,该ID插入创建者字段和更新者字段中。

我是Java的新手,感谢您的帮助。谢谢

更新:

这是我的HiUser类:

@Entity
@Table(name ="hi_user")
public class HiUser extends Auditable<HiUser> implements Serializable{

    private static final long serialVersionUID = 6435055512245662354L;

    @Id
    @SequenceGenerator(schema = "PUBLIC", name="USER-SEQ",sequenceName="HI_USER_SEQ", initialValue=1, allocationSize=1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER-SEQ")
    private long hiUserId;

    @NotNull
    private String email;

    @NotNull
    private String password;

    @NotNull
    private String firstname;
    private String lastname;

// Getter Setter
}

解决:

我的初步结论是,我无法在转换器中执行数据库查询,因此我删除了转换器,并对类AuditableAuditorAwareImpl进行了一些更改,如下所示,然后一切又恢复了: / p>

可审核

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable<U> {

    @JsonIgnore
    @CreatedBy
    @Column(nullable = false, updatable = false)
    protected long createdby;

    @CreatedDate
    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable = false, updatable = false)
    protected Date created;

    @JsonIgnore
    @LastModifiedBy
    protected long updatedby;

    @JsonIgnore
    @LastModifiedDate
    @Temporal(TemporalType.TIMESTAMP)
    protected Date updated;

    // Getter Setter
}

AuditorAwareImpl

public class AuditorAwareImpl implements AuditorAware<Long>{

    @Autowired
    AppUserRepository repository;

    @Override
    public Long getCurrentAuditor() throws NullPointerException {

        String Username = SecurityContextHolder.getContext().getAuthentication().getName();

        HiUser hiUser = repository.findByEmail(Username);
        return hiUser.getHiUserId();
    }

}

0 个答案:

没有答案