Hibernate returns last row n times - mysql

I am using hibernate core 5.4.33.Final, and mysql-connector 8.0.22.
DB MySql org.hibernate.dialect.MySQLDialect.
My problem occurs on previous release too.
Running a simple SQL query on a single table, no join, returns only last record for n times, where n is the correct records number.
HQL Query
SELECT ig.id, ig.codice
FROM ImpiantiGestitiDto as ig
Schema:
id=integer autoincrement
idPalestra=integer
codice=varchar(6)
numero=char(3)
codice_descrizione_impianto=varchar(6)
ImpiantiGestitiDto=Dto
This is my code. I don’t use loop.
Query<Object> query = session.createQuery(hql);
result = query.getResultList();
The mysql-connector run the query succesfully:
StandardRowReader - ---Processing Row---
Extracted JDBC value [0] - [1]
Extracted JDBC value [1] - [000.00]
StandardRowReader - ---Processing Row---
Extracted JDBC value [0] - [2]
Extracted JDBC value [1] - [000.01]
StandardRowReader - ---Processing Row---
Extracted JDBC value [0] - [3]
Extracted JDBC value [1] - [000.02]
StandardRowReader - ---Processing Row---
Extracted JDBC value [0] - [4]
Extracted JDBC value [1] - [000.03]
Output on getResultList(); Wrong!
[[4,"000.03"],[4,"000.03"],[4,"000.03"],[4,"000.03"]]
I think the problem occurs in:
-org.hibernate.sql.results.spi package;
-class ListResultsConsumer;
-method Consume.
in following loop, results.add(row) ; instructions occurs the error.
Why occurs the problem? Can you help me? Thank you!
while (rowProcessingState.next()) {
final R row = rowReader.readRow( rowProcessingState, processingOptions );
boolean add = true;
if ( uniqueRows ) {
if ( results.contains( row ) ) {
add = false;
}
}
if ( add ) {
results.add( row );
}
rowProcessingState.finishRowProcessing();
}
Using the query
FROM ImpiantiGestitiDto order by id
it’s works.
Using the query
SELECT id, codice FROM ImpiantiGestitiDto order by id
does not work.
I have to use the second query because the table ImpiantiGestitiDto is joined to another table and I have to use the WHERE clause.
Thanks for your answer.

Related

Linq query with Take() to Mysql does not add limit to actual query

