1. 程式人生 > >hibernate多表連線 查詢的解決方案

hibernate多表連線 查詢的解決方案

大家在用hibernate的時候總會遇到多表連線的查詢,遇到這種問題 總是各種糾結。

方案1:建立檢視 ,事先在資料庫裡面建立檢視。然後建立這個檢視的實體類,指定一個主鍵。然後建立這個檢視的查詢.

方案2:給實體類加臨時屬性HQL查詢補充。

上程式碼:

實體類:

@Entity
@Table(name = "FLOW_RU_INFO")
public class FlowRuInfo implements java.io.Serializable {

    // Fields

    private String id;
    private String deptid;
    private String processinstanceid;
    private String content;
    private String creator;
    private Date createtime;
    private String flowtype;
    private String flowcode;
    private String formid;
    private String state;
    private String currentuserid;
    private String pflowtype;
    private String formNo;
    
    //臨時屬性
    private String task;
    private String lastAudiUser;
    private String nextUser;
    private String userName;
    private String deptName;


    // Constructors

    /** default constructor */
    public FlowRuInfo() {
    }

    

    public FlowRuInfo(String id, String deptid, String processinstanceid,
            String content, String creator, Date createtime, String flowtype,
            String flowcode, String formid, String state, String currentuserid,
            String pflowtype, String formNo, String task, String lastAudiUser,
            String nextUser, String userName, String deptName) {
        super();
        this.id = id;
        this.deptid = deptid;
        this.processinstanceid = processinstanceid;
        this.content = content;
        this.creator = creator;
        this.createtime = createtime;
        this.flowtype = flowtype;
        this.flowcode = flowcode;
        this.formid = formid;
        this.state = state;
        this.currentuserid = currentuserid;
        this.pflowtype = pflowtype;
        this.formNo = formNo;
        this.task = task;
        this.lastAudiUser = lastAudiUser;
        this.nextUser = nextUser;
        this.userName = userName;
        this.deptName = deptName;
    }



    // Property accessors
    @GenericGenerator(name = "generator", strategy = "uuid.hex")
    @Id
    @GeneratedValue(generator = "generator")
    @Column(name = "ID", unique = true, nullable = false, length = 32)
    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Column(name = "DEPTID", length = 32)
    public String getDeptid() {
        return this.deptid;
    }

    public void setDeptid(String deptid) {
        this.deptid = deptid;
    }

    @Column(name = "PROCESSINSTANCEID", length = 100)
    public String getProcessinstanceid() {
        return this.processinstanceid;
    }

    public void setProcessinstanceid(String processinstanceid) {
        this.processinstanceid = processinstanceid;
    }

    @Column(name = "CONTENT", length = 800)
    public String getContent() {
        return this.content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Column(name = "CREATOR", length = 32)
    public String getCreator() {
        return this.creator;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }


    @Column(name = "CREATETIME", length = 7)
    public Date getCreatetime() {
        return this.createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    @Column(name = "FLOWTYPE", length = 32)
    public String getFlowtype() {
        return this.flowtype;
    }

    public void setFlowtype(String flowtype) {
        this.flowtype = flowtype;
    }

    @Column(name = "FLOWCODE", length = 32)
    public String getFlowcode() {
        return this.flowcode;
    }

    public void setFlowcode(String flowcode) {
        this.flowcode = flowcode;
    }

    @Column(name = "FORMID", length = 32)
    public String getFormid() {
        return this.formid;
    }

    public void setFormid(String formid) {
        this.formid = formid;
    }

    @Column(name = "STATE", length = 1)
    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    @Column(name = "CURRENTUSERID", length = 32)
    public String getCurrentuserid() {
        return this.currentuserid;
    }

    public void setCurrentuserid(String currentuserid) {
        this.currentuserid = currentuserid;
    }

    @Column(name = "PFLOWTYPE", length = 32)
    public String getPflowtype() {
        return this.pflowtype;
    }

    public void setPflowtype(String pflowtype) {
        this.pflowtype = pflowtype;
    }
    
    
    @Column(name = "FORM_NO", length = 32)
    public String getFormNo() {
        return formNo;
    }

    public void setFormNo(String formNo) {
        this.formNo = formNo;
    }

    @Transient   //這種是臨時屬性的註解
    public String getTask() {
        return task;
    }

    public void setTask(String task) {
        this.task = task;
    }

    @Transient
    public String getLastAudiUser() {
        return lastAudiUser;
    }

    public void setLastAudiUser(String lastAudiUser) {
        this.lastAudiUser = lastAudiUser;
    }
    @Transient
    public String getNextUser() {
        return nextUser;
    }

    public void setNextUser(String nextUser) {
        this.nextUser = nextUser;
    }
    @Transient
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    @Transient
    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

}

HQL查詢語句:

//hql多表連線 拼裝實體
String hql="select new com.entity.flow.FlowRuInfo(t.id,t.deptid,t.processinstanceid,"
            +"t.content, t.creator, t.createtime, t.flowtype,"
            +"t.flowcode, t.formid, t.state, t.currentuserid,"
            +"t.pflowtype, t.formNo, t.creator, t.creator,"
            +"t.creator, u.empName, d.depName) from FlowRuInfo t,Department d,User u where  t.creator=u.userid and d.depId=t.deptid  order by t.createtime desc";