Reputation: 2135
I have @OneToOne
bi-directional relationship between following tables:
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
How to write the query to get if the username exists in the database? This is what i have tried:
@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;
}
and i get the following exception:
java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: idstudent of: com.af.domain.StudentAccount
Upvotes: 2
Views: 6230
Reputation: 19020
That should be s.student.idstudent<>:accIdParam
Remember that s
refer to StudentAccount
with a nested student
property, with a further nested idStudent
property
Upvotes: 6