views:

154

answers:

1
+1  Q: 

HQL Problem

Hi every one I have these 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

and

@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;

when i use this Query :

executeQuery("select l from Login as l inner join l.branch as b where l.branch.bname = ?", username)

or this:

executeQuery("select b.login from Branch b where b.username = ?", username)

I have get this error:

org.hibernate.QueryException: could not resolve property: bname of: Data.Entity.Branch

but when use this code:

executeQuery("select b.login from Branch b where b.id = ?", username)
it's return correct result

I means this type of HQL just work for Primary key? or my maping have problem? is there any way that I can use other field(except Primary Key) form joinable table?

+2  A: 

Hibernate expects you to use the property name instead of the database column name, i.e. branchName insted of bname and userName instead of username.

So if you change your queries to

executeQuery("select l from Login as l inner join l.branch as b " +
     "where l.branch.branchName  = ?",
     username);

and

executeQuery("select b.login from Branch b where b.userName = ?", username);

, everything should work as expected.

Henning
Tanx Man :PI am newbie In Hibernate and don't know many thingsTanx for your fast answer
Am1rr3zA