如何在ManyToOne关系中从“很多”OBJECT中获得“一个”对象?请帮我?

时间:2013-08-06 07:23:17

标签: sql hibernate jpa

这是我的java代码:三个实体,包括CompanyTypeRelation ,CompanyType,Department

public class CompanyTypeRelation  implements java.io.Serializable {

   private static final long serialVersionUID = 4227115995926533087L;
   // Fields    

    private long id;
    private CompanyType companyType;
    private Department department;
    private long operator;
    private String operateTime;
    // the relationship
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="company_type_id", nullable=false)

    public CompanyType getCompanyType() {
       return this.companyType;
    }

    public void setCompanyType(CompanyType companyType) {
      this.companyType = companyType;
    }
        // the relationship
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="company_id", nullable=false)

    public Department getDepartment() {
       return this.department;
    }

    public void setDepartment(Department department) {
    this.department = department;
    }
    ..............
    other code:
}

这是我的查询sql:

SELECT c 
FROM com.hna.dt.orm.CompanyTypeRelation c 
LEFT JOIN c.companyType ct LEFT JOIN c.department cd

我可以通过上面的sql从departmentName获取Department吗?

1 个答案:

答案 0 :(得分:0)

假设departmentName位于department表中,只需将cd.name(或列名称)添加到选择列表中。