gpt4 book ai didi

java - hibernate : Retrieve objects using Restrictions

转载 作者:行者123 更新时间:2023-11-30 11:26:28 25 4
gpt4 key购买 nike

我最近开始将 hibernate 与 java 结合使用。

我有一个像这样的实体员工:

final public class Staff {
@Id @GeneratedValue
private int staffId;
private String firstName;
private String lastName;
private String Email;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "auth_id", nullable = false)
private Authentication auth;
//And all the getters and setters
}

我的身份验证类:

final public class Authentication {

@Id @GeneratedValue
@Column(name="auth_id")
private int authId;
private String pwd;
private String secretQuestion;
private String secretAnswer;
//And all the getters and setters
}

对于登录,我需要检查电子邮件和密码。所以我创建了一个 HashMap :

Map<String, String> requirements = 
new HashMap<String, String>();
requirements.put("Email", "example@example.com");
requirements.put("auth.pwd", "password");

然后运行查询:

Criteria criteria = session.createCriteria(currentClass);
criteria.add(Restrictions.allEq(requirements));
staffList = (ArrayList<Staff>) criteria.list();

但是我得到了这个异常:

org.hibernate.QueryException: could not resolve property: auth.pwd of: models.Staff

我的问题是:我们如何为实体内部的实体设置限制?

如果我遗漏了我应该提及的任何其他信息,请告诉我。谢谢!

最佳答案

Staff 实体的 auth 属性使用别名:

Criteria crit = session.createCriteria(Staff.class, "staff");
crit.createAlias("auth", "a"); // Create alias for auth
crit.add(Restrictions.like("a.propertyName", propertyValue+"%"));

关于java - hibernate : Retrieve objects using Restrictions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19806231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com