convert criteria to detached criteria for self join
Asked Answered
G

2

1

I would like to know if i can convert this criteria into a detached criteria. I am not understanding detached criteria correctly. can some one help.

Criteria crit = sessionC.createCriteria(OP_DOCTOR_VISIT.class, "OPDV1");
crit.createAlias("OPDV1.OP_VISIT", "OPDV2", JoinType.LEFT_OUTER_JOIN, Restrictions.and(Restrictions.eq("OPDV2.FORM", "NEW"), Restrictions.ge("OPDV2.USER_DATETIME", fromdate), Restrictions.le("OPDV2.USER_DATETIME", todate)));
    crit.add(Restrictions.ge("OPDV1.USER_DATETIME", fromdate));
    crit.add(Restrictions.le("OPDV1.USER_DATETIME", todate));
    ProjectionList p1 = Projections.projectionList();
    p1.add(Projections.alias(Projections.count("OPDV1.OP_VISIT_ID"), "TOTAL"));
    p1.add(Projections.count("OPDV2.FORM"));
    p1.add(Projections.alias(Projections.sqlGroupProjection("date(this_.USER_DATETIME) as createdDate", "createdDate", new String[]{"createdDate"}, new Type[]{StandardBasicTypes.DATE}), "DAT"));
    crit.setProjection(p1);

Is it possible to rewrite the above so that I could avoid using "@OneToMany" in my POJO given below.

POJO

@Entity
@Table(name = "OP_DOCTOR_VISIT")
@SQLDelete(sql = "UPDATE OP_DOCTOR_VISIT SET DELETED = 'DELETED' WHERE OP_VISIT_ID = ? and VERSION_UPDATES = ?")
@Where(clause = "DELETED <> 'DELETED'")
public class OP_DOCTOR_VISIT implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "OP_VISIT_ID")
private Long OP_VISIT_ID;

@OneToMany(mappedBy = "OP_VISIT_ID", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@LazyCollection(LazyCollectionOption.EXTRA)
@Fetch(FetchMode.SELECT)
private List<OP_DOCTOR_VISIT> OP_VISIT;

    public Long getOP_VISIT_ID() {
        return OP_VISIT_ID;
    }

    public void setOP_VISIT_ID(Long OP_VISIT_ID) {
        this.OP_VISIT_ID = OP_VISIT_ID;
    }

    public List<OP_DOCTOR_VISIT> getOP_VISIT() {
        return OP_VISIT;
    }

    public void setOP_VISIT(List<OP_DOCTOR_VISIT> OP_VISIT) {
        this.OP_VISIT = OP_VISIT;
    }

}
Griswold answered 16/2, 2016 at 5:47 Comment(0)
C
0

Only the first line where you create your criteria object.

  1. DetachedCriteria allows you to create a query without session. So you do not require session while builiding your query. DetachedCriteria is same as a Criteria except you can create your queries without session.

    DetachedCriteria detachedCriteria = DetachedCriteria.forClass(OP_DOCTOR_VISIT.class);

  2. Finally when you have a session object is available you may execute your query

    `criteria .getExecutableCriteria(session).

Cockle answered 16/2, 2016 at 6:10 Comment(0)
A
0

DetachedCriteria crit = DetachedCriteria.forClass(OP_DOCTOR_VISIT.class, "OPDV1");

The detached criteria allows you to create the query without Session. Then you can execute the search in an arbitrary session.

In fact you should think carefully when use detached criterias using another, or a new, session (no cache, and creation of the session).

They are most useful for make some join conditions, subselects, and to query outside the current session. Another common use is for code reuse.

If you are using Spring and choose to use HibernateTemplate, it doesn't provide createCriteria() method. You will find only **DetachedCriteria.

Ara answered 16/2, 2016 at 7:20 Comment(2)
can u please explain the second sentence. "In fact you should think carefully when use detached criterias using another, or a new, session (no cache, and creation of the session)."Griswold
DetachedCriteria not using any session.So don't use/link it with session.i.e. no cache is available.It is always early loading.Ara

© 2022 - 2024 — McMap. All rights reserved.