2

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

1 Answer 1

6

That should be s.student.idstudent<>:accIdParam

Remember that s refer to StudentAccount with a nested student property, with a further nested idStudent property

Sign up to request clarification or add additional context in comments.

4 Comments

I've followed your suggestion and it's not working. The exception is: java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: Student of: com.iquest.af.domain.StudentAccount [SELECT COUNT(*) FROM com.af.domain.StudentAccount s WHERE s.username=:usernameParam AND s.Student.idStudent<>:accIdParam] org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
it's student with lowercase s, not Student. It's the property and not class name: s.student.idStudent
I am trying to update the Student (edit profile) and now i get the following error: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '4' for key 'UK_tr1t4oo8ivgiur0xy63odvvfy'. Do you have any idea what can cause this exception? Because i do not have duplicate entries into the database.
That's a different question really, where you'll have to show the code for the update operation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.