2009-07-19 4 views
2

Hallo jeder Ich habe diese classeHQL Problem

@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 

und

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

wenn ich diese Abfrage verwenden:

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

oder dies:

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

Ich habe diesen Fehler:

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

aber, wenn die Verwendung dieser Code:

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

I bedeutet diese Art von HQL nur für Primärschlüssel arbeiten? oder mein Maping Problem? gibt es eine Möglichkeit, dass ich andere Feld (außer Primärschlüssel) Formular verbindbare Tabelle verwenden kann?

Antwort

3

Hibernate erwartet Sie den Eigenschaftsnamen anstelle der Datenbankspalte Namen zu verwenden, das heißt branchName insted bname und userName statt username.

Also, wenn Sie Ihre Anfragen an

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

und

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

, alles sollte wie erwartet funktionieren.

+0

Tanx Man: P Ich bin Neuling In Hibernate und weiß nicht viele Dinge Tanx für Ihre schnelle Antwort – Am1rr3zA