如何编写Criteria查询?

时间:2014-03-25 07:33:29

标签: java hibernate jpa hibernate-criteria

我有两个名为EmployeeEntityEmployeeDeparmentEntity的实体。如果我获取该员工,我想要获取所有活跃的部门。我已经为这两个实体使用了双向映射。

EmployeeDeparment

@Entity
public class EmployeeDeparment implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "employeeid")
    private EmployeeEntity employeeentity;

    @Temporal(javax.persistence.TemporalType.DATE)
    private Date addeddate;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date modifieddate;
    @ManyToOne
    private EmployeeEntity reportingTo;

    @ManyToOne
    private DepartmentMaster departmentID;
    private boolean status;

}

1 个答案:

答案 0 :(得分:0)

根据您的情况,我提出了以下标准的hibernate查询,这可能对您的情况有所帮助。

Criteria criteria = session.createCriteria(EmployeeDeparment.class);
criteria.add(Expression.eq("status",true));  //all active department
criteria.add(Expression.eq("employeeentity.id",1234));  //set the employee id 

List lisObj = criteria.list();


Iterator it=lisObj .iterator();

while(it.hasNext())
        {
            EmployeeDeparment p=(EmployeeDeparment)it.next();
            p.get(.........);
            p.get(.........);
            p.get(.........);
        }

如果您有任何进一步的问题,请告诉我......