当数据库中有数据时,为什么在休眠状态下会出现NoResultException?

时间:2019-01-02 15:08:29

标签: java spring hibernate hql

我试图通过电子邮件使用HQL从处于休眠状态的MySql数据库中获取实体。但是,即使数据库中存在javax.persistence.NoResultException: No entity found for query,我也会得到它。我正在尝试使用getSingleResults()通过电子邮件检索它,并且电子邮件在数据库中是唯一的,因此我确信只会有一个结果。

请参考以下代码:

@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();
    }

请为实体类参考以下代码:

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);
    }

}

如果有帮助,控制器中的代码:

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

请求网址为:http://localhost:8080/api/users/xlz@wwe.com

请帮助! 预先感谢。

1 个答案:

答案 0 :(得分:0)

尝试定义路径变量的名称。 请将@PathVariable String email替换为@PathVariable(value="email") String email

根据上述评论,您必须在请求xlz%40wwe.com中对电子邮件进行编码 但没有必要用Java代码对其进行解码。它应该无需解码即可工作。