2013-05-30 55 views
0

我有一個user_info表中,如下HQL查詢一元表

enter image description here

正如你所看到的主管列是指在同一個表。

我爲我的開發使用了Netbean。我的POJO創建了以下類

package database.hibernate_pojo; 
// Generated May 31, 2013 12:50:13 AM by Hibernate Tools 3.2.1.GA 


import java.util.HashSet; 
import java.util.Set; 
import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import static javax.persistence.GenerationType.IDENTITY; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 
import javax.persistence.UniqueConstraint; 

/** 
* UserInfo generated by hbm2java 
*/ 
@Entity 
@Table(name="user_info" 
    ,catalog="bit_final_year_project" 
    , uniqueConstraints = @UniqueConstraint(columnNames="user_name") 
) 
public class UserInfo implements java.io.Serializable { 


    private Integer userId; 
    private UserInfo userInfo; 
    private String userName; 
    private String firstName; 
    private String secondName; 
    private char department; 
    private String password; 
    private char privilege; 
    private int invalidLogin; 
    private char status; 
    private String signaturePath; 
    private Set<Shipping> shippings = new HashSet<Shipping>(0); 
    private Set<Audit> audits = new HashSet<Audit>(0); 
    private Set<UserInfo> userInfos = new HashSet<UserInfo>(0); 


    public UserInfo() { 
    } 


    public UserInfo(String userName, String firstName, String secondName, char department, String password, char privilege, int invalidLogin, char status) { 
     this.userName = userName; 
     this.firstName = firstName; 
     this.secondName = secondName; 
     this.department = department; 
     this.password = password; 
     this.privilege = privilege; 
     this.invalidLogin = invalidLogin; 
     this.status = status; 
    } 
    public UserInfo(UserInfo userInfo, String userName, String firstName, String secondName, char department, String password, char privilege, int invalidLogin, char status, String signaturePath, Set<Shipping> shippings, Set<Audit> audits, Set<UserInfo> userInfos) { 
     this.userInfo = userInfo; 
     this.userName = userName; 
     this.firstName = firstName; 
     this.secondName = secondName; 
     this.department = department; 
     this.password = password; 
     this.privilege = privilege; 
     this.invalidLogin = invalidLogin; 
     this.status = status; 
     this.signaturePath = signaturePath; 
     this.shippings = shippings; 
     this.audits = audits; 
     this.userInfos = userInfos; 
    } 

    @Id @GeneratedValue(strategy=IDENTITY) 

    @Column(name="user_id", unique=true, nullable=false) 
    public Integer getUserId() { 
     return this.userId; 
    } 

    public void setUserId(Integer userId) { 
     this.userId = userId; 
    } 
@ManyToOne(fetch=FetchType.LAZY) 
    @JoinColumn(name="supervisor") 
    public UserInfo getUserInfo() { 
     return this.userInfo; 
    } 


    public void setUserInfo(UserInfo userInfo) { 
     this.userInfo = userInfo; 
    } 

    @Column(name="user_name", unique=true, nullable=false, length=12) 
    public String getUserName() { 
     return this.userName; 
    } 

    public void setUserName(String userName) { 
     this.userName = userName; 
    } 

    @Column(name="first_name", nullable=false, length=15) 
    public String getFirstName() { 
     return this.firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    @Column(name="second_name", nullable=false, length=15) 
    public String getSecondName() { 
     return this.secondName; 
    } 

    public void setSecondName(String secondName) { 
     this.secondName = secondName; 
    } 

    @Column(name="department", nullable=false, length=1) 
    public char getDepartment() { 
     return this.department; 
    } 

    public void setDepartment(char department) { 
     this.department = department; 
    } 

    @Column(name="password", nullable=false, length=45) 
    public String getPassword() { 
     return this.password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    @Column(name="privilege", nullable=false, length=1) 
    public char getPrivilege() { 
     return this.privilege; 
    } 

    public void setPrivilege(char privilege) { 
     this.privilege = privilege; 
    } 

    @Column(name="invalid_login", nullable=false) 
    public int getInvalidLogin() { 
     return this.invalidLogin; 
    } 

    public void setInvalidLogin(int invalidLogin) { 
     this.invalidLogin = invalidLogin; 
    } 

    @Column(name="status", nullable=false, length=1) 
    public char getStatus() { 
     return this.status; 
    } 

    public void setStatus(char status) { 
     this.status = status; 
    } 

    @Column(name="signature_path", length=45) 
    public String getSignaturePath() { 
     return this.signaturePath; 
    } 

    public void setSignaturePath(String signaturePath) { 
     this.signaturePath = signaturePath; 
    } 
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="userInfo") 
    public Set<Shipping> getShippings() { 
     return this.shippings; 
    } 

    public void setShippings(Set<Shipping> shippings) { 
     this.shippings = shippings; 
    } 
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="userInfo") 
    public Set<Audit> getAudits() { 
     return this.audits; 
    } 

    public void setAudits(Set<Audit> audits) { 
     this.audits = audits; 
    } 
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="userInfo") 
    public Set<UserInfo> getUserInfos() { 
     return this.userInfos; 
    } 

    public void setUserInfos(Set<UserInfo> userInfos) { 
     this.userInfos = userInfos; 
    } 




} 

我想在我的應用程序中顯示主管的名稱。我已經在Workbench中編寫了完美工作的SQL命令。

select e.user_name from user_info e, user_info s where e.user_id=s.supervisor 

但問題是,當我試圖把它寫在HQL存在通過Netbean中的UserInfo類沒有創建相應的方法。

任何人都可以幫我解決這個問題找到解決辦法嗎?

回答

0

我已經找到了我自己的問題的解決方案。它只是另一個對象內的對象。因此我需要查詢第一個對象,然後可以訪問第二個對象

public Iterator getCustomSupervisor(){ 
    String hqlQuery = "FROM UserInfo"; 
    Query query = session.createQuery(hqlQuery); 
    while(query.list().iterator().hasNext()){ 
     //the first object 
     UserInfo ui = (UserInfo) query.list().iterator().next(); 
     //access the second object 
     System.out.println(ui.getUserInfo().getUserName()); 
    } 
}