我在下列表格中有@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
答案 0 :(得分:3)
那应该是s.student.idstudent<>:accIdParam
请注意,s
引用StudentAccount
具有嵌套student
属性,并具有进一步嵌套的idStudent
属性