0

I have a table DocMaster which has one to many mapping with DocMovement Table. DocMovement table has columns that contain sending and receiving user id with date of sending and receiving the document as separate columns.

//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 !!!

2

2 Answers 2

1

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):

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:

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)));
2
  • the first criteria that you told, "doc_master.department.id" in my case "doc_master.department.deptId "is not working. the exception i get is "org.hibernate.QueryException: could not resolve property: doc_master.department.deptId of: database.Doc_Mvmnt". Can you please help Mar 22, 2015 at 3:51
  • Maybe it's a typo. Does doc_master.barcode for example in the query work for you?
    – Ori Dar
    Mar 22, 2015 at 14:21
1

You need to Use 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.

where

docMovement is object name in docMaster

2
  • recieving_date property belongs to Doc_Mvmnt class , so I am unable to use recieving_date property for docMaster criteria :/ Mar 22, 2015 at 4:04
  • ya you are right..you try this..."docMovement.recieving_date" Mar 22, 2015 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.