An Example for Hibernate Named Query with Aggregate Function
来源:互联网 发布:广发证券软件下载 编辑:程序博客网 时间:2024/06/06 10:57
1. Does entity fit here?
I don't think so, we'd better have a VO or DTO to represent.
2. hbm.xml
<sql-query name="getCustomerInfo">SELECT baseT.customer_id AS customerId, phone, currency, account_type AS accountType, active, customer_type AS customerType, follow_percentages AS followBetPercentages, ( Sum(IF(cust_trans_id IS NULL, 0, IF(accnt_trans_type = 'Credit', 0 - Round(amount, 2), IF(accnt_trans_type = 'Debit', Round(amount, 2), 0)))) ) AS balance FROM (SELECT customerT.customer_id, customerT.phone, customerT.credit_limit, customerT.currency, customerT.account_type, customerT.active, customerT.customer_type, CONVERT(Ifnull(customerT.follow_percentages, '') USING utf8) AS follow_percentages FROM (SELECT customer.customer_id, phone, credit_limit, currency, customer.active, customer_type, account_type, Group_concat(followed_perc SEPARATOR '_') AS follow_percentages ...GROUP BY baseT.customer_id </sql-query>
Noteworthy:
(1) For the <sql-query>, just configure the name and the SQL sentence, simple enough,don't have to set the <return> series.
3. DAO
public CustomerInfo getCustomerInfo(int customerId) {Query q = currentSession().getNamedQuery("getCustomerInfo");String sql = q.getQueryString();SQLQuery sqlQuery = currentSession().createSQLQuery(sql);sqlQuery.addScalar("customerId", StandardBasicTypes.INTEGER).addScalar("phone", StandardBasicTypes.STRING).addScalar("currency", StandardBasicTypes.STRING).addScalar("accountType", StandardBasicTypes.STRING).addScalar("active", StandardBasicTypes.STRING).addScalar("customerType", StandardBasicTypes.STRING).addScalar("followBetPercentages", StandardBasicTypes.STRING).addScalar("balance", StandardBasicTypes.DOUBLE);sqlQuery.setInteger("customerId", customerId);sqlQuery.setResultTransformer(Transformers.aliasToBean(CustomerInfo.class));return (CustomerInfo)sqlQuery.uniqueResult();}Noteworthy:
(1) This DAO is the most error-prone part of this example due to inadequate official documentation and lack of online resource. Just feel it!
4. VO
public class CustomerInfo {private int customerId;private String accountType;private String currency;private String active;private String customerType;private String followBetPercentages;private String phone;private double balance; //setters and getters...}
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath*:applicationContext-persistence.xml")@ActiveProfiles("test")public class NamedQueriesDaoTest {@Autowiredprivate INamedQueriesDao namedQueriesDao;/** * Beware this is an integration test. *///@Ignore@Testpublic void testGetCustomerInfo() {CustomerInfo customerInfo = namedQueriesDao.getCustomerInfo(2);assertEquals(2, customerInfo.getCustomerId());}}
0 0
- An Example for Hibernate Named Query with Aggregate Function
- Hibernate named query examples
- Hibernate query by Example
- An Example of association query
- PostgreSQL aggregate function 2 : Aggregate Functions for Statistics
- PostgreSQL aggregate function 3 : Aggregate Functions for Ordered-Set
- an example for mouse hook
- Hibernate Error in named query (Unknown entity)
- Example #1: Struts with Hibernate
- An example of lib3ds 2.0 with opengl32
- A Brief Introduction to Hibernate for .NET with A Simple Example
- An example for Perl Multi-threads
- an example of use namespace for encapsulation
- an example for ram file system
- An example for accessing remote mysql server
- An attempt to attach an auto-named database for file
- (hibernate)Executing an update/delete query
- Aggregate Function Data Type
- 基于高通MSM 8x60的I2C驱动终极讲解总结
- js中的比较
- IE7 overflow:hidden bug
- 使用异步 I/O 大大提高应用程序的性能
- 关于Web中Session失效
- An Example for Hibernate Named Query with Aggregate Function
- 身份证有效验证
- Control.PointToScreen(Point p)&&Control.PointToClient(Point p)
- 让html-options显示说明信息
- RCP视图标题显示为圆角(弧形Tab)的方法
- boost::thread之内部干将interrupt_wait
- 关于solr schema.xml 和solrconfig.xml的解释
- 使用MFC 发送消息模拟按钮按下事件
- Best Time to Buy and Sell Stock II