gpt4 book ai didi

hibernate - JPA 查询 org.hibernate.QueryException : could not resolve property

转载 作者:行者123 更新时间:2023-12-02 01:00:35 26 4
gpt4 key购买 nike

我在以下表格之间有@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

最佳答案

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

记住 s引用StudentAccount嵌套 student属性,进一步嵌套 idStudent属性

关于hibernate - JPA 查询 org.hibernate.QueryException : could not resolve property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29109457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com