Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java OnBeforeSave被调用,但什么也没有发生_Java_Spring_Mongodb_Spring Data_Event Listener - Fatal编程技术网

Java OnBeforeSave被调用,但什么也没有发生

Java OnBeforeSave被调用,但什么也没有发生,java,spring,mongodb,spring-data,event-listener,Java,Spring,Mongodb,Spring Data,Event Listener,我试图检查我试图持久化的对象是否已经有“所有者”,如果没有,我设置登录用户。此EventListener适用于任何其他联系人字段,并且在调试时设置用户。MongoRepository save方法的返回包含正确的set user,但是当我查看数据库时,用户没有被保存 OwnedByUserMongoEventListener类 public void onBeforeSave(BeforeSaveEvent<Contact> event) { Contact source =

我试图检查我试图持久化的对象是否已经有“所有者”,如果没有,我设置登录用户。此EventListener适用于任何其他联系人字段,并且在调试时设置用户。MongoRepository save方法的返回包含正确的set user,但是当我查看数据库时,用户没有被保存

OwnedByUserMongoEventListener类

public void onBeforeSave(BeforeSaveEvent<Contact> event) {
    Contact source = event.getSource();
    ReflectionUtils.doWithFields(source.getClass(), new OwnedByUserCallback(source));
}
/

联系人扩展了可审核的{
@身份证
私有字符串id;
@索引(唯一=真,稀疏=真)
@NotNull
私有长序列ID;
私有BasicDBObject属性=新BasicDBObject(新HashMap());
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
@抑制警告(“未选中”)
公共映射getAttributes(){
返回attributes.toMap();
}
公共void集合属性(映射属性){
this.attributes=新的基本对象(属性);
}
公共字符串toString(){
返回“ID:+getId()+”属性:+getAttributes();
}
公共长getSeqId(){
返回seqId;
}
公共无效设置seqId(长序列ID){
this.seqId=seqId;
}
}
可审核的公共抽象类{
@创建数据
私人日期创建日期;
@DBRef
@创造的
由用户创建的私有用户;
@DBRef
@用户拥有
私人用户所有者;
@最后修改日期
私人日期更新日期;
@DBRef
@最后修改
私人用户更新人;
公共日期getCreatedDate(){
返回createdDate;
}
public void setCreatedDate(日期createdDate){
this.createdDate=createdDate;
}
公共用户getCreatedBy(){
返回createdBy;
}
公共void setCreatedBy(用户createdBy){
this.createdBy=createdBy;
}
公共日期GetUpdateDate(){
返回更新日期;
}
公共无效设置日期日期(日期更新日期){
this.updateDate=updateDate;
}
公共用户getUpdatedBy(){
返回更新人;
}
public void setUpdatedBy(用户更新人){
this.updatedBy=updatedBy;
}
公共用户getOwner(){
归还所有人;
}
公共无效集合所有者(用户所有者){
this.owner=所有者;
}
}

我试图使用OnBeforeSave,在调用MongoConverter后,除非您更改事件。getDBObject()。如果要使用类对象进行更改,则需要使用OnBeforeConvert。。。我花了很多时间跟踪这一点,因为文档很差…

我试图使用OnBeforeSave,在调用MongoConverter后,除非您更改事件。getDBObject()。如果要使用类对象进行更改,则需要使用OnBeforeConvert。。。我花了很多时间来跟踪这个,因为糟糕的文档

@Override
public void doWith(java.lang.reflect.Field field) throws IllegalArgumentException, IllegalAccessException {
    ReflectionUtils.makeAccessible(field);
    final Object fieldValue = field.get(getSource());
    if (field.isAnnotationPresent(OwnedByUser.class)) {
        if ( fieldValue == null ) {
            JwtUser principal = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            User user = mongoOperations.findOne(Query.query(Criteria.where("username").is(principal.getUsername())), User.class);
            field.set(getSource(), user);
        }
    }

}
Contact extends Auditable {

    @Id
    private String id;

    @Indexed(unique=true,sparse=true)
    @NotNull
    private Long seqId;

    private BasicDBObject attributes = new BasicDBObject(new HashMap<>());

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> getAttributes() {
        return attributes.toMap();
    }

    public void setAttributes(Map<String, Object> attributes) {
        this.attributes = new BasicDBObject(attributes);
    }

    public String toString() {
        return "ID: "+getId() +" Attributes: " + getAttributes();
    }

    public Long getSeqId() {
        return seqId;
    }

    public void setSeqId(Long seqId) {
        this.seqId = seqId;
    }

}



public abstract class Auditable {

    @CreatedDate
    private Date createdDate;

    @DBRef
    @CreatedBy
    private User createdBy;

    @DBRef
    @OwnedByUser
    private User owner;

    @LastModifiedDate
    private Date updatedDate;

    @DBRef
    @LastModifiedBy
    private User updatedBy;

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public User getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    public Date getUpdatedDate() {
        return updatedDate;
    }

    public void setUpdatedDate(Date updatedDate) {
        this.updatedDate = updatedDate;
    }

    public User getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(User updatedBy) {
        this.updatedBy = updatedBy;
    }

    public User getOwner() {
        return owner;
    }

    public void setOwner(User owner) {
        this.owner = owner;
    }

}