Hibernate - 查询没有返回任何内容

时间:2017-12-28 08:16:54

标签: hibernate spring-mvc hql

大家好,我是hibernate的新手。我有一个方法,旨在获得一个列。

@Override
public List<User> getUsersByEmail(String email) {
    session = sessionFact.openSession();
    Query query = session.getNamedQuery("User.findByEmail");
    query.setParameter("email", email);

    return query.list();
}

之后,控制器使用返回的值进一步返回JSON数据。它应该返回用户数据但不返回任何内容。

@RequestMapping(value = "/api/customer/customer-id/{email}", method = RequestMethod.GET)
public @ResponseBody List<User> getUserId(@PathVariable String email) {
    return customerDaoImp.getUsersByEmail(email);
}

实体类 的 User.java

    @Entity
@Table(name = "tbl_user", catalog = "lifestyle", schema = "")
@NamedQueries({
    @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u")
    , @NamedQuery(name = "User.findByUserId", query = "SELECT u FROM User u WHERE u.userId = :userId")
    , @NamedQuery(name = "User.findByFullName", query = "SELECT u FROM User u WHERE u.fullName = :fullName")
    , @NamedQuery(name = "User.findByAddress", query = "SELECT u FROM User u WHERE u.address = :address")
    , @NamedQuery(name = "User.findByContact", query = "SELECT u FROM User u WHERE u.contact = :contact")
    , @NamedQuery(name = "User.findByGender", query = "SELECT u FROM User u WHERE u.gender = :gender")
    , @NamedQuery(name = "User.findByDob", query = "SELECT u FROM User u WHERE u.dob = :dob")
    , @NamedQuery(name = "User.findByEmail", query = "SELECT u FROM User u WHERE u.email = :email")
    , @NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password")
    , @NamedQuery(name = "User.findByActive", query = "SELECT u FROM User u WHERE u.active = :active")
    , @NamedQuery(name = "User.findByCreatedDate", query = "SELECT u FROM User u WHERE u.createdDate = :createdDate")})
public class User implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)

    @Column(name = "user_id")
    private Integer userId;

    @NotNull
    @Size(min = 1, max = 256)
    @Column(name = "full_name")
    private String fullName;
    @Size(max = 256)
    @Column(name = "address")
    private String address;
    @Size(max = 30)
    @Column(name = "contact")
    private String contact;
    @Size(max = 10)
    @Column(name = "gender")
    private String gender;
    @Column(name = "dob")
    @Temporal(TemporalType.DATE)
    private Date dob;
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation

    @NotNull
    @Size(min = 1, max = 256)
    @Column(name = "email")
    private String email;

    @NotNull
    @Size(min = 1, max = 256)
    @Column(name = "password")
    private String password;

    @NotNull
    @Column(name = "active", insertable = false)
    private short active;
    @Column(name = "created_date", insertable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdDate;

    public User() {
    }

    public User(Integer userId) {
        this.userId = userId;
    }

    public User(Integer userId, String fullName, String email, String password, short active) {
        this.userId = userId;
        this.fullName = fullName;
        this.email = email;
        this.password = password;
        this.active = active;
    }
//getter and setter
  

http://localhost:8080/LifeStyle/api/customer/customer-id/nishandhungana41@hotmail.com

这是我输入的用于获取值的网址

2 个答案:

答案 0 :(得分:3)

电子邮件必须是URL编码

http://localhost:8080/LifeStyle/api/customer/customer-id/nishandhungana41%40hotmail.com

所以@ char被识别

答案 1 :(得分:1)

我认为这不承认.

尝试更改为:

@RequestMapping(value = "/api/customer/customer-id/{email:.+}", method =  RequestMethod.GET)
public @ResponseBody List<User> getUserId(@PathVariable String email) {