hibernatecreatecriteria

What is the equivalent mysql statement of criteria code shown below


Im tryingto understand the code shown below

Criteria criteria = session.createCriteria(Payables.class);
            criteria.add(Restrictions.eq("companyId", companyId));

        criteria.createAlias("makePayment", "makePayment");

        if (creditorId != null) {
            criteria.createAlias("makePayment.creditor", "creditor");
            criteria.add(Restrictions.eq("creditor.id", creditorId));
        }

            criteria.add(Restrictions.eq("journalEntryId", journalEntryId));

I do know what createCriteria do, but adding createAlias makes it really confusing for me. I've already read the documentations but everything's still a blur.

Could you tell me how does the code above looks like using mysql statement?


Solution

  •     Criteria criteria = session.createCriteria(Payables.class);
        // select * from Payables p;
    
        criteria.add(Restrictions.eq("companyId", companyId));
        // where p.companyId = :companyId
    
        criteria.createAlias("makePayment", "makePayment");
        // inner join Payement makePayment on makePayement.payables_id = p.id
    
        if (creditorId != null) {
            criteria.createAlias("makePayment.creditor", "creditor");
            // inner join Creditor c on c.payement_id = makePayement.id
            criteria.add(Restrictions.eq("creditor.id", creditorId));
            // where c.id = :creditorId
        }
    
        criteria.add(Restrictions.eq("journalEntryId", journalEntryId));
        // where p.journalEntryId = :journalEntryId
    

    So the result is :

        select * from Payables p
        inner join Payement makePayment on makePayement.payables_id = p.id
        inner join Creditor c on c.payement_id = makePayement.id
        where p.companyId = :companyId
        and c.id = :creditorId
        and p.journal_entry_id = :journalEntryId
    

    For the table name, use the value in @Table annotation of the entity, for the column name, use the @Column name value located on field or getter, for the column name used in join, use the value in the @JoinColumn annotation located near the @OneToMany or @ManyToOne annotation