How to get a user id from table using hibernate

hibernate spring-mvc

576 观看

1回复

476 作者的声誉

I am trying to get a single value from a table but got errors on there

Let me consider one simple sql syntax.


select id from tbl_name where email = 'abc@gmail.com';

Now i want a method that returns a user id using hibernate

here is what i have tried so far

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;

Error is enter image description here

作者: Elon Musk 的来源 发布者: 2017 年 12 月 27 日

回应 1


1

6200 作者的声誉

决定

We need to use entity names and properties in the query, not the table and column names. So the below query:

Query query = session.createQuery("SELECT u.user_id FROM tbl_user u WHERE u.email=:emailParam");

should be

Query query = session.createQuery("SELECT u.userId FROM User u WHERE u.email=:emailParam");

作者: Madhusudana Reddy Sunnapu 发布者: 2017 年 12 月 28 日
32x32