如何将Criteria与关系映射对象一起使用?

时间:2013-08-09 08:00:38

标签: hibernate criteria

我已将关系bbdd表映射到Java Objects。

@Entity
@Table(name = "user")
public class User implements Serializable{

// Another attributes...
@OneToOne
@JoinColumn(name = "id")
private UserAccount userAccount;

// Getters and setters...

}

@Entity
@Table(name = "user_account")
public class UserAccount implements Serializable{

@Id
@GenericGenerator(name = "generator", strategy = "foreign",
        parameters =
        @Parameter(name = "property", value = "user"))
@GeneratedValue(generator = "generator")
@Column(name = "user_id", unique = true, nullable = false)
private int user_id;
// Another attributes...

// Getters and setters...

}

我有一个这样的标准。

Criterion crit = Restrictions.and(
                Restrictions.like("userAccount.email", email),
                Restrictions.like("userAccount.password", MD5.crypt(password)));

但是我收到了这个错误。

org.hibernate.QueryException: could not resolve property: User of: com.mypackage.entity.User

如何通过条件从用户访问UserAccount.email?

1 个答案:

答案 0 :(得分:1)

您必须为每个联接创建新条件:

Criteria criteria = session.createCriteria(User.class)
        .createCriteria("userAccount").add(Restrictions.and(
                Restrictions.like("email", email),
                Restrictions.like("password", MD5.crypt(password)));d