如何使用hibernate从表中获取用户标识

时间:2017-12-27 16:19:15

标签: hibernate spring-mvc

我试图从表中获取单个值,但在那里出现错误

让我考虑一个简单的SQL语法。

  

从tbl_name中选择id,其中email ='abc@gmail.com';

现在我想要一个使用hibernate

返回用户ID的方法

这是我到目前为止所尝试的内容

public int getIdByEmail(String email) {
    session = sessionFact.openSession();
    Query query = session.createQuery("SELECT u.user_id FROM tbl_user u WHERE u.email=:emailParam");
    query.setParameter("emailParam", email);
    return (int) query.uniqueResult();
}

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;

错误是 enter image description here

1 个答案:

答案 0 :(得分:1)

我们需要在查询中使用实体名称和属性,而不是表名和列名。所以下面的查询:

  

查询query = session.createQuery(“SELECT u.user_id FROM tbl_user u   WHERE u.email =:emailParam“);

应该是

  

查询query = session.createQuery(“SELECT u.userId FROM User u WHERE   u.email =:emailParam“);