Hibernate Criteria和DetachedCriteria会连接属性或字段

时间:2014-02-07 15:44:44

标签: java hibernate criteria detachedcriteria

我有criteria like

public ArrayList<Student>getStudentsWithPicture(final Student student)
{
    final Criteria criteria = session.createCriteria(Student.class).add(and(prepareForSelect()));
    criteria.add(Subqueries.gt(1L,getDetached);//the students with a less added picture...
    return new ArrayList<Student>(criteria.list());    
}

我需要在表图片中没有图片的学生,但此数据库是遗留的

他们存储了some fields for the student entity

的图片

是的,非常奇怪

我想要这样的东西

SQL

select
    this_.ID as y0_,
    this_.C01 as y1_,
    this_.C02 as y2_,
    this_.C03 as y3_ 
from
    student_table this_ 
where
    (          
        and this_.C11=true
        and 1>=
        (
            select
                count(*)
            from
                PICTURE_TABLE this_ 
            where
                (
                    this_.C03='concatening'+ this_.ID+ this_.C01+this_.C02+this_.C03//the fields of the student
                )
        )       
    )           

这只是一个可以理解的例子,实际查询更糟糕......

你可以看到我希望学生status='true'并且他们在 PICTURE_TABLE 上的匹配较少但是表中的字段C03是通过连接来创建的Student的字段,我也检索了它......

my detached

public DetachedCriteria getWithDetachedMatchStudentWithPictures()
{
    final String concatedFields = ...........how i accomplish this??????.................
    final DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Pictures.class)
       .add(equals("c03",concatedFields))
       .setProjection(addProjection("id"))
       .setResultTransformer(transformer(Pictures.class));           
    return detachedCriteria;
}    

我的问题是。

我可以在运行时连接字段吗? using Criteria A.P.I

有一些方法吗?

非常感谢

1 个答案:

答案 0 :(得分:1)

是的,我们可以在hibernate中联系多列运行时。 我在我心爱的查询中有连续列。

 Query query = session.createQuery("SELECT DISTINCT f.fileid , f.filename,  f.filetype , f.folderpath , max(f.version) from FileBean f  GROUP BY concat(folderpath,filename,'.',filetype)");
  result = query.list();