0

我有两个表:组织(父)和部门(子)。

存在一对多关系,仅在组织表中提及。

@Entity
@Table(name="TBL_STD_ORGANIZATION")
public class Organization implements Serializable  {
  @Id
  @GeneratedValue
  @Column(name="FLD_ORG_ID")
  private Long organizationId;

  @Column(name="FLD_ORG_NAME")
  private String orgName;

  @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
  private java.util.List<Department> listOfDepartMents = new java.util.ArrayList<Department>();
}

下面是部门类:

    @Entity
    @Table(name="TBL_STD_DEPARTMENT")
    public class Department implements Serializable { 

    @Id
    @GeneratedValue
    @Column(name = "FLD_DEPARTMENT_ID")
    private Long departmentId;

    @Column(name = "FLD_DEPARTMENT_NAME")
    private String departmentName;
    }

我在父表中写了关系,因为它休眠创建第三个表。现在,我必须检索以“sa”关键字开头并在特定组织中的部门。所以我想要 HQL 或 SQL 查询查询。我不明白如何编写如此复杂的查询。有什么建议么?

4

1 回答 1

1

我相当肯定 HQL/JPQL 将是:

SELECT d FROM Organization o JOIN o.listOfDepartMents d WHERE d.departmentName LIKE "sa%"
于 2013-10-10T17:37:23.993 回答