2017-12-27 344 views
0

我想從表中得到一個單一的價值,但在那裏如何使用Hibernate

有錯誤報道讓我考慮一個簡單的SQL語法來獲得從表中的用戶ID。


選擇從tbl_name其中電子郵件= '[email protected]' 身份證;

現在我想返回使用用戶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

@JohnJoe - 向我顯示錯誤 – Hoax

+0

爲什麼使用不推薦的專有標準API來執行此操作,而不是使用簡單的標準JPQL查詢? –

回答

0

我們需要在查詢中使用實體名稱和屬性,而不是表名和列名。因此,以下查詢:

查詢的查詢= session.createQuery( 「選擇u.user_id FROM tbl_userù WHERE u.email =:emailParam」);

應該是

查詢的查詢= session.createQuery( 「選擇u.userId FROM用戶U WHERE u.email =:emailParam」);

+0

感謝您的回答 – Hoax

+1

我認爲您使用的是'SELECT u.user_id ...',但它應該是'SELECT u.userId ...'。用戶實體中的屬性名稱。 –