[英]How to query with email id using Hibernate using Session.get()?
I am just getting started with Hibernate. 我刚刚开始使用Hibernate。 And I'm building a sample application where I'm signing up users by their email address.
我正在构建一个示例应用程序,我通过他们的电子邮件地址注册用户。
And when they log in, they use that email address and password. 当他们登录时,他们会使用该电子邮件地址和密码。 Now, how do I get/retrieve a record/result while querying with their email ids?
现在,如何在查询电子邮件ID时获取/检索记录/结果?
My code looks like this: 我的代码看起来像这样:
Configuration config = new Configuration();
SessionFactory sessionFactory = config.configure().buildSessionFactory();
Session session = sessionFactory.openSession();
try{
Transaction tx = session.beginTransaction();
LoginBean L = (LoginBean)session.get(LoginBean.class,loginBean.getEmail());
}catch(HibernateException HE){
System.out.println(HE.getMessage());
}
Of course the email
field is a String
. 当然,
email
字段是一个String
。 And Session.get()
takes class,integer
as its arguments. Session.get()
以class,integer
为参数。 I do have a primary key of type Integer
but I'd like to do a search/get using the email
field. 我有一个
Integer
类型的主键,但我想使用email
字段进行搜索/获取。
How do I go about it? 我该怎么办呢?
Session.get()
finds an entity by its primary key. Session.get()
通过其主键查找实体。 To find entities via other means, you need to execute queries: 要通过其他方式查找实体,您需要执行查询:
LoginBean user = (LoginBean)
session.createQuery("select lb from LoginBean lb where lb.email = :email")
.setString("email", email)
.uniqueResult();
Queries and JPQL are explained in the reference documentation. 参考文档中解释了查询和JPQL。 Read it.
阅读。
使用表的主键来执行session.get(),如果表的主键不是电子邮件ID,则必须编写查询/ criteria / sql查询以匹配电子邮件密码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.