【问题标题】:HQL query for the unary table一元表的 HQL 查询
【发布时间】:2013-05-26 12:42:54
【问题描述】:

我有一个如下的 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 类中创建相应的方法。

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

【问题讨论】:

    标签: sql hibernate jakarta-ee nhibernate-mapping hql


    【解决方案1】:

    我自己找到了解决问题的方法。它只是另一个对象中的对象。因此我需要先查询第一个对象,然后才能访问第二个对象

    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());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-26
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 2018-02-13
      相关资源
      最近更新 更多