我有一个使用@OneToOne注释的hibernate实体加入其他实体。
但是当尝试使用标准时,我会得到org.hibernate.QueryException: could not resolve property: username
例外。
我的道法:
public PasswordResetToken getPasswordResetToken(String username) {
Criteria criteria = session.createCriteria(PasswordResetToken.class);
criteria.add(Restrictions.eq("username",username));
List<PasswordResetToken> tokens = criteria.list(); // <- exception
if (tokens.size() > 0) {
return tokens.get(0);
} else {
return null;
}
}
PasswordResetToken实体:
@Entity
public class PasswordResetToken {
private static final int EXPIRATION = 60 * 24;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(targetEntity = User.class, fetch = FetchType.EAGER)
@JoinColumn(nullable = false, name = "username")
private User user;
...
数据库表:
Table: PasswordResetToken
Columns:
id bigint(20) AI PK
expiryDate datetime
token varchar(255)
username varchar(45)
答案 0 :(得分:0)
字段名称为user
criteria.add(Restrictions.eq("user", username));