2017-12-27 12 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 ...'。用户实体中的属性名称。 –