Search code examples
javaspringhibernatehql

Why do I get NoResultException in hibernate when there is a data in the database?


I trying to get an entity from the MySql database in hibernate using HQL by email. However, I get javax.persistence.NoResultException: No entity found for query even though it exists in the database. I am trying to retrieve it by email using getSingleResults() and the emails are unique in the database so I am sure that there is only going to be a single result.

Please refer the code below:

@Override
    public UserDetails getUser(String email) {

        Session currentSession = sessionFactory.getCurrentSession();

        Query query = currentSession.createQuery("from UserDetails u where u.email = :email ");
        query.setParameter("email", email);
        return (UserDetails) query.getSingleResult();
    }

Please refer the following code for the entity class:

package com.travelplanner.rest.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;

@Entity
@Table(name = "users_details")
public class UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "email")
    private String email;

    @Transient
    private String password;

    @OneToMany(mappedBy = "userDetails", cascade = CascadeType.ALL)
    @JsonIgnore
    @JsonManagedReference
    private List<TravelPlans> travelPlans;

    public UserDetails() {
    }

    public UserDetails(int id, String firstName, String lastName, String email) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<TravelPlans> getTravelPlans() {
        return travelPlans;
    }

    public void setTravelPlans(List<TravelPlans> travelPlans) {
        this.travelPlans = travelPlans;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
    }

    public void add(TravelPlans travelPlan) {
        if (travelPlans == null) {
            travelPlans = new ArrayList<>();
        }

        travelPlans.add(travelPlan);

        travelPlan.setUserDetails(this);
    }

}

The code in controller if it helps:

@GetMapping("/users/{email}")
    public UserDetails authUser(@PathVariable String email) {
        return userService.getUser(email);
    }

The request url is: http://localhost:8080/api/users/[email protected]

Please help! Thanks in advance.


Solution

  • Try to define the name of the path variable. Please replace @PathVariable String email to @PathVariable(value="email") String email .

    According to the above comment You have to encode email in request xlz%40wwe.com but it is not necessary to decode it in java code. It should work without decoding.