I am using net core 2.2 with Pomelo EntityFramework with MySql database.
The following code:
return context.SomeTable
.OrderByDescending(item => item.ExpiredTime)
.Where(item => item.FinishedTime.HasValue
&& item.ExpiredTime.HasValue
&& item.ExpiredTime.Value < DateTime.UtcNow
&& item.IsArchive.GetValueOrDefault(false) == false/* is null or false*/)
.Take(500)
.Select(i=>new ItemWrapper(i))
.ToArray();
Returns the following MySql:
SELECT `item`.`Id`, `item`.`ExpiredTime`, `item`.`FinishedTime`,
`item`.`IsArchive`
FROM `SomeTable` AS `item`
WHERE (`item`.`FinishedTime` IS NOT NULL AND `item`.`ExpiredTime` IS NOT
NULL) AND (`item`.`ExpiredTime` < UTC_TIMESTAMP())
ORDER BY `item`.`ExpiredTime` DESC
It seems like the Take(500) is not being reflected in the query.
I expect to see limit = 500 in the sql query.
Edit 1:
I am using Select(i=>new ItemWrapper(i) to create a new class for the result object, it seems to be the root of the issue.
What am i doing wrong?
This happens because you are calling item.IsArchive.GetValueOrDefault(false) == false in Where clause. EF.Core cannot translate this method to SQL so it materializes all the items first and then tries to apply the rest to the data retrieved from SQL server. Try to remove this condition or rewrite it. BTW, usually EF.Core shows warnings for these kind of issues in the log.

Couchbase N1qlQuery: delete cause error to the query service

I try to run this query
delete from bucket o
use keys (select raw ARRAY_CONCAT(ARRAY_CONCAT(ARRAY_CONCAT(d, t), s), u)
from bucket
use keys 'SS')
I get this response:
{
"status": "Unexpected server error"
}
in the server log I see this:
Service 'query' exited with status 1. Restarting. Messages: runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1 fp=0xc20e97cfc0 sp=0xc20e97cfb8
created by github.com/couchbase/query/parser/n1ql.NewLexerWithInit
/home/couchbase/jenkins/workspace/watson-unix/goproj/src/github.com/couchbase/query/parser/n1ql/n1ql.nn.go:30999 +0x4a6c9
[goport] 2016/11/29 08:40:11 /opt/couchbase/bin/cbq-engine terminated: signal: aborted (core dumped)
What the problem with this query?
I am using couchbase version 4.5.
you can also use ARRAY_FLATTEN() function or the FIRST operator.
delete from bucket o use keys
ARRAY_FLATTEN( ( select raw ARRAY_CONCAT(ARRAY_CONCAT(ARRAY_CONCAT(d, t), s), u) from bucket use keys 'SS'), 1)
returning meta(o).id;
or
delete from bucket o
use keys FIRST x FOR x IN
( select raw ARRAY_CONCAT(ARRAY_CONCAT(ARRAY_CONCAT(d, t), s), u)
from bucket
use keys 'SS' )
END
returning meta(o).id;
Note that parenthesis (in bold italics) is required around the sub-query when it is used as expression (for example, as parameter to ARRAY_FLATTEN() or in the FIRST construct)
USE KEYS requires array of keys. ARRAY_CONCAT() returns array and Subquery returns array. It become array of array.
Remove one of the array as follows.
delete from bucket o
use keys (select raw ARRAY_CONCAT(ARRAY_CONCAT(ARRAY_CONCAT(d, t), s), u)
from bucket
use keys 'SS')[0];
If the ARRAY_CONCAT() argument is missing or null, it may return same panic error in 4.5. This has been fixed in 4.5.1.

EF6 Contains() works with only interned Strings

I want to filter my entity by name property. I use Contains() method. When I pass parameter as "E" it works but if I pass as variable it doesn't work.
Code is below :
if (!String.IsNullOrWhiteSpace(searchingModel.LanguageNameContains))
query = query.Where(n => n.Name.Contains(searchingModel.LanguageNameContains));
if (!String.IsNullOrWhiteSpace(searchingModel.LanguageCodeContains))
query = query.Where(n => n.Code.Contains(searchingModel.LanguageCodeContains));
return query;
Above example doesn't work. But if I write like this
if (!String.IsNullOrWhiteSpace(searchingModel.LanguageNameContains))
query = query.Where(n => n.Name.Contains("E"));
if (!String.IsNullOrWhiteSpace(searchingModel.LanguageCodeContains))
query = query.Where(n => n.Code.Contains("En"));
return query;
It works. I have debugged lots of times. I passed same parameters as variable and as constants. If I pass as constant it works but If I pass as variable it doesn't work. Curiously enough, The same code works MySql.Data.Entity 6.9.3 but now I am using 6.9.5 and it doesn't work. Is it a bug or my mistake ?
Generated SQL :
-when I pass parameter as variable:
SELECT
Extent1.Id,
Extent1.Code,
Extent1.Name
FROM Languages AS Extent1
WHERE Extent1.Name LIKE '%p__linq__0%'
-- p__linq__0: 'E' (Type = String, Size = 1)
-- Executing at 30.12.2014 22:30:40 +02:00
-- Completed in 0 ms with result: EFMySqlDataReader
when I pass as interned string :
SELECT
Extent1.Id,
Extent1.Code,
Extent1.Name
FROM Languages AS Extent1
WHERE Extent1.Name LIKE '%E%'
Second one returns rows but first one doesn't return any rows.

How can I execute a query when columns comparator_type is in LexicalUUIDType?

I have created a column family with Comparator_type="LexicalUUIDType", Default_validation_class="UTF8Type" and Key_validation_class="UTF8Type".
And set TimeUUID as column_name within the column family above. It's insertion runs very well, but how can I get the columns? I can't set the correct column_name! The following are the code:
ColumnPath path = new ColumnPath();
path.setColumn_family("test");
path.setColumn(("44c32fe1-38a4-11e1-a06a-485d60c81a3e".getBytes()));
ColumnOrSuperColumn or = new ColumnOrSuperColumn();
try {
or = client.get(ByteBuffer.wrap("key").getBytes()), path, ConsistencyLevel.ONE);
} catch (InvalidRequestException e) {
...
data in Cassandra DB:
=> (column=44c32fe0-38a4-11e1-a06a-485d60c81a3e, value=32, timestamp=1325881397726)
=> (column=44c32fe1-38a4-11e1-a06a-485d60c81a3e, value=33, timestamp=1325881397726)
=> (column=44c32fe2-38a4-11e1-a06a-485d60c81a3e, value=34, timestamp=1325881397727)
=> (column=44c37e00-38a4-11e1-a06a-485d60c81a3e, value=35, timestamp=1325881397728)
=> (column=44c37e01-38a4-11e1-a06a-485d60c81a3e, value=36, timestamp=1325881397728)
...
And the exception informations:
InvalidRequestException(why:LexicalUUID should be 16 or 0 bytes (36))
at org.apache.cassandra.thrift.Cassandra$get_result.read(Cassandra.java:6490)
at org.apache.cassandra.thrift.Cassandra$Client.recv_get(Cassandra.java:519)
at org.apache.cassandra.thrift.Cassandra$Client.get(Cassandra.java:492)
at test.cassandra.MainTest.query(MainTest.java:118)
...
That's why? I can't execute single query or slice query now. How can I execute query by key and column name with uuid? Thank in advance!
The byte representation of a UUID is not what you get when you call "44c32fe1-38a4-11e1-a06a-485d60c81a3e".getBytes() (a uuid is 16 bytes, this string.getBytes() is 36 bytes). The FAQ on the cassandra wiki has instructions how to do what you want in java:
java.util.UUID.fromString("44c32fe1-38a4-11e1-a06a-485d60c81a3e");

Counting several sums on same query fails

I run a query, where I count several sums on the different fields of same database entity. My problem arises when I am running tests on the query and at the same time changing the query from Mysql to the native language used on tests in IntelliJ Idea tool (I don't know what it uses).
Problem is this: in the new environment all sum statements inside the single query return the value equal to the one that is got of the first sum statement.
I am using JPA and NativeQuery.
More information:
I have a code like this
List < Object [ ] > row = List < Object [ ] > em.createNativeQuery("select sum (e.field), sum (e.otherField) from entity e where somevalue = something").getResultList();
and then
return new MyResult ( ( Double ) row.get ( 0 ) [ 0 ] , ( Double )row.get ( 0 ) [ 1 ] );
Everything is inside a doInJPA function.
Real issue was identified to be so that my Idea used for testing HSQL and that somehow does not support sum in the same way that Mysql does.