2013-07-09 49 views
1

可以在模型中访问DAO。我在MongoDB中描述为集“项目”的模式是这样的:访问模型中的DAO - Spring Data MongoDB

@Component 
@Document(collection = "item") 
public class Item 
{ 

    @Id 
    private ObjectId id; 

    private Integer  authId; 

    @Autowired 
    @Qualifier("mongoItemDao") 
    private AuthorDao dao; 

    public Author getAuthor() 
    { 
     dao.findById(this.authId); 
    } 

    /* Others setters and getters */ 
} 

正如你看到的我是从“项目”集合指的是“作者”集合(1对多关系),我需要在请求时获得确切的对象。我相信Hibernate会在后台做类似的事情。

在APP-context.xml中我有

<context:component-scan base-package="eu.cloudscale.showcase.db.dao.mongo.impl" /> 

在哪里的DAO的实现。

回答

0

应该只是尝试:

@Component 
@Document(collection = "item") 
public class Item 
{ 

    @Id 
    private ObjectId id; 

    private Integer  authId; 

    @DBRef 
    private Author author; 

    /* Others setters and getters */ 
}