【问题标题】:mongodb DAO sets all attributes to null before save()mongodb DAO 在 save() 之前将所有属性设置为 null
【发布时间】:2015-12-07 12:06:43
【问题描述】:

我尝试从上下文中接收一个类的 bean,然后将它保存在一个 mongodb 集合中。问题是,由于未知原因,它的所有属性在保存之前都设置为 null。当我尝试将对象保存在由new 运算符创建的数据库中时,它工作得非常好,但不使用 bean:

public static void main(String[] args) {
    ApplicationContext ctx;
    ctx = new ClassPathXmlApplicationContext("context.xml");
    Komputer komputer = (Komputer)ctx.getBean("komputer");

    Dao dao = (Dao)ctx.getBean(Dao.class);
    dao.deleteAll();

    System.out.println("inserting: " + komputer.getTyp());
    dao.save(komputer); // sets everything to null and puts in db

    Komputer new_komputer = new Komputer();
    new_komputer.setTyp("test");

    System.out.println("inserting: " + komputer.getTyp());
    dao.save(new_komputer); // correctly puts in db

    Iterable <Komputer> komputer_iterable = dao.findAll();
    System.out.println("List: ");
    for (Komputer komputer : komputer_iterable) {
        System.out.println("id:" + komputer.getId() + ", typ: " + komputer.getTyp());
    }
}

在控制台打印结果:

inserting: Intel Pentium B970
13:33:38.719 [main] DEBUG o.s.data.mongodb.core.MongoTemplate - Inserting DBObject containing fields: [_class, _id, obwodKola, liczbaLosowa] in collection: komputer
13:33:38.720 [main] DEBUG o.s.data.mongodb.core.MongoDbUtils - Getting Mongo Database name=[lab_test]
inserting: test
13:33:38.726 [main] DEBUG o.s.data.mongodb.core.MongoTemplate - Inserting DBObject containing fields: [_class, _id, typ, obwodKola, liczbaLosowa] in collection: komputer
13:33:38.726 [main] DEBUG o.s.data.mongodb.core.MongoDbUtils - Getting Mongo Database name=[lab_test]
13:33:38.730 [main] DEBUG o.s.data.mongodb.core.MongoTemplate - find using query: { } fields: null for class: class org.zut.lab1.Komputer in collection: komputer
13:33:38.731 [main] DEBUG o.s.data.mongodb.core.MongoDbUtils - Getting Mongo Database name=[lab_test]
List: 
id:56657ca244ae837d313d8b29, typ: null
id:56657ca244ae837d313d8b2a, typ: test

当然,所有第一个对象的属性都设置正确。

dao 类是这样的:

import org.springframework.data.repository.CrudRepository;

public interface Dao extends CrudRepository<Komputer, String> {

}

还有 Komputer 类:

import org.springframework.data.annotation.Id;

public class Komputer {
    @Id
    private String id;
    private Procesor procesor;
    private String typ;
    private float obwodKola; // 2 * pi * r
    private float liczbaLosowa;

    public void setProcesor(Procesor procesor) {
        this.procesor = procesor;
    }

    public Procesor getProcesor() {
        return this.procesor;
    }

    public void setTyp(String typ) {
        this.typ = typ;
    }

    public String getTyp() {
        return this.typ;
    }   

    public float getObwodKola() {
        return obwodKola;
    }

    public void setObwodKola(float obwodKola) {
        this.obwodKola = obwodKola;
    }

    public float getLiczbaLosowa() {
        return liczbaLosowa;
    }

    public void setLiczbaLosowa(float liczbaLosowa) {
        this.liczbaLosowa = liczbaLosowa;
    }

    public String getId() {
        return id;
    }

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

context.xml 文件中的 Bean:

<mongo:mongo id="mongo" host="localhost" port="27017" />

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg ref="mongo" />
    <constructor-arg name="databaseName" value="lab_test" />
</bean>

<mongo:repositories base-package="org.zut.lab1"></mongo:repositories> 

<bean id="procesor" class="org.zut.lab1.Procesor">
    <property name="iloscRdzeni" value="4"/>
    <property name="czestotliwosc" value="300000"/>
</bean>

<bean id="komputer" class="org.zut.lab1.Komputer">
    <property name="liczbaLosowa" value="#{T(java.lang.Math).random() * procesor.czestotliwosc}"/>
    <property name="procesor" ref="procesor"/>
    <property name="obwodKola" value="#{T(java.lang.Math).PI * 2 * procesor.getIloscRdzeni()}"/>
    <property name="typ" value="Intel Pentium B970"/>
</bean>

这让我完全迷失了方向。我做错了什么?

【问题讨论】:

  • Komputer怎么样komputer = (Komputer)ctx.getBean("komputer");放 ?您确定 Komputer 已将 Typ 设置为某个值吗?只需在保存之前打印它。有时我们假设并打破我们的头。或者想想 deleteAll() 实际上并没有删除所有内容,数据库中遗漏了一个空值。在 context.xml 中显示 bean 定义怎么样?
  • @TheNeoNoirDeveloper 我已经编辑了帖子。所有值都设置正确,可以在控制台中打印。每次调用 deleteAll() 方法时都会清除数据库。
  • 保存前请打印以确保
  • @Valijon 我添加了一些额外的调试数据。
  • 您能否访问lab_test 数据库并手动删除那里的所有数据以更加确定。如果 deleteAll() 实际上删除了所有。

标签: java spring mongodb


【解决方案1】:

基本上,Spring AOP 为 id komputer 的 bean 实例创建一个 Proxy

作为代理,它调用代理方法(例如当您使用Mockito 模拟时),但原始属性为空。如果您使用某个 IDE(例如 Eclipse)调试您的代码,您可以看到发生了什么。

因此,当您保存代理实例时,它需要 attribute's 值,而不是 getters

如果你从上下文中删除aop proxy,komputer bean 将被正确保存。

<aop:aspectj-autoproxy/>
....
<aop:config>
    <aop:aspect ref="profiler">
        <aop:pointcut expression="execution(* org.zut.lab1.Komputer.oblicz(..)) and args(czas)"
         id="test"/>
        <aop:around method="profile" pointcut-ref="test"/>
    </aop:aspect>
</aop:config>

【讨论】:

猜你喜欢
  • 2012-01-25
  • 2019-04-23
  • 2018-01-03
  • 2017-07-12
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多