Announcement Announcement Module
Collapse
No announcement yet.
Declared Queries with Repositories Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Declared Queries with Repositories

    I'm using Spring Data - MongoDB and haven't been unable to figure out from the documentation how to use a Declared Query with a Repository interface. Let's say I have the following:

    Code:
    @Document
    public class MyPojo implements Serializable {
    
        private static final long serialVersionUID = 5704858837896096092L;
    
        @Id
        protected String id;
    
        @Indexed
        @NotBlank
        protected String name;
    
        protected Set<String> tags = new HashSet<String>();
    
        // getters and setters
    }
    
    public interface MyPojoRepository extends PagingAndSortingRepository<MyPojo, String> {
    
        public Page<MyPojo> findByName(String name, Pageable pageable);
        public List<MyPojo> findByName(String name);
    	
    	// findByTags??
    
    }
    I'd like to be able to query instances of MyPojo by their tags attribute, but that would be too complex for a NamedQuery (as far as I know at least). Since the Spring Data documentation mentions that custom queries can be defined, I figured that would be the logical route. The only problem is that I can't find how to actually do that. Section 4.3.2.1. "Query lookup strategies" of the Spring Data docs describes the notion of declared queries, but no indication of how to create them.

    Thanks,
    Jeff
    Last edited by smeags; Aug 25th, 2011, 08:17 AM.

  • #2
    Bumping. Is there anyone out there who can point me at documentation for creating declared queries (whether specific to MongoDB or not)?

    Comment


    • #3
      By the way Mongo queries work in general you should be able to simply do:

      List<MyPojo> findByTags(String tag);
      List<MyPojo> findByTags(List<String> tags);

      where the semantics are actually "has tag" or "has tags" respectively. The documentation for declared queries for MongoDB can be found at [0].

      [0] http://static.springsource.org/sprin...tories.queries

      Comment


      • #4
        Fantastic, looks like I was making it more complicated than it really was. Thanks Oliver!

        Comment

        Working...
        X