hibernate查询语言或使用条件?
任何人告诉我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Criteria API 非常适合动态查询生成,并且是我的首选。你可以这样做:
请注意,我有点推断,但你不应该在数据库中存储明确的密码,也不应该通过电子邮件发送密码(这本质上是不安全的)。实际上,密码恢复的常见过程是通过邮件发送具有有限生命周期的链接,允许用户输入新密码。
更新:实际上,您可能不需要这里的动态查询,但我将上面的内容留给参考。
要使用 Criteria API 实现 OR,您可以执行以下操作:
在 HQL 中,您可以运行以下查询:
在这种情况下,无论您选择哪个解决方案,两者都可以完成这项工作。
The Criteria API is very appropriate for dynamic query generation and would have my preference here. You could do something like this:
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:
In HQL, you could run the following query:
In this case, it doesn't matter which solution you choose, both will do the job.
如果您所做的只是获取一个字段,那么您可能只想使用 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