JPA Query org.hibernate.QueryException:无法解析属性

时间:2015-03-17 20:54:07

标签: hibernate jpa

我在下列表格中有@OneToOne双向关系:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table
public class StudentAccount implements DomainModel {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer idstudentaccount;
    @OneToOne(optional = false)
    @JoinColumn(name = "idstudent")
    private Student student;
    private String username;
    private String password;
    //getters and setters
}



@Entity
@Table(name = "student")
public class Student implements DomainModel {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer idStudent;
    private String firstname;
    private String lastname;
    @OneToOne(mappedBy = "student", cascade = CascadeType.ALL, targetEntity=StudentAccount.class)
    private StudentAccount studentAccount;
    //getters and setters

如果用户名存在于数据库中,如何编写查询? 这就是我的尝试:

@Transactional
@Override
public boolean usernameAvailability(String username, Integer studentId) {

    Query checkUsername = getEntityManager()
            .createQuery(
                    "SELECT COUNT(*) FROM StudentAccount s WHERE s.username=:usernameParam AND s.idstudent<>:accIdParam");
    checkUsername.setParameter("usernameParam", username);
    checkUsername.setParameter("accIdParam", studentId);
    long count = (long) checkUsername.getSingleResult();
    if (count > 0) {
        return true;
    }
    return false;
}

我得到以下例外:

java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: idstudent of: com.af.domain.StudentAccount

1 个答案:

答案 0 :(得分:3)

那应该是s.student.idstudent<>:accIdParam

请注意,s引用StudentAccount具有嵌套student属性,并具有进一步嵌套的idStudent属性