0

我有一个 user_info 表如下

在此处输入图像描述

如您所见,主管列指的是同一张表。

我正在使用 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 类中没有创建相应的方法。

谁能帮我解决这个问题以找到解决方案?

4

1 回答 1

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());
    }
}
于 2013-07-24T21:17:34.147 回答