Spring boot Mongodb创建嵌套文档

时间:2016-11-14 07:18:19

标签: spring mongodb

任何人都可以告诉我我失踪了吗? 我正在尝试使用spring boot mongodb创建一个Mongo集合。

我想创建像这样的东西

{
    "_id":"123456"
    "entity_name":"some name"
    "entity_desc":"some description"
    "events":[
                {"title":"some title", "description":"some description"},
                {"title":"title2", "description":"description2" }
             ]
}

但我得到了这个

{
   "_id":"123456"
"entity_name":"some name"
"entity_desc":"some description"
"events":[ ]
}

我的域类是

@Document
public class Entity {

     @Id
     private BigInteger id;

     private String name, description;

     private List<Event> events=new ArrayList<Event>();

    /* GETTERS AND SETTERS */

} 




public class Event {

    private String titles;
    private String descriptions;

    /* GETTERS AND SETTERS */

}

我的存储库是

public interface MyRepository extends MongoRepository<Entity, String>{

}

控制器

@Controller
public class RootController {

    @Autowired
    private MyRepository mr;

    /* GET and other methods */


      @RequestMapping(value="/", method=RequestMethod.POST)
      public String helloPost(@ModelAttribute Entity entity){



          mr.save(entity);

          return "success";

     }
}

我的jsp表单是

<form:form modelAttribute="entity" role="form">


            <div class="form-group">
             Name <input type="text" id="name" name="name" /><br />
             </div>
             <div class="form-group">
        Description <input type="text" id="description"   name="description" /><br />
        </div>

      <div class="form-group">
        event <input type="text" id="event" name="event" /><br />
        </div>

      <div class="form-group" >
        title <input type="text" id="title" name="title" /><br />
        </div>

        <div class="form-group" >
        description <input type="text" id="description" name="description" /><br />
       </div>


        <div>
        <button type="submit" class="btn btn-default">Submit</button>
        </div>



    </form:form>

1 个答案:

答案 0 :(得分:0)

不应该像下面那样吗?

public class Event {

    private String title;
    private String description;

    /* GETTERS AND SETTERS */

}

编辑:感谢jsp,显然你没有为孩子挑选传递值的方式。 Spring MVC不支持ONGL,所以它不会用点表示法来获取子对象。但是通过设置路径有复杂的机制。请查看它通过表单参数传递子对象列表的answer。所以它可以是一个很好的起点。