SpringData PageImpl totalElements is wrong - spring-data

When I new a PageImpl Object like :
new PageImpl<CompanyEntity>(content, new PageRequest(page, size), total);
the result is not correct.
When the "size" lagger than "total" the totalElements use the "content.size" and when "size" less than "total" the totalElements use the "total".
why ?
The "totalElements" shouldn't represent the total count of elements ?
Help!

set PageRequest page start to 0. It solved my problem.
PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize,sort);
Page<T> response = new PageImpl<>(result,pageRequest,totalCount);

i saw the source of "PageImpl" and fond that :
this.total = !content.isEmpty() && pageable != null && (long)(pageable.getOffset() + pageable.getPageSize()) > total?(long)(pageable.getOffset() + content.size()):total;
is that right ? why (long)(pageable.getOffset() + pageable.getPageSize()) > total?(long)(pageable.getOffset() + content.size()):total?

Please add this property in application.properties:
spring.data.web.pageable.one-indexed-parameters=true
Without this property, the default page size starts from 0 and that would mess up all of the calculations.

Related

findall specification pageable is taking time for 10 data java spring boot jpa

Hi I have a table HrCrEmp where i have 132 columns and have several relationship with other tables. I am using a pagination
if(sortField == null || sortField.equals("") || sortField.equals("id") ) sortField = "id";
Sort sort = sortDir.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortField).descending() : Sort.by(sortField).ascending();
Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
Page<HrCrEmp> hrCrEmp = this.hrCrEmpRepository.findAll((Specification<HrCrEmp>) (root, cq, cb) -> {
//rest of the code;
}
But unfortunately this pagination is taking about 40 to 50 seconds to return 10 values; Can anyone please help what might be the issue and how can i sort out this issue in java spring boot.
I have searched for solutions in the net but haven't found any . This is beyond my knowledge.
Normally you would use the findList method. findAll is a full table query, and I don't know which class your hrCrEmpRepository is extends from

Is it possible to compare with a number in "Where"?

I am new in Entity Framework and I have a little question.
Is it possible to compare in "Where" with a number.
I will add code example.
var source = db.Book.Where(book => book.Count > 0);
So, I need to get all book items where count more than zero.
Thanks in advance for your help!
Here is the structure of database
I'm not sure why you are trying to query the entire table to check if there is any record, yet you can do something like:
var query = context.Books.Any() ? context.Books.ToList() : null;
if (query is not null)
{
query.ForEach(b =>
{
Console.WriteLine($"ID:{b.Id}, Title:{b.Title}");
});
}
However, if you are trying to find out if there is duplication available, you can use the following query:
var query = context.Books.FirstOrDefault(b => b.Title == "some title")
is not null ? context.Books.ToList() : null;
if (query is not null)
{
query.ForEach(b =>
{
Console.WriteLine($"ID:{b.Id}, Title:{b.Title}");
});
}
By the way, I don't think it is a good idea to have such a scenario.

How to define and access local variable in Typoscript 2 (Neos)?

I have created a custom NodeType "Events" with a custom TS2 file in Neos, but I guess it is more a general question about Typoscript 2.
prototype(Some.Namespace:Events) < prototype(TYPO3.Neos:Document) {
...
sortOrder = ${request.arguments.sortOrder == 'asc' ? 'asc' : 'desc'}
otherVariable = ${sortOrder}
...
}
Of course this is simplified to focus on the issue:
I want to assign the value of the variable sortOrder (which is "asc" or "desc") to another variable named otherVariable.
How can I do that? I cannot access the value using ${sortOrder}, which returns always NULL.
All you need to do is add this as below and {otherVariable} in your fluid template will work. Flush cache in case you sill have NULL.
sortOrder = ${request.arguments.sortOrder == 'asc' ? 'asc' : 'desc'}
otherVariable = ${this.sortOrder}

Strange drools syntax

I am having difficulty understanding the part value : value == 0? How does this code work?
rule "My rule"
when
m : MyClass( value : value == 0)
then
end
Assuming you are using Drools 5.4 or a newer snapshot, you can write any boolean expression as a constraint, so value == 0 is a constraint where "value" is a field name in MyClass.
Drools also allows you do use ":" to bind an attribute to a variable name, like this:
<variable_name> : <fieldName>
So, you can write:
MyClass( $var : value == 0)
Finally, since Drools uses a "context-aware" parser, you can have a variable with the same name as the attribute name, because Drools knows what comes before the : is a variable name, not a field. So, in your example: the variable "value" will be bound to the attribute "value" and the constraint "value == 0" will be true if the value attribute is equal to 0.
Hope it helps.

Remove in MongoDb chsarp fails when using the _id

in a sample program removing items from the mongo db doesn't work when I want to use the id.
I assume the problem is that my class has a Id Property, but mongo uses an _id ?
So when I trigger the remove in the following code nothing changes. Using remove with a query which uses name or value everything works as expected and the item gets removed.
Any hint ?
MongoServer server = MongoServer.Create(#"mongodb://localhost/?safe=true");
server.Connect();
var db = server.GetDatabase("data");
var collection = db.GetCollection<Foo>("foo");
string id = Guid.NewGuid().ToString();
Foo a = new Foo();
a.Id = id;
a.name = "Boas";
a.Value = 1;
collection.Insert<Foo>(a);
Console.WriteLine(collection.Count()+ " items"); // Count is 1
collection.Remove(Query.EQ("_id",id));
Console.WriteLine(collection.Count() + " items"); // Count is still 1 :-( should be 0
Console.ReadLine();
Addition:
When I use the same query with Find() the item is found. So I don't understand why find does find the element and remove doesn't remove it.
collection.Find(Query.EQ("_id",id)).Count() // returns 1 element
I have done this over and over and it continually prints out 1 items and then 0 items. Are you sure your collection doesn't already have an item in it?
After your collection.Insert(a), add a collection.RemoveAll().