Suppose i have a table Student
- ID Name Sub1 Sub2
- 1 Deepika 99 89
- 2 Shubham 78 90
I want my hibernate application to provide me the following result
- ID Name Sub1 Sub2 Total
- 1 Deepika 90 80 170
- 2 Shubham 78 90 168
i.e my sql query is
select id name, sub1, sub2, sub1+sub2 as total
from employee
How can i do this in Hibernate? What changes should i make in my hbm.xml file and bean class?
Using addScalar() you can fetch any additional row than the existing information without changing the xml. For more details refer this documentation
Otherwise you need to change the xml and bean class corresponding to the new table structure.
Related
Let's say I have a very simple table called test:
ID AGE NAME
1 12 Bob
1 13 Bob
2 13 John
3 9 Michael
3 11 Michael
I want to return all results that have the largest AGE in the table. So for this case, the result would be the entities with the age 13:
ID AGE NAME
1 13 Bob
2 13 John
I would think something like this should exist, my thought process would have been the following (even though I know the syntax would not work):
connection.getSqlQuery()
.from(test)
.max(test.age)
.list(test.id, test.age, test.name);
What would be the proper query for this?
You can create a subquery and use it in your where clause:
// Aggregate to get the maximum age
var subQuery = connection.getSqlQuery()
.select(test.age.max())
.from(test);
connection.getSqlQuery()
.from(test)
.where(test.age.eq(subQuery))
.list(test.id, test.age, test.name);
If you use JPA, you can also use var subQuery = JPAExpressions.select(...
I need to find max salary and displaying in new attribute.
Employee table has following columns:
Id,
Name,
Salary
I have added new add transient variable with groovy expression using Edit Expression Editor :
max(Salary)
to display max salary which exists in whole table.
I have tried this example in 11g.
Table : Emp :
E_ID EMP_NAME E_SAL
2 emp2 5000000
4 e5 100000
5 emp2 300000
6 emp3 250000
7 emp3 100000
8 emp3 100000
3 eee 250000
1 emp 100000
In which E_ID - 2 has max salary as 5000000.
Now coming back to ADF:
I have created EMP table EO and VO both.
EMPEO
EMPVO - EmpView
Now in EmpView has all the attributes as Eid, EmpName, Esal.
Created a ViewAccessor of Same VO (EmpView) in EmpView
- Click on EmpView.
- Go to View Accessor. Click on Green plus symbol. Add the EmpView from left to right. It will be named as EmpView1.
Create a transient variable named as maxSal. Give a groovy expression
to it in a Expression component.
EmpView1.max("Esal")
Now,create a jsff page and add EmpView as Form or table (as per your
requirement) with newly created transient variable. Run the
application. It's working in my case.
Hope this may help.
i am trying to new cache configuration with new DB tables. following are sample table structure and sample Data.
TABLE_DEPT:
Dept Id Detp Name Dept Dtls
111 SALES A1
112 MARKET A2
TABLE_EMP:
Emp Id Emp Name DeptId Working Started Working ended
1 ABEmp 111 01-01-2017 02-02-2017
2 CDEmp 112 01-01-2017 03-12-2017
3 EFEmp 113 01-01-2017 03-12-2017
1 ABEmp 115 03-02-2017 03-12-2017
if i want to add load the data in cache by using Dept Id, i will have unique dept Id with list of emp Id - details.
if i want to search by dept id in ehcache configuration, i can simply give search attribute is "dept id".
but, if i want to search by emp id, i should get list of dept under employee worked.
what should be my ehcache design?
my Java bean/POJO looks like below.
Class DeptDtls{
int deptId;
String deptName;
List<Integer> empIdList;
//Setter & getters
}
cache - i want to put key as deptId & value is whole DeptDtls.
in this case, how can i allow search operation based on empId?
Ehcache can provide in memory data storage only but in your case search also required on top cache.I got the same scenario and used elasticsearch for caching the data and search on stored data.elastic search has inbuilt support for search and we can index the stored data .elasticsearch can be configured for cache.
I have a dbmap which holds the database data
List<Long,List<validitycommerce>> dbmap= new HashMap<Long,List<ValidityCommerceregion>>
ValidityCommerceRegion contains
Validity id,
commerce id,
region id,
Now I have incoming list coming commerce id and region id say 151 be and 17 lu and I found the validityid of both 151 and 17 and I got the list of validityid's
Now I want to find the common validityid in both 151 BE and 17 LU
How can we achieve this?
I've not much experience in sql.In my little java program using ucanaccess library I was able to perform some simple queries after creating a simple table (named ReportSales). The table is:
ID DATE PRODUCT SALES FEES
1 2014-10-02 productA 10.000 100
2 2014-09-02 productC 12.000 240
3 2014-09-02 productA 8.000 80
4 2014-11-02 productB 7.000 105
5 2014-08-02 productB 6.000 90
.. .......... ........ ...... ....
.. .......... ........ ...... ....
The last task is to create a pivot table in which I would insert the monthly sales per product. Something like:
PRODUCT AUG SEP OCT NOV
productA 0 8.000 10.000 0
productB 6000 0 0 7000
productC 0 12.000 0 0
And another pivot for monthly fees
My (wrong) attempt is:
SELECT [8] as AUG, [9] as SEP, [10] as OCT, [11] as NOV
FROM
(SELECT SALES,MONTH(DATE)
FROM ReportSales) AS tmp
PIVOT
(
SUM(SALES)
FOR MONTH(DATE) IN ([8], [9], [10], [11])
) AS PivotTable
Does anyone can help me? Thanks in advance
UCanAccess supports Pivot queries, only if they are built in Access, so you have firstly to create a crosstab query using the Access IDE.
Or you may simply create a new query in Access entering directly the following SQL:
TRANSFORM sum(ReportSales.sales) AS SalesCount
SELECT ReportSales.product
FROM ReportSales
GROUP BY ReportSales.product
PIVOT Format([date],"mmm");
Then assuming that you saved the query as query1, you have just to execute, with UCanAccess:
Select * from query1;