index out of bound exception in hibernate3 - java

In my project i store and retrieve the timesheet in week manner.
i have a table like this
Id projectId activityId Date Spenttime
1 1 1 2014-11-10 8
2 1 1 2014-11-11 8
3 1 1 2014-11-12 8
4 1 1 2014-11-13 8
5 1 1 2014-11-14 8
6 1 1 2014-11-15 8
7 1 1 2014-11-16 8
8 1 2 2014-11-10 8
9 1 2 2014-11-11 8
10 1 2 2014-11-12 8
11 1 2 2014-11-13 8
12 1 2 2014-11-14 8
13 1 2 2014-11-15 8
14 1 2 2014-11-16 8
15 2 1 2014-11-15 8
16 2 1 2014-11-16 8
i want the result for the above table,like below
projectId activityId 2014-11-10 2014-11-11 2014-11-12 2014-11-13 2014-11-14 2014-11-15 2014-11-16
1 1 8 8 8 8 8 8 8
1 2 8 8 8 8 8 8 8
2 1 0 0 0 0 0 8 8
My hibernate code for the above table
List<Timesheet> timesheetList=sessionfactory.getCurrentSession.createCriteria(Timesheet.class)
.add(Restrctions.between("date",formatter.parse("2014-11-09"),formatter.parse("2014-11-16"))
.list();
Retrieve logic:
List<DisplayTable> dispaly=new ArrayList<DisplayTable>();
for(int i=0;i<timesheetList.size();i+=7)
{
DisplayTable disp=new DisplayTable();
disp.setProjectId(timesheetList.get(i).getProjectId());
disp.setActivityId(timesheetList.get(i).getActivityId());
disp.setSpentTimeDate1(timesheetList.get(i).getSpentTime());
disp.setSpentTimeDate2(timesheetList.get(i+1).getSpentTime());
disp.setSpentTimeDate3(timesheetList.get(i+2).getSpentTime());
disp.setSpentTimeDate4(timesheetList.get(i+3).getSpentTime());
disp.setSpentTimeDate5(timesheetList.get(i+4).getSpentTime());
disp.setSpentTimeDate6(timesheetList.get(i+5).getSpentTime());
disp.setSpentTimeDate7(timesheetList.get(i+6).getSpentTime());
}
The above logic works fine in first two iteration.after that it throws index outofbound exception.
i know the exception is throwed because project 2 contains only 2 rows.
Is their any ways to achive the desired result in hibernate3?
Any help wll be greatly appreciated!!!!

change the condition to
i < timesheetList.size() - 6
because you don't want go +7 than size

Related

Select the first duplicated element

I have 2 Classes in JAVA ( MODEL1 && MODEL2 )
As you can see here :
MODEL1_ID ACTN_DTE MODEL2_ID
---------- --------- --------------
1 14/11/19 18
1000 14/11/19 4
1001 14/11/19 19
1002 14/11/19 4
1003 14/11/19 4
1004 14/11/19 18
2000 14/11/19 5
I am trying to find a way with SQL Or HQL to get all elements from MODEL1 that have a list of MODEL2_ID , get only the first (min MODEL1_ID) MODEL1 per MODEL2_ID ( in case if it's duplucated ).
Exemple :
Input : MODEL2_ID in (18,4,19,5)
MODEL1_ID ACTN_DTE MODEL2_ID
---------- --------- --------------
1 14/11/19 18
1000 14/11/19 4
1001 14/11/19 19
2000 14/11/19 5
select MIN(MODEL1_ID) FROM table GROUP BY (MODEL2_ID)
It is possible that by "first" you mean the minimum actn_date and the question just has a useless sample of data (because all the values are the same).
If so, you can use aggregation with keep to get the first value by actn_date:
select model2_id, min(actn_date) as actn_date,
min(model1_id) keep (dense_rank first order by actn_date) as model1_id
from t
group by model2_id;

Java - Hibernate : Generate sequence column based on another field

Need to add a field to the database which will record a sequence number related to that (foreign) id.
Example table data (current):
ID ACCOUNT some_other_stuff
1 1 ...
2 1 ...
3 1 ...
4 2 ...
5 2 ...
6 1 ...
I need to add a sequenceid column which increments separately for each account, achieving:
ID ACCOUNT SEQ some_other_stuff
1 1 1 ...
2 1 2 ...
3 1 3 ...
4 2 1 ...
5 2 2 ...
6 1 4 ...
Note that the sequence is related to account.
Unfortunately this cannot be done with JPA and hibernate. The only solution would be to do it manually in the service. You can use #Generated value on a column but that relies on the database to provide the value. And you cannot create a custom sequence implementation and use #GeneratedValue because that works only for the ID column.

Count Unique Text and value count

I would like to get the result unique count value/text/ etc
A B
2 BADER 111
3 FAISA 112
4 NASSE 113
5 NASSE 113
6 MOHS 121
7 ASI 122
8 AHME 100
9 AHME 100
10 AHME 100
11 ASI 122
RESULT AS BELOW.
A B
2 BADER 111
3 FAISA 112
4 NASSE 113
5 NASSE 113
6 MOHS 121
7 ASI 122
8 AHME 100
9 AHME 100
10 AHME 100
11 ASI 122
6 6
For the number of different values in A2:A11 try this formula
=SUMPRODUCT((A2:A11<>"")/COUNTIF(A2:A11,A2:A11&""))
That will work for numeric or text values

R: read.arff error

I am starting to work with Weka in R and I got stuck at the first step. I converted my csv file into arff file and I did this using an online converter, but when i tried to read it into R I got the following error message.
require(RWeka)
A <- read.arff("Environmental variables all overviewxlsx.arff")
Error in .jnew("weka/core/Instances", .jcast(reader, "java/io/Reader")) :
java.io.IOException: no valid attribute type or invalid enumeration, read Token[[°C]], line 6
Does anyone have an idea that could help me?
Thanks!
p.s. the proper package (RWeka) is already installed.
Because read.arff() returns a dataframe you could skip the conversion process and use read.csv().
train_arff<-read.arff(file.choose())
str(train_arff)
'data.frame': 14 obs. of 5 variables:
$ outlook : Factor w/ 3 levels "sunny","overcast",..: 1 1 2 3 3 3 2 1 1 3 ...
$ temperature: Factor w/ 3 levels "hot","mild","cool": 1 1 1 2 3 3 3 2 3 2 ...
$ humidity : Factor w/ 2 levels "high","normal": 1 1 1 1 2 2 2 1 2 2 ...
$ windy : logi FALSE TRUE FALSE FALSE FALSE TRUE ...
$ play : Factor w/ 2 levels "yes","no": 2 2 1 1 1 2 1 2 1 1 ...
train_csv<-read.csv(file.choose())
str(train_csv)
'data.frame': 14 obs. of 5 variables:
$ outlook : Factor w/ 3 levels "overcast","rainy",..: 3 3 1 2 2 2 1 3 3 2 ...
$ temperature: Factor w/ 3 levels "cool","hot","mild": 2 2 2 3 1 1 1 3 1 3 ...
$ humidity : Factor w/ 2 levels "high","normal": 1 1 1 1 2 2 2 1 2 2 ...
$ windy : logi FALSE TRUE FALSE FALSE FALSE TRUE ...
$ play : Factor w/ 2 levels "no","yes": 1 1 2 2 2 1 2 1 2 2 ...
Otherwise your .arff file should have this format

Select single row for column value

Here is sample table data which is dynamic.
ColId Name JobId Instance
1 aaaaaaaaa 1 2dc757b
2 bbbbbbbbb 1 2dc757b
3 aaaaaaaaa 1 010dbb8
4 bbbbbbbbb 1 010dbb8
5 bbbbbbbbb 1 faa2733
6 aaaaaaaaa 1 faa2733
7 aaaaaaaaa 1 bc13d69
8 aaaaaaaaa 1 9428f4d
I want output like
ColId Name JobId Instance
1 aaaaaaaaa 1 2dc757b
3 aaaaaaaaa 1 010dbb8
5 bbbbbbbbb 1 faa2733
7 aaaaaaaaa 1 bc13d69
8 aaaaaaaaa 1 9428f4d
What should be the JPA query so that I can retrieve entire row having only single 'Instance'(there is no max min condition involved).
I need one row for each 'Instance' value
FROM table t GROUP BY t.instance should suit your needs.
Something like JPQL "Select entity from Entity entity where entity.id in (select min(subinstance.id) from Entity subinstance group by subinstance.instance)"
Functions like count, min, avg etc are allowed over columns not included in the group by statement, so any such should work if it returns a single id value from the grouping.

Categories