sreenivas
sreenivas

Reputation: 401

Get List Of documents based on my query using spring and mongodb

My Model Class look like this:

 package io.springBoot.ProductionDetails;
    import org.springframework.data.annotation.Id;

    public class ProductionDetailsModel {

    //Created an entity for ProductDetails
    @Id
     private String productId;
     private String name;
     private String description;
     private String image; //Alway's in Binary data 
     private String sellerUserId;


    public ProductionDetailsModel() {

    }

    //Constructors
    public ProductionDetailsModel( String productId, String name, String description, String image, String sellerUserId) {
        super();
        this.productId = productId;
        this.name = name;
        this.description = description;
        this.image = image;
        this.sellerUserId = sellerUserId;
    }

    //Getter and Setter Methods for objects
    public String getProductId() {
        return productId;
    }
    public void setProductId(String productId) {
        this.productId = productId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getImage() {
        return image;
    }
    public void setImage(String image) {
        this.image = image;
    }
    public String getSellerUserId() {
        return sellerUserId;
    }
    public void setSellarUserId(String sellerUserId) {
        this.sellerUserId = sellerUserId;
    }



I am able to get single document based on this query:

    public void getProductDetails(String Id) {
        return productdetialsRepository.findOne(id);    
    }   

I want to pass a perameter like: sellerUserId and get all the documents based on sellerUserId form the Mongodb. How can i achieve this? Any Help please?

Add a method like this in ProductDetailsRepository

 public interface ProductdetialsRepository extends MongoRepository<ProductionDetailsModel, String>{ 
    List< ProductionDetailsModel> findAllBySellerUserId(final String  sellerUserId); 
    }

In my Controller I added

@RequestMapping(method=RequestMethod.GET, path = "/{sellerUserId}")
    void getProductDetails(@PathVariable String sellerUserId) {
    System.out.println(sellerUserId);
    productdetialsRepository.findAllBySellerUserId(sellerUserId);
    }

and also i done experiment with this

Query query = new Query();
query.addCriteria(Criteria.where("id").is(sellerUserId));
List<ProductionDetailsModel> productionDetailsList =    productdetialsRepository.find(query ProductionDetailsModel.class);

But i am getting null pointer Exception any one can please help me?

Upvotes: 0

Views: 1989

Answers (2)

user2235729
user2235729

Reputation: 69

You need to write method in your Repository Interface

List< ProductionDetailsModel> findAllBySellerUserId(final String sellerUserId);

and call this method in your serviceImpl class

List<ProductionDetailsModel> findAllBySellerUserId(final String sellerUserId){
    return productdetialsRepository.findBySellerUserId(sellerUserId); 
}

And other better solution as you are using mongo with spring is to use mongoOperations(which is provided by spring framework)

Query query = new Query();
query.addCriteria(Criteria.where("id").is(sellerUserId));
List<ProductionDetailsModel> productionDetailsList = mongoOperation.find(your query, ProductionDetailsModel.class);

Upvotes: 1

pvpkiran
pvpkiran

Reputation: 27018

Add a method like this in ProductDetailsRepository

List<ProductDetails> findBySellerUserId(String sellerUserId)

Spring data will formulate the query based on method name and get the result.

Upvotes: 0

Related Questions