Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Springboot hibernate ORM

First, I am using the springboot 2.5.12 version, and I tried to implement the login function in my project.

I declared the email and password as 'uEmail' and 'uPassword' among the variables of User Entity in the User layer that I created for login. After writing the code and building it, I saw an error that I couldn't create bean. When I looked at it, I found an error in the findByEmailAndPassword function in the JpaRepository library.

The error statement is as follows:

Error creating bean with name 'userController': 
Unsatisfied dependency expressed through field 'userService'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'userService': 
Unsatisfied dependency expressed through field 'userRepository'; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'userRepository' defined in com.lululala.peopleofwork.persistance.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: 
Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract com.lululala.peopleofwork.model.UserEntity com.lululala.peopleofwork.persistance.UserRepository.findByEmailAndPassword(java.lang.String,java.lang.String)! 
Reason: Failed to create query for method public abstract com.lululala.peopleofwork.model.UserEntity com.lululala.peopleofwork.persistance.UserRepository.findByEmailAndPassword(java.lang.String,java.lang.String)! 
No property email found for type UserEntity! Did you mean 'uEmail','UEmail'?; 
nested exception is java.lang.IllegalArgumentException: 
Failed to create query for method public abstract com.lululala.peopleofwork.model.UserEntity com.lululala.peopleofwork.persistance.UserRepository.findByEmailAndPassword(java.lang.String,java.lang.String)! 
No property email found for type UserEntity! Did you mean 'uEmail','UEmail'?

At first, I was wondering if there was a problem with the variable being written in CamelCase, so I changed the variable to SnakeCase, but the same error came out.

Below is my code.

UserEntity.java

@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = "uEmail")})
public class UserEntity {
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id; // Unique id granted to user
    @Column(nullable = false)
    private String uName; // User name
    @Column(nullable = false)
    private String uEmail; // User email(Id)
    @Column(nullable = false)
    private String uPassword; // User password

UserService.java

@Slf4j
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    public UserEntity create(final UserEntity userEntity) {
        if(userEntity == null || userEntity.getUEmail() == null) {
            throw new RuntimeException("Invalid arguments");
        final String email = userEntity.getUEmail();
        if(userRepository.existsByEmail(email)) {
            log.warn("Email already exists {}", email);
            throw new RuntimeException("Email already exists");
        return userRepository.save(userEntity);
    public UserEntity getByCredentials(final String uEmail, final String uPassword) {
        return userRepository.findByEmailAndPassword(uEmail, uPassword);

UserRepository.java

@Repository
public interface UserRepository extends JpaRepository<UserEntity, String> {
    UserEntity findByEmail(String uEmail);
    Boolean existsByEmail(String uEmail);
    UserEntity findByEmailAndPassword(String uEmail, String uPassword);

UserController.java

@Slf4j
@RestController
@RequestMapping("/auth")
public class UserController {
    @Autowired
    private UserService userService;
    //skip signup method 
    @PostMapping("/signin")
    public ResponseEntity<?> authenticate(@RequestBody UserDTO userDTO){
        UserEntity user = userService.getByCredentials(
                userDTO.getUEmail(),
                userDTO.getUPassword());
        if(user != null) {
            final UserDTO responseUserDTO = UserDTO.builder()
                    .uEmail(user.getUEmail())
                    .id(user.getId())
                    .build();
            return ResponseEntity.ok().body(responseUserDTO);
        } else {
            ResponseDTO responseDTO = ResponseDTO.builder()
                    .error("Login failed.")
                    .build();
            return ResponseEntity.badRequest().body(responseDTO);

UserDTO.java

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserDTO {
    private String token;
    private String uEmail;
    private String uName;
    private String uPassword;
    private String id;

I also looked for naming strategies in hibernate ORM, but I didn't get a clear answer. Below is the site I looked for.

https://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#naming

https://www.jpa-buddy.com/blog/hibernate-naming-strategies-jpa-specification-vs-springboot-opinionation/

https://docs.jboss.org/hibernate/orm/5.4/javadocs/org/hibernate/boot/model/naming/Identifier.html

Eventually, I changed the variable name to 'email' and 'password' and solved it, but anyway, what UserRepository does is write queries, and the variable name is at the discretion of the person who writes the code, so I wonder why there is an error.

Any help is appreciated!

This seems to be the issue :

UserEntity findByEmailAndPassword(String uEmail, String uPassword);

Since the method is findByEmailAndPassword , JPA is trying to create the query as : select email,password from User and while doing so , it is looking for attributes email and password in UserEntity.

Since you have defined the attribute name as uEmail it is throwing the indicative error :

No property email found for type UserEntity! Did you mean 'uEmail','UEmail'?; 

The obvious solution will be to just rename the repository method as :

UserEntity findByUEmailAndUPassword(String uEmail, String uPassword);

In conclusion the repository method should match with the entity attribute name.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.