0

I have written a method in Java and It returns list according to query. By mistake I wrote allotment_date to allotmentDate but it returns same list after correcting it and in mysql table fields name also allotment_date. I don't understand why allotment_date returning same list.

public List<ShareAllotment> getlis(STUDENT st, Date fromAllotDate, Date toAllotDate, Integer branch_code) {
        Session session = getSessionFactory(st).openSession();
        try {
            Criteria criteria = session.createCriteria(Student.class);
            if (fromAllotDate != null) {
                criteria.add(Restrictions.ge("allotment_date", fromAllotDate));
            }
            if (toAllotDate != null) {
                criteria.add(Restrictions.le("allotment_date", toAllotDate));
            }
            if(branch_code != null && branch_code > 0) 
                criteria.add(Restrictions.eq("branch_code", branch_code));
            return criteria.list();

        } finally {
            session.flush();
            session.close();
        }
    }

public List<ShareAllotment> getlis(STUDENT st, Date fromAllotDate, Date toAllotDate, Integer branch_code) {
        Session session = getSessionFactory(st).openSession();
        try {
            Criteria criteria = session.createCriteria(Student.class);
            if (fromAllotDate != null) {
                criteria.add(Restrictions.ge("allotmentDate", fromAllotDate));
            }
            if (toAllotDate != null) {
                criteria.add(Restrictions.le("allotmentDate", toAllotDate));
            }
            if(branch_code != null && branch_code > 0) 
                criteria.add(Restrictions.eq("branchCode", branch_code));
            return criteria.list();

        } finally {
            session.flush();
            session.close();
        }
    }

Student POGO is

@Entity(name = "student")
@Table(name = "student")
public class Student extends BaseDO{

    public Student() {
    }

    @Column(name = "application_date")
    @DateTimeFormat(pattern = "dd-MMM-yyyy")
    @NotNull(message="Please enter Application Date")
    private Date applicationDate;

    @Column(name = "receipt_date")
    @DateTimeFormat(pattern = "dd-MMM-yyyy")
    @NotNull(message="Please enter Date of Receipt")
    private Date dateofReceipt;

    @DateTimeFormat(pattern = "dd-MMM-yyyy")
    @NotNull(message="Please enter Allotment Date")
    @Column(name = "allotment_date")
    private Date allotmentDate;

    @Column(name = "branch_code")
    private Integer branchCode;

    public void setApplicationDate(Date applicationDate) {
        this.applicationDate = applicationDate;
    }

    public Date getAllotmentDate() {
        return allotmentDate;
    }

    @Transient
    public String getAllotmentDateStr() {
        return allotmentDate != null ? Configuration.DATE_FORMAT.format(allotmentDate): "";
    }

    @Transient
    public String getAllotDateShotStr() {
        DateFormat df = new SimpleDateFormat("dd-MMM-yy"); 
        return allotmentDate != null ? df.format(allotmentDate): "";
    }

    public void setAllotmentDate(Date allotmentDate) {
        this.allotmentDate = allotmentDate;
    }

    public Integer getBranchCode() {
        return branchCode;
    }

    public void setBranchCode(Integer branchCode) {
        this.branchCode = branchCode;
    }

}

please help me. Thanks in advance.

Ravindra Kumar
  • 1,842
  • 14
  • 23
  • 1
    maybe the values that you pass in are null `if (fromAllotDate != null) {` and so that portion of code is not included – Scary Wombat Jul 04 '17 at 07:51
  • @ScaryWombat i have pass all the values and already checked it – Ravindra Kumar Jul 04 '17 at 07:52
  • do both methods produce the same query? – XtremeBaumer Jul 04 '17 at 07:52
  • @XtremeBaumer one query values are on inspect is [allotment_date>=Wed Mar 01 00:00:00 IST 2017, allotment_date<=Fri Jun 30 00:00:00 IST 2017, branch_code=17] and another query values are [allotmentDate>=Wed Mar 01 00:00:00 IST 2017, allotmentDate<=Fri Jun 30 00:00:00 IST 2017, branchCode=17] – Ravindra Kumar Jul 04 '17 at 07:55
  • i was not asking for the values... show the queries – XtremeBaumer Jul 04 '17 at 07:58
  • it is the hibernate naming convention see https://stackoverflow.com/questions/25681386/hibernate-entity-fields-camelcase-database-underscore – Scary Wombat Jul 04 '17 at 07:59
  • @XtremeBaumer on debuging i dont find out the query. – Ravindra Kumar Jul 04 '17 at 08:13
  • check the table maybe you have both columns (fields) ? – Yazan Jul 04 '17 at 08:22
  • @Yazan i have already checked. there is only one column with name is allotment_date – Ravindra Kumar Jul 04 '17 at 08:30
  • @Ravindra Please paste Student class source code, but i only see 2 options: either your fromAllotDate and toAllotDate variables are null and corresponding code is not executed or you have a in your Student class the two attributes (allotmentDate & allotment_date) mapped on the same column – Abass A Jul 04 '17 at 09:05
  • @Ravindra OK so the property name is allotmentDate and it is the one that should be used in criteria, so now run your code in debug mode with `allotment_date` and confirm that this portion of code `criteria.add(Restrictions.ge("allotment_date", fromAllotDate));` is executed, if it is, so you should encounter exception since allotmend_date doen't exists as property name. (I suppose in your BaseDo class you don't have any mapping on allotment_date) – Abass A Jul 04 '17 at 10:29
  • @AbassA my BaseDO is public abstract class BaseDO { protected boolean allBorders= false; protected boolean topBorder= false; protected boolean topBottomBorders= false; protected boolean leftRightBorders= false; protected int color=0; public boolean isTopBorder() { return topBorder; } public void setTopBorder(boolean topBorder) { this.topBorder = topBorder; } public boolean getTopBottomBorders() { return topBottomBorders; } public boolean getAllBorders() { return allBorders; } public int getColor() { return color; } } – Ravindra Kumar Jul 04 '17 at 10:33
  • @Ravindra ok fine , please dont put code in comment section it's unreadable, instead update your post. Have you launched your code in debubg mode ? what is the result ? – Abass A Jul 04 '17 at 10:40
  • yes, same result. – Ravindra Kumar Jul 04 '17 at 11:18

0 Answers0