2

I tried to create custom query using @Query annotation. But it throws some error.

This is my Repository

package com.springboot.springmongodb.user;
import java.util.List;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

public interface UserRepository extends CrudRepository<User, String>{
    @Override
    User findOne(String id);

    @Transactional(readOnly=false)
    @Query("SELECT user FROM User user WHERE user.username = :username")
    public List<User> findByUsername(@Param("username") String username);

}

I want to create custom query for check username.

Here the error which I'm getting.

2017-12-04 14:46:10.413 WARN 1452 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDetailsService': Unsatisfied dependency expressed through field 'userRep'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is com.mongodb.util.JSONParseException: SELECT user FROM User user WHERE user.username = :username ^

5
  • can you show us the full exception? Commented Dec 4, 2017 at 9:14
  • 1
    It seems that you are declaring a SQL Query as mongodb. A mongo query should look like this: @Query("{ 'firstname' : ?0 }") Commented Dec 4, 2017 at 9:15
  • updated my full exception @YCF_L Commented Dec 4, 2017 at 9:18
  • Would you try to code format it? The line breaks are surely not original. Commented Dec 4, 2017 at 9:22
  • Yea I have mode changes for stackoverflow Commented Dec 4, 2017 at 10:56

1 Answer 1

3

The exception tells that the @Query you gave gets treated as JSON. Taking a look at your imports leads us to the reason:

import org.springframework.data.mongodb.repository.Query; 

While your query SELECT user FROM User user WHERE user.username = :username would be valid for JPA (SQL), it's invalid for MongoDB. Either change your import to fit your database or change the query to fit it.

A valid mongoDB looks like from the Spring Data MongoDB example:

public interface PersonRepository extends MongoRepository<Person, String>

  @Query("{ 'username' : ?0 }")
  List<Person> findByThePersonsFirstname(String firstname);

}

...And a valid JPA statement from the Spring Data JPA example:

public interface UserRepository extends JpaRepository<User, Long> {

  @Query("select u from User u where u.username = ?1")
  User findByEmailAddress(String emailAddress);
}

This would need the import org.springframework.data.jpa.repository.Query

Sign up to request clarification or add additional context in comments.

11 Comments

Yes I have given the same import which you have mentioned.
That's the problem I think. What's your database?
Here am using Crudrepository interface
Absolutely mongodb
Than your query is invalid. Try change it as I suggested.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.