What are some of the real world example where JPA2 Criteria API is more preferable?

后端 未结 4 1298
不思量自难忘°
不思量自难忘° 2020-12-13 10:15

I have taken a look at JPA 2.0 Criteria API, but I found it to be too cumbersome unlike Hibernate Criteria. Is there any good reason to use JPA 2.0 Criteria API rather than

4条回答
  •  爱一瞬间的悲伤
    2020-12-13 10:35

    JPA 2.0 Criteria API is The Object-based API for building queries. I think it can play a good job when you have a dynamic query which can become more readable as follows

    cq.select(...)
      .where(...)
      .orderBy(...)
      .groupBy(...);
    

    But when using static query prefer To use an externalized, maintainable and readable file

    
        ...
        
           
               
           
        
        
           
               
           
        
        ...
    
    

    If you have a modularized application use one xml file for each module as follows

    br
       com
           ar
               moduleA
                   model
                       repository
                           moduleA.xml
               moduleB
                   model
                       repository
                           moduleB.xml               
               moduleC
                   model
                       repository
                           moduleC.xml
    

    Then you define your mappinf-file element

    br/com/ar/moduleA/model/repository/moduleA.xml
    br/com/ar/moduleB/model/repository/moduleB.xml
    br/com/ar/moduleC/model/repository/moduleC.xml
    

提交回复
热议问题