对象不是持久保存mongodb spring数据java

时间:2014-07-25 10:05:33

标签: java spring spring-data-mongodb

您好我正在使用spring数据mongo在mongodb中实现审计 这是我的审计类,每个模型类都扩展了该类

public abstract class Audit implements Auditable<String,String> {  
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id 
    private String id;  

    @Version  
    private Long version;  

    @CreatedBy
    private String createdBy;

    @CreatedDate
    private DateTime createdDate;  

    @LastModifiedBy
    private String lastModifiedBy;  

    @LastModifiedDate
    private DateTime lastModifiedDate;

    public boolean isNew() {  

        return id == null;
    }

    public String getId() {
        return id;
    }

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

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }

    public String getCreatedBy() {
        return createdBy;
    }

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

    public DateTime getCreatedDate() {
        return createdDate;
    }

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

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public DateTime getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(DateTime lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }



} 

所以当我更新任何对象并调用以下函数时

//event is updated object
mongoTemplate.save(event);

在保存时,我正在按照以下方式对ApplicationListener进行控制,因此控制转移到onApplicationEvent函数

public class AuditingEventListner implements ApplicationListener<BeforeConvertEvent<Object>> {

    private AuditorAware<String> auditorAware;


    public AuditingEventListner(AuditorAware<String> auditorAware) {

        this.auditorAware = auditorAware;
    }

    public void onApplicationEvent(BeforeConvertEvent<Object> event) {

        Object obj = event.getSource();
        // this obj is not persisting as i am getting object from db before updating so it should have all the values pre populated but it other then new updated values are all coming as null and result into new object creation in DB
        if (Audit.class.isAssignableFrom(obj.getClass())) {

            Audit entity = (Audit) obj;
            if (entity.isNew()) {

                entity.setCreatedBy(auditorAware.getCurrentAuditor());
                entity.setCreatedDate(new DateTime());
            }

            entity.setLastModifiedBy(auditorAware.getCurrentAuditor());
            entity.setLastModifiedDate(new DateTime());
        }
    }
}

这是我编辑事件的dao方法

public Event edit(Event event,String eventid) throws Exception,NotFoundException{

        logger.debug("Editing existing Event");

        Event existingEvent = null; 

        try {

            if(null != eventid && !eventid.equalsIgnoreCase("") ){

                existingEvent = get(eventid);
            }

            if(null ==existingEvent){

                throw new NotFoundException(eventid); 
            }

            if(null != event.getTitle() && !event.getTitle().equalsIgnoreCase(""))
            {
                existingEvent.setTitle(event.getTitle());
            }

            if(null != event.getDescription() && !event.getDescription().equalsIgnoreCase(""))
            {
                existingEvent.setDescription(event.getDescription());
            }

            if(event.getOrganizationId()!=0){

                existingEvent.setOrganizationId(event.getOrganizationId());
            }

            if(event.getEventTypeId()!=0){

                existingEvent.setEventTypeId((event.getEventTypeId()));
            }

            if(null != event.getAddress1() && !event.getAddress1().equalsIgnoreCase("")){

                existingEvent.setAddress1(event.getAddress1());
            }

            if(null != event.getAddress2() && !event.getAddress2().equalsIgnoreCase("")){

                existingEvent.setAddress2(event.getAddress2());
            }

            if(null != event.getCity() && !event.getCity().equalsIgnoreCase(""))
            {
                existingEvent.setCity(event.getCity());
            }

            if(null != event.getState() && !event.getState().equalsIgnoreCase(""))
            {
                existingEvent.setState(event.getState());
            }

            if(null!= event.getCountry() && !event.getCountry().equalsIgnoreCase(""))
            {
                existingEvent.setCountry(event.getCountry());
            }

            if(null != event.isMultiDate()){

                existingEvent.setMultiDate(event.isMultiDate());
            }

            if(null!=event.getLatitude() &&  event.getLatitude()!=0.0 && !event.getLatitude().toString().equalsIgnoreCase("") )
            {
                existingEvent.setLatitude(event.getLatitude());
            }

            if(null != event.getLongitude() && event.getLongitude() != 0.0 && !event.getLongitude().toString().equalsIgnoreCase("")){

                existingEvent.setLongitude(event.getLongitude());
            }

            if(null != event.getLastModifiedBy() && !event.getLastModifiedBy().equalsIgnoreCase(""))
            {           
                existingEvent.setLastModifiedBy(event.getLastModifiedBy());
            }

            if(null != event.getMultiLingual() && event.getMultiLingual().size() > 0 )
            {
                for(MultiLingual multiLingual:event.getMultiLingual())
                {
                    multiLingual.setId(UUID.randomUUID().toString());

                }
                existingEvent.setMultiLingual(event.getMultiLingual());
            }

            if(null !=event.getTo() && !event.getTo().toString().equalsIgnoreCase(""))
            {
                existingEvent.setTo(event.getTo());
            }

            if(null !=event.getFrom() && !event.getFrom().toString().equalsIgnoreCase(""))
            {
                existingEvent.setFrom(event.getFrom());
            }

            //existingEvent.setLastModified(new Date());

            mongoTemplate.save(event);

            return event;

        } catch (Exception e) {

            logger.error("An error has occurred while trying to edit existing event", e);

            throw e;
        }

    }

因此在onApplicationEvent中,对象不会预先填充。任何想法我怎样才能实现这一目标。 ?我在更新之前从db获取对象,如您在edit方法中所见,因此它应该预先填充所有值,但其他新的更新值都将为null并导致在DB中创建新对象

1 个答案:

答案 0 :(得分:0)

我遇到问题我做错了

 mongoTemplate.save(event);

我需要将existingEvent对象传递给save方法而不是事件,因此更正是

 mongoTemplate.save(existingEvent);