An Example for Hibernate Named Query with Aggregate Function
2014-01-16 20:12
507 查看
1. Does entity fit here?
I don't think so, we'd better have a VO or DTO to represent.
2. hbm.xml
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
(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
5. Integration Test with JUnit4
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...}
5. Integration Test with JUnit4
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:applicationContext-persistence.xml") @ActiveProfiles("test") public class NamedQueriesDaoTest { @Autowired private INamedQueriesDao namedQueriesDao; /** * Beware this is an integration test. */ //@Ignore @Test public void testGetCustomerInfo() { CustomerInfo customerInfo = namedQueriesDao.getCustomerInfo(2); assertEquals(2, customerInfo.getCustomerId()); } }
相关文章推荐
- An Example for Javascript Function Scoping and Closure
- A Brief Introduction to Hibernate for .NET with A Simple Example
- An Example of Pre-Query and Post-Query Triggers in Oracle Forms With Using Display_Item to Highlight Dynamically
- A Brief Introduction to Hibernate for .NET with A Simple Example
- An example of MFC ( help myself with beginning in this field )
- org.hibernate.MappingException: Named query not known: XXXX 解决办法
- A story for anyone who with an appetite for LOVE!
- Open quote is expected for attribute "class" associated with an elemen的解决方案
- 笔记-2008-An Empirical Comparison of Goodness Measures for Unsupervised CWS with a ~
- no lable views point to this text field with an android:lablFor=”@+id/@+id
- an example for mouse hook
- An example editor with table and image support
- example program to process an idoc - inbound function module
- python 2.7版本解决TypeError: 'encoding' is an invalid keyword argument for this function
- This is an example of state transitions - for Transition System Semantics of DFL as a Scientific Workflow Language
- CSS selectors for Selenium with example,selenium IDE
- Python学习笔记 --- 'password' is an invalid keyword argument for this function解决办法
- Simple example for transform xml with xlst in C#
- JavaEE_SSM_报错_Open quote is expected for attribute "property" associated with an element
- ORA-24347: Warning of a NULL column in an aggregate function