Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data MongoDB: set repository collection?

how do I specify the collection I want my repository to be of?

My configuration:

<!-- Database -->
<mongo:mongo id="mongoDb" host="localhost" port="27017"/>
<mongo:db-factory id="mongoDbFactory" mongo-ref="mongoDb" dbname="test"/>

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

<mongo:repositories base-package="de.cochu.springtest.mongodb.repositories"/>

My document:

@Document
public class User {

    @Id String id;
    @Indexed String email;

    ...getters and setters omitted...

}

the repository for the user-collection (is inside the ...mongodb.repositories package). I extended from the CrudRepository:

public interface UserRepository extends CrudRepository<User, String>{
    User findByEmail(String email);
}

and finally, a controller of my spring servlet in which I want to use the UserRepository:

@Controller
public class MyController {

    @Autowire UserRepository repo;

    @RequestMapping("/test.html")
    public String something(HttpServletRequest request) {
        User u = userRepository.findByEmail(request.getParameter("email"));
        ...
    }

}

I set up some example users via console, the mongoTemplate.getCollection("users").count() works fine. The repository finds nothing.

... how does the UserRepository know that I want to search the users in the collection "users" ?/Is this the right way to use the repositories?

... Intellij IDEA does not recognize the autowiring of the UserRepository. Although it displays an error, it compiles and throws no exception. Is there something I can do about that?

like image 484
Eike Cochu Avatar asked Apr 25 '12 17:04

Eike Cochu


People also ask

Can I use spring data JPA with MongoDB?

Yes, DataNucleus JPA allows it, as well as to many other databases. You make compromises by using the JPA API for other types of datastores, but it makes it easy to investigate them.

What is the difference between MongoOperations and MongoTemplate?

MongoTemplate provides a simple way for you to save, update, and delete your domain objects and map those objects to documents stored in MongoDB. You can save, update and delete the object as shown below. MongoOperations is the interface that MongoTemplate implements.

What is @document annotation in spring boot?

Annotation @DocumentThe annotations @Document applied to a class marks this class as a candidate for mapping to the database. The most relevant parameter is value to specify the collection name in the database. The annotation @Document specifies the collection type to DOCUMENT .


1 Answers

This part of the docs may have the answer you're looking for:

http://static.springsource.org/spring-data/data-mongodb/docs/current/reference/html/#mongo-template.save-insert.collection

There are two ways to manage the collection name that is used for operating on the documents. The default collection name that is used is the class name changed to start with a lower-case letter. So a com.test.Person class would be stored in the "person" collection. You can customize this by providing a different collection name using the @Document annotation. You can also override the collection name by providing your own collection name as the last parameter for the selected MongoTemplate method calls.

like image 198
sdouglass Avatar answered Oct 26 '22 18:10

sdouglass