Announcement Announcement Module
Collapse
No announcement yet.
Frusteration & Confusion Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Frusteration & Confusion

    I have been attempting to write a simple application using Spring Data & Mongo DB for about the last 24 hours.

    I have come across a number of different internet postings and view the examples that are provided by spring. However from what I can see is there is a large number of different approaches to accomplishing this task.

    These solutions range from using DI to setup the standard mongo libraries and making direct calls to using Repositories in with Spring.

    The later of the 2 is what I really wish to accomplish as it is the most Spring(ish) and will probably be better supported moving forward. Unfortunately I can not seem to find a complete example of implementing repositories.

    So far I have proceeded to the following (Posting code below)
    PHP Code:
    @Document
    public class User implements Serializable{
        @
    Id
        
    private String  emailAddress;
        @
    Indexed
        
    private String  erpId;
        private 
    String  firstName;
        private 
    String  lastName;
        private 
    String  passwordEnc;
        private 
    Boolean active;

        
    //Getters and Setters below

    Question #1: @Id does not seem to exist anywhere in Spring or Mongo libraries from what I can see, should javax.persistence.Id be used?
    PHP Code:
    @Repository
    public interface IUserRepository extends MongoRepository<User,String>{
        public 
    User findByErpId(String erpId);
        public 
    User findByEmail(String emailthrows UsernameNotFoundExceptionDataAccessException;

    PHP Code:
    @Repository
    public class UserRepository implements IUserRepository {
        @Autowired
        MongoTemplate mongoTemplate;

       
        private Boolean unique(String emailAddress) {
            return mongoTemplate.findOne(Query.query(Criteria.where("emailAddress").is(emailAddress)), User.class) == null;
        }
        public User findByEmail(String emailAddress) {
            return mongoTemplate.findOne(Query.query(Criteria.where("emailAddress").is(emailAddress)), User.class);
        }
        public User findByErpId(String erpId) {
            return mongoTemplate.findOne(Query.query(Criteria.where("erpId").is(erpId)), User.class);
        }
        public List<User> save(Iterable<? extends User> iterable) {
            List<User> saved = new ArrayList<User>();
            for(User user : iterable) {
                if(unique(user.getEmailAddress())) {
                    mongoTemplate.save(user);
                    saved.add(user);
                }
            }
            return saved;
        }

        public List<User> findAll() {
            return mongoTemplate.findAll(User.class);
        }

        public List<User> findAll(org.springframework.data.domain.Sort sort) {
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }
    }
    Question #2: org.springframework.data.domain.Sort does not seem to exist, surely I am missing something (library, concept, common sense)
    Question #3: Given the API I am not clear how I would implement using the Sort object passed in as it does not seem to be supported
    by the Query object (I.E. no query.setSort(sort))

    Thank you in advance for your assistance.

  • #2
    I have resolved #2, I was simply missing the spring data commons libraries.

    However other questions remain open.

    Comment

    Working...
    X