|
Concat Expression
The following example shows concatenation operation using HQL.
Refer first example for the configuration and mapping.
|
package com.bethecoder.tutorials.hibernate.basic.tests;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;
public class HQLConcatOperationTest {
/**
* @param args
*/
public static void main(String[] args) {
getCompanies("select id||'->'||lower(name) from Company");
getCompanies("select id||'['||lower(name)||', '||upper(address1)||']' from Company");
}
private static void getCompanies(String hql) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
List<?> rows = session.createQuery(hql).list();
System.out.println("Selected row count : " + rows.size());
Iterator<?> rowsIt = rows.iterator();
while (rowsIt.hasNext()) {
System.out.println(rowsIt.next());
}
session.getTransaction().commit();
session.close();
}
}
|
| |
It gives the following output,
Selected row count : 10
1->pqr
2->abc
3->mno
4->ijk
5->art
6->rnk
7->aoq
8->bqo
9->cfr
10->cfr
Selected row count : 10
1[pqr, ADD1@]
2[abc, ADD11]
3[mno, ADD111]
4[ijk, ADD1112]
5[art, ADD1112]
6[rnk, ADD1412]
7[aoq, ADD17892]
8[bqo, ADD1459]
9[cfr, ]
10[cfr, ]
|
|