convert criteria to detached criteria for self join

◇◆丶佛笑我妖孽 提交于 2019-12-11 12:33:21

问题


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;
    }

}

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/35424717/convert-criteria-to-detached-criteria-for-self-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!