Hibernate无法解析属性

时间:2016-03-14 11:22:21

标签: java hibernate

Hibernate无法解析我的属性,我无法弄清楚为什么会这样。尝试查看其他stackoverflow问题,但我无法解决它。

这是我的两个班级。

用户:

@Entity
@Table(name = "users")
public class User {

    @Id
    @Column(name = "user_id")
    @JsonIgnore
    private int userId;

    @ManyToOne
    @JoinColumn(name = "ad_object_id")
    private ADUser activeDirectoryUser;

    @Column(name = "created_at")
    private DateTime createdAt;

    @Column(name = "updated_at")
    private DateTime updatedAt;

    public User() {}

    public User(int userId, ADUser activeDirectoryUser, DateTime createdAt, DateTime updatedAt) {
        this.userId = userId;
        this.activeDirectoryUser = activeDirectoryUser;
        this.createdAt = createdAt;
        this.updatedAt = updatedAt;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public ADUser getActiveDirectoryUser() {
        return activeDirectoryUser;
    }

    public void setActiveDirectoryUser(ADUser activeDirectoryUser) {
        this.activeDirectoryUser = activeDirectoryUser;
    }

    public DateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(DateTime createdAt) {
        this.createdAt = createdAt;
    }

    public DateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(DateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
}

ADUser便有:

@Entity
@Table(name = "ad_users")
public class ADUser {

    @Id
    @Column(name = "ad_object_id")
    @JsonIgnore
    private String activeDirectoryId;

    @Column(name = "email")
    private String email;

    @Column(name = "full_name")
    private String fullName;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @ManyToOne
    @JoinColumn(name = "ad_group_id")
    private ADGroup classInfo;

    public ADUser() {}

    public ADUser(String activeDirectoryId, String email, String fullName, String firstName, String lastName, ADGroup classInfo) {
        this.activeDirectoryId = activeDirectoryId;
        this.email = email;
        this.fullName = fullName;
        this.firstName = firstName;
        this.lastName = lastName;
        this.classInfo = classInfo;
    }

    public String getActiveDirectoryId() {
        return activeDirectoryId;
    }

    public void setActiveDirectoryId(String activeDirectoryId) {
        this.activeDirectoryId = activeDirectoryId;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public ADGroup getClassInfo() {
        return classInfo;
    }

    public void setClassInfo(ADGroup classInfo) {
        this.classInfo = classInfo;
    }
}

我正在尝试使用以下方法查找用户。

public Optional<User> findByEmail(String email){
    Criteria criteria = criteria().add(Restrictions.eq("activeDirectoryUser.email", email));

    return Optional.fromNullable(uniqueResult(criteria));
}

这是我收到的错误。

  

org.hibernate.QueryException:无法解析属性:activeDirectoryUser.email:-removed-.objects.User

1 个答案:

答案 0 :(得分:1)

您需要为关联使用别名

Criteria criteria = criteria().createAlias("activeDirectoryUser",
    "activeDirectoryUser").add(Restrictions.eq("activeDirectoryUser.email", email));