hibernate查询语言或使用条件?

发布于 2024-09-02 16:56:00 字数 72 浏览 4 评论 0原文

任何人告诉我使用 criteria/hql/sql 进行查询。 要求是用户输入电子邮件或用户名,查询返回密码 来自表用户的用户。

Any one who tell me the query using criteria/hql/sql.
Requirement is that user enter email or username the query return the password of the
user from table user.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凹づ凸ル 2024-09-09 16:56:00

Criteria API 非常适合动态查询生成,并且是我的首选。你可以这样做:

Criteria criteria = session.createCriteria(User.class)
    .setProjection(Projections.property("password"));

if (email != null) {
    criteria.add(Expression.eq("email", email));
}
if (username != null) {
    criteria.add(Expression.eq("username", username));
}
String password = (String) criteria.uniqueResult();

请注意,我有点推断,但你不应该在数据库中存储明确的密码,也不应该通过电子邮件发送密码(这本质上是不安全的)。实际上,密码恢复的常见过程是通过邮件发送具有有限生命周期的链接,允许用户输入新密码。


更新:实际上,您可能不需要这里的动态查询,但我将上面的内容留给参考。

要使用 Criteria API 实现 OR,您可以执行以下操作:

Criteria criteria = session.createCriteria(User.class);
Criterion username = Restrictions.eq("username", usernameOrPassword);
Criterion email = Restrictions.eq("email", usernameOrPassword);
LogicalExpression orExp = Restrictions.or(username, email);
criteria.add(orExp);

在 HQL 中,您可以运行以下查询:

from User s 
where u.username = :usernameOrPassword 
   or u.password = :usernameOrPassword

在这种情况下,无论您选择哪个解决方案,两者都可以完成这项工作。

The Criteria API is very appropriate for dynamic query generation and would have my preference here. You could do something like this:

Criteria criteria = session.createCriteria(User.class)
    .setProjection(Projections.property("password"));

if (email != null) {
    criteria.add(Expression.eq("email", email));
}
if (username != null) {
    criteria.add(Expression.eq("username", username));
}
String password = (String) criteria.uniqueResult();

Note that I'm a bit extrapolating but you shouldn't store clear passwords in database and you shouldn't send passwords by email (which is unsecure by nature). Actually, a common procedure for password recovery is to send a link with a limited lifetime by mail allowing the user to enter a new password.


Update: Actually, you may not need a dynamic query here but I'm leaving the above for reference.

To implement an OR with the Criteria API, you can do something like this:

Criteria criteria = session.createCriteria(User.class);
Criterion username = Restrictions.eq("username", usernameOrPassword);
Criterion email = Restrictions.eq("email", usernameOrPassword);
LogicalExpression orExp = Restrictions.or(username, email);
criteria.add(orExp);

In HQL, you could run the following query:

from User s 
where u.username = :usernameOrPassword 
   or u.password = :usernameOrPassword

In this case, it doesn't matter which solution you choose, both will do the job.

趁微风不噪 2024-09-09 16:56:00

如果您所做的只是获取一个字段,那么您可能只想使用 hql(或者可能是 sql)。

如果您执行标准,我相信您会拉回整个对象,只是为了最终使用一个字段。

编辑:
这是一个非常广泛的问题。
这是一个教程

If all you're doing is fetching one field, you probably just want to go hql (or possibly sql).

If you do criteria, I believe you're pulling back the entire object, just to eventually use one field.

Edit:
That's a really broad question.
Here is a tutorial

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文