简体   繁体   English

休眠-使用条件从两个表中获取数据

[英]hibernate - fetching data from two tables using criteria

I have a table DocMaster which has one to many mapping with DocMovement Table. 我有一个表DocMaster,它与DocMovement Table有一对多的映射。 DocMovement table has columns that contain sending and receiving user id with date of sending and receiving the document as separate columns. DocMovement表的列包含发送和接收用户ID,以及发送和接收文档的日期为单独的列。

//Doc_Mvmnt
    @ManyToOne
    @JoinColumn(name = "BARCODE", nullable=false)
    public Doc_Master doc_master;

    @ManyToOne
    @JoinColumn(name="RECIPIENT_DETAIL")
    private User_Details recipient_detail;

    @ManyToOne
    @JoinColumn(name="SENDER_DETAIL")
    private User_Details sender_detail;

    @Temporal(TemporalType.DATE)
    //@Type(type="date")
    @Column(name="RECIEVING_DATE")
    private Date recieving_date;

    @Temporal(TemporalType.DATE)
    @Column(name="SENDING_DATE")
    private Date sending_date;

//Doc_Master
    @Column(name = "BARCODE", nullable=false, unique=true)
    private String barcode;

    @ManyToOne
    @JoinColumn(name = "DEPT_ID")
    private Department department;

//Department
    @OneToMany(mappedBy= "department")
    private List<Doc_Master> documents = new ArrayList<>();

The problem I am facing is : 我面临的问题是:

The user enters the department and range of dates. 用户输入部门和日期范围。 I want to display all the document movements withing this range of dates for that department.I am unable to write a criteria so that I can fetch the data I want. 我想显示该部门在该日期范围内的所有文档移动。我无法编写标准,因此无法获取所需的数据。

Criteria criteria = session.createCriteria(Doc_Master.class);
criteria.add(Restrictions.eq("department", deptId));
criteria.add(Restrictions.between(/* */, fromDate, toDate)); // what to do here
List<Doc_Master> documents = (List<Doc_Master>)criteria.list();

Please help !!! 请帮忙 !!!

You need to Use createAlias. 您需要使用createAlias。

Criteria criteria = session.createCriteria(Doc_Master.class,"docMaster");
.createAlias("docMaster.docMovement","docMovement")
criteria.add(Restrictions.eq("docMaster.department",deptId));
criteria.add(Restrictions.between("docMovement.recieving_date", fromDate, toDate));

you need to set documentMovement object in documentMaster. 您需要在documentMaster中设置documentMovement对象。

where 哪里

docMovement is object name in docMaster docMovement是docMaster中的对象名称

The user enters the department and range of dates. 用户输入部门和日期范围。 I want to display all the document movements withing this range of dates for that department. 我想显示该部门在此日期范围内的所有文档移动。

First of all, you should query Doc_Mvmnt and not Doc_Master (you should avoid underscores for class names): 首先,您应该查询Doc_Mvmnt而不是Doc_Master (应该避免在类名下使用下划线):

Criteria criteria = session.createCriteria(Doc_Mvmnt.class);

Note that using your current mapping, you can't navigate from master to movement. 请注意,使用当前映射,您无法从母版导航到运动。 It's a uni-directional association from movement to master. 这是从机芯到主机的单向关联。

Next, you are not querying for a department, but a department id: 接下来,您不是要查询部门,而是要查询部门ID:

criteria.add(Restrictions.eq("doc_master.department.id", deptId));

// what to do here //在这里做什么

criteria.add(Restrictions.or(
    Restrictions.between("sending_date", fromDate, toDate),
    Restrictions.between("recieving_date", fromDate, toDate)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM