APP_INDEX Column is not update properly - java

Currently I am working on a java project where they used hibernate with JPA. One of my table have a column APP_INDEX and this column automatically increase value when we do an insert. This order should not have gaps. But when I remove an record its not automatically change the order. so I am getting errors when I try to read records from the table. Just wonder why for delete APP_INDEX does not change properly. I just wonder I need to add any mapping for this according to JPA spec
Table data
id name APP_INDEX
001 AA 0
002 BB 1
003 CC 2
I am deleting 002 record
Expected result
id name APP_INDEX
001 AA 0
003 CC 1
But I am getting
id name APP_INDEX
001 AA 0
003 CC 2

Related

Updating table in Schema 1 based on values of few columns of a table in Schema 2

I will try to explain the problem with the best of my ability.
SO I have 2 tables in 2 different Schemas with few columns in both & I own only 1 of the schemas.
What I need to do is Update Table A in Schema 1 with a value from one of the fields from Table B from Schema 2.
I need to update only few rows in this table
The problem lies in when table A is populated the data in Table B is not ready with the data.
I am trying to this programmatically if possible.
Since, they are in different schemas & update size is comparatively smaller than the A's table size what should be the best way to do this?
SAMPLE DATA
**
Table A
orderNum | orderNumInternal | validity | averageSales |type
1000 | 5636 | 2020-06-30 00:00:00.000 | NULL |valid
Table B
orderNum | orderNumInternal | validity | averageSales
1000 | 5636 | 2020-06-30 00:00:00.000 | 65
**
Here I need to update Table A with the averageSales value from Tabel B whenever the type in Table A is valid & there is match in table B for the first 3 columns
Table A is created in an overnight whilst I don't have control over when the data would be available in Table B
Would this not simply be an UPDATE with a JOIN?
UPDATE A
SET averageSales = B.averageSales
FROM Schema1.TableA A
JOIN Schema2.TableB B ON A.orderNum = B.orderNum
WHERE A.averageSales IS NULL; --Unsure if this WHERE is needed

FOREACH in cypher - neo4j

I am very new to CYPHER QUERY LANGUAGE AND i am working on relationships between nodes.
I have a CSV file of table containing multiple columns and 1000 rows.
Template of my table is :
cdrType ANUMBER BNUMBER DUARTION
2 123 456 10
2 890 456 5
2 123 666 2
2 123 709 7
2 345 789 20
I have used these commands to create nodes and property keys.
LOAD CSV WITH HEADERS FROM "file:///2.csv" AS ROW
CREATE (:ANUMBER {aNumber:ROW.aNumber} ),
CREATE (:BNUMBER {bNumber:ROW.bNumber} )
Now I need to create relation between all rows in the table and I think FOREACH loop is best in my case. I created this query but it gives me an error. Query is :
MATCH (a:ANUMBER),(b:BNUMBER)
FOREACH(i in RANGE(0, length(ANUMBER)) |
CREATE UNIQUE (ANUMBER[i])-[s:CALLED]->(BNUMBER[i]))
and the error is :
Invalid input '[': expected an identifier character, whitespace,
NodeLabel, a property map, ')' or a relationship pattern (line 3,
column 29 (offset: 100)) " CREATE UNIQUE
(a:ANUMBER[i])-[s:CALLED]->(b:BNUMBER[i]))"
I need relation for every row. like in my case. 123 - called -> 456 , 890 - called -> 456. So I need visual representation of this calling data that which number called which one. For this I need to create relation between all rows.
any one have idea how to solve this ?
What about :
LOAD CSV WITH HEADERS FROM "file:///2.csv" AS ROW
CREATE (a:ANUMBER {aNumber:ROW.aNumber} )
CREATE (b:BNUMBER {bNumber:ROW.bNumber} )
MERGE (a)-[:CALLED]->(b);
It's not more complex than that i.m.o.
Hope this helps !
Regards,
Tom

Hibernate criteria to fetch the records?

I have below records in table.
col1 col2 col3
------------------------
1 Abc IN
2 DEF CA
3 Xyz IN
4 Cae CA
5 Pty IN
6 Zwe DE
7 Zwf US
Here User sends an Input like IN or CA or DE etc. User input has to be mapped against col3. Now I need to query all the records from the table but the records matching the user input (IN or CA or DE) should appear first in the list then all other records should appear. How can I do it using hibernate criteria?
I need the results in below the order if user sends IN as an input.
1 Abc IN
5 Pty IN
3 Xyz IN
2 DEF CA
4 Cae CA
6 Zwe DE
7 Zwf US
You could try to use ORDER BY CASE construct:
order by case when <your entity>.col3 = :parameter then '0' else '1' end asc
There are two ways to solve this problem:
1. Create two queries one with equal to another with not equal to and all results for both in single list.
2. If you don't want to query database twice then you have to write algo in java that will remove elements for your input from list and add it another list and after iteration add remaining list at the end.
try the case ... when statements:
select *, ( case when col3=:input then '0' | col3 else col3 end) as sorter from table order by sorter asc
not sure, if it works, but if it does it would be exactly what you want

Comparing rowset data in Java

I get two cached row sets of data from two tables; table1 and table2.
table1
OrgCode Name Amt FeeCode
1 John 100 R
1 Micheal 200 L
table2
OrgCode AgreementNo AddressNo CustomerNo FeeCode
1 111 211 555 R
1 222 212 666 L
1 333 213 777 P
I need to compare two cachedrow sets, and send the data (cached row set) table to JSP depending on the matching feecode in tables2.
The Sql formed for Cached row set for table-1 is dynamically formed,the same method is used by different classes,with return type cached row set
How might I do this?

How to combine two result sets from one table sorted independently using one SQL query?

This is a simplified task which I have to solve in real project. In this project data is stored in HSQLDB. Data is accessed using JDBC.
I have one table:
name | flag
-----------
aa | 1
bb | 0
cc | 1
dd | 0
ee | 1
ff | 0
I need to compose query to get the following table:
name | flag
-----------
aa | 1
cc | 1
ee | 1
ff | 0
dd | 0
bb | 0
The final table is like rows with flag = 1 were taken and sorted ascending, rows with flag = 0 were taken and sorted descending and results were combined one after another.
Please, pay attention, that rows with flag = 1 and rows with flag = 0 have opposite sort order.
Is it possible to do in SQL? I wouldn`t like to make two queries and merge ResultSets in Java code manually.
In any SQL, only the outermost order by applies so it has to be done there.
I don't know if this works in your SQL dialect (and can't check sorry), but you could do this
SELECT name, flag
FROM 'table'
ORDER BY
flag desc,
CASE WHEN flag = 1 THEN name ELSE '' END,
CASE WHEN flag = 0 THEN name ELSE '' END DESC
Try this:
SELECT name, flag
FROM 'table'
ORDER BY flag desc, name
Let the database do the work whenever you can. Don't do such things in Java. Think "SQL first".
order by can take more than one column:
select *
from table
order by flag desc, name asc

Categories