大家好,我有这些classe
@Entity
@Table(name = "login", uniqueConstraints={@UniqueConstraint(columnNames={"username_fk"})})
public class Login implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue
private int id;
@Column(name = "password", length = 64)
private String password;
@Column(name = "roles", length = 32)
private String roles;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@OnDelete(action=OnDeleteAction.CASCADE)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@JoinColumn(name = "username_fk", nullable=false)
private Branch branch;
//some getter and sette
和
@Entity
@Table(name = "branch", uniqueConstraints = {@UniqueConstraint(columnNames = {"bname", "branch_fk"})})
public class Branch implements Serializable {
@Id
@GeneratedValue
private int id;
@Column(name = "username", length = 64, nullable=false)
private String userName;
@Column(name = "bname", length = 64)
private String branchName;
@Column(name = "officername", length = 64)
private String officerName;
@Column(name = "studcount")
private int studCount;
@Column(name = "blevel", columnDefinition="int default 0")
private int level;
@Column(name = "officeremail", length = 64)
private String officerEmail;
@Column(name = "confirmed", columnDefinition = "tinyint default 0")
private int confirmed;
@OneToOne(mappedBy = "branch", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OnDelete(action=OnDeleteAction.CASCADE)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Login login;
当我使用此查询时:
executeQuery("select l from Login as l inner join l.branch as b where l.branch.bname = ?", username)
或者这个:
executeQuery("select b.login from Branch b where b.username = ?", username)
我收到此错误:
org.hibernate.QueryException: could not resolve property: bname of: Data.Entity.Branch
但使用此代码时:
executeQuery("select b.login from Branch b where b.id = ?", username)
it's return correct result
我的意思是这种类型的HQL只适用于主键吗?或者我的maping有问题? 有什么方法可以使用其他字段(主键除外)形成可连接表吗?
答案 0 :(得分:3)
Hibernate希望您使用属性名称而不是数据库列名称,即branchName
和bname
而不是userName
的{{1}}。
因此,如果您将查询更改为
username
和
executeQuery("select l from Login as l inner join l.branch as b " +
"where l.branch.branchName = ?",
username);
,一切都应该按预期工作。