JPA: the better way of getting the first object in a list of ordered objects? - jpa

I need to retrieve the most recently updated object. Currently I am able to retrieve it by first doing something similar to the following:
em.createQuery("select m from MyObject m order by m.updateTime").setFirstResult(0).setMaxResults(1).getResultList()
and then getting the only object if the result list is not empty. I am curious whether there is any better way of doing this type of thing.
Thanks and regards.

Setting first result to 0 is redundant in this case and if you need just one row then you could use a getSingleResult() function.
So your code could be modified as follows:
MyObject myObject = em.createQuery("select m from MyObject m order by m.updateTime").setMaxResults(1).getSingleResult();
EDIT: as Tiny has mentioned in the comment in case when there are no matching rows the code will throw a NoResultException which is to be handled (e.g. assign a null value in this case).
EDIT2:
I've checked the Hibernate implementation of both methods and it turned out that getSingleResult() has all the code that getResultList() has and on top of that the following block:
if ( result.size() == 0 ) {
NoResultException nre = new NoResultException( "No entity found for query" );
getEntityManager().handlePersistenceException( nre );
throw nre;
}
else if ( result.size() > 1 ) {
final Set<X> uniqueResult = new HashSet<X>(result);
if ( uniqueResult.size() > 1 ) {
NonUniqueResultException nure = new NonUniqueResultException( "result returns more than one elements" );
getEntityManager().handlePersistenceException( nure );
throw nure;
}
else {
return uniqueResult.iterator().next();
}
}
else {
return result.get( 0 );
}
thus you end up with a slower implementation when using getSingleResult().
EDIT3: getSingleResult() would be beneficial in case if having zero or more than one resulting row is an invalid/exceptional data state (i.e. if such a case occurs then you know that your data is corrupted). Since both NoResultException and NonUniqueResultException are uncatched exceptions you will end up with only one line of code (no if, no catch needed in this case).

Related

Webflux - return Flux or error after a condition

I'm learning reactive programming with webflux, and for that I'm migrating some code.
For example I'm trying to migrate this method:
public Set<Vaccine> getAll(Set<Long> vaccinesIds) throws EntityNotFoundException {
if (null == vaccinesIds) {
return null;
}
Set<Long> vaccinesToFind = new HashSet<>(vaccinesIds);
vaccinesToFind.remove(null);
Set<Vaccine> vaccines = new HashSet<>();
vaccineRepository.findByIdIn(vaccinesToFind).forEach(vaccines::add);
if (vaccines.size() != vaccinesToFind.size()) {
LOG.warn("Could not find vaccines with ids: " + vaccinesToFind.removeAll(vaccines.stream().map(Vaccine::getId).collect(Collectors.toSet())));
throw new EntityNotFoundException(VACCINE_ERROR_NOT_FOUND);
}
return vaccines;
}
To summarize the code, if the respository returns all the vaccines that are requested should return the result, if not should return an error.
For that, I thought in something like this, but is not working:
public Flux<Vaccine> getAll(Set<Long> vaccinesIds) {
if (null == vaccinesIds) {
return Flux.empty();
}
Set<Long> vaccinesToFind = new HashSet<>(vaccinesIds);
Flux<Vaccine> byIdIn = vaccineRepository.findByIdIn(vaccinesToFind);
Mono<Long> filter = vaccineRepository.findByIdIn(vaccinesToFind).count().filter(x -> x.equals(Long.valueOf(vaccinesToFind.size())));
return filter.flatMapMany(asd -> vaccineRepository.findByIdIn(vaccinesToFind)
).switchIfEmpty(Flux.error((new EntityNotFoundException(VACCINE_ERROR_NOT_FOUND))));
}
What am I doing wrong?
My first doubt is why the filter is a Mono of Long if it has a equals method in the end. My problem is about evaluating the filter in order to return the list or the error.
First of all, you are querying the same result vaccineRepository.findByIdIn(vaccinesToFind) multiple times. The same data is queried, transferred and deserialized multiple times. This is a sign that something is wrong here.
Let's assume the result set fits into the memory. Then the idea would be to transform flux into a usual collection and to decide whether to emit an error or not:
return vaccineRepository.findByIdIn(vaccinesIds)
.collectList()
.flatMapMany(result -> {
if(result.size() == vaccinesIds.size()) return Flux.fromIterable(result);
else return Flux.error(new EntityNotFoundException(VACCINE_ERROR_NOT_FOUND));
});
In the case the result is to huge for the main memory, you could do count in the db by the first query and in the positive case query the results. The solution is similar to your code:
return vaccineRepository.countByIdIn(vaccinesIds)
.filter(count -> count == vaccinesIds.size())
.flatMapMany($ -> vaccineRepository.findByIdIn(vaccinesIds))
.switchIfEmpty(Mono.error(new EntityNotFoundException(VACCINE_ERROR_NOT_FOUND)));
The result of filter is Mono<Long> because filter just takes the elements from the upstream and tests against the given predicate. If the predicate returns false, the item is filtered out and the Mono is empty. To keep all results of a the test you could use map and the type would be Mono<Boolean>.

How to read an array with BsonReader in MongoDB?

Basically, I have a data set where a document can contain an array of variable length. I'm writing a codec, so all I have is BsonReader. The problem is readStartArray() returns void rather than returning, for example, the number of elements. Also, I don't see any methods that allow one to test when the end of the array has been reached (apart from trying readEndArray() and catching the exception).
Any ideas?
I found the documentation for custom serialization to be lacking too, but you just need to work with the Reader.State property. Once you've identified the start of the array you need to iterate over each of the elements until state == BsonReaderState.EndOfArray. You need to ensure that you only call ReadBsonType when the state is "type".
The following example deserializes an array of documents:
context.Reader.ReadStartArray();
bool ctn = true;
do
{
//If in "type" state we can call ReadBsonType. The order is important.
if (context.Reader.State == BsonReaderState.Type)
{
context.Reader.ReadBsonType();
}
//Now that we're in a value state we can read the value safely
if (context.Reader.State == BsonReaderState.Value)
{
if (context.Reader.CurrentBsonType == BsonType.Document)
{
//Handle the document here. In this case pass I it to a private method
DeserializeChild(context, args, node);
}
//state will now be "type", so continue do loop and recheck type
continue;
}
if ( BsonReaderState.EndOfArray == BsonReaderState.EndOfArray)
{
context.Reader.ReadEndArray();
ctn = false;
}
} while (ctn);
Ah, it's done with:
while (reader.readBsonType() == BsonType.DOCUMENT) {
reader.readStartDocument();
// ...
reader.readEndDocument();
}
(it's an array of documents in this case, I'm still new to BSON so I don't know how it would work for an array of values).

insertion sort on linked list

//I wrote java code for insertion method on doubly linked list but there is a infinite loop //when I run it. I'm trying to find a bug, but have not found so far. any suggestions?
//it is calling a helper function
public IntList insertionSort ( ) {
DListNode soFar = null;
for (DListNode p=myHead; p!=null; p=p.myNext) {
soFar = insert (p, soFar);
}
return new IntList (soFar);
}
// values will be in decreasing order.
private DListNode insert (DListNode p, DListNode head) {
DListNode q=new DListNode(p.myItem);
if(head==null){
head=q;
return head;
}
if(q.myItem>=head.myItem){
DListNode te=head;
q.myNext=te;
te.myPrev=q;
q=head;
return head;
}
DListNode a;
boolean found=false;
for(a=head; a!=null;){
if(a.myItem<q.myItem){
found=true;
break;
}
else{
a=a.myNext;
}
}
if(found==false){
DListNode temp=myTail;
temp.myNext=q;
q.myPrev=temp;
myTail=q;
return head;
}
if(found==true){
DListNode t;
t=a.myPrev;
a.myPrev=q;
t.myNext=q;
q.myPrev=t;
q.myNext=a;
}
return head;
}
Your code is a bit hard to read through but I noticed a few problems
First:
handling the case where you are inserting a number at the head of the list:
if(q.myItem>=head.myItem){
DListNode te=head;
q.myNext=te;
te.myPrev=q;
q=head;
return head;
}
specifically the line q=head; and the return. q=head can be removed, and it should return q not head because q is the new head. I think what you meant to do was head=q; return head;. The current code will essentially add the new node on the front but never return the updated head so they will "fall off the edge" in a way.
Second:
I am assuming myTail is some node reference you are keeping like myHead to the original list. I don't think you want to be using it like you are for the sorted list you are constructing. When you loop through looking for the place to insert in the new list, use that to determine the tail reference and use that instead.
DListNode lastCompared = null;
for(a=head; a!=null; a=a.myNext) {
lastCompared = a;
if(a.myItem<q.myItem) {
break;
}
}
if( a )
{
// insert node before a
...
}
else
{
// smallest value yet, throw on the end
lastCompared.myNext = q;
q.myPrev = lastCompared;
return head;
}
Finally make sure myPrev and myNext are being properly initialized to null in the constructor for DListNode.
disclaimer I didn't get a chance to test the code I added here, but hopefully it at least gets you thinking about the solution.
A couple stylistic notes (just a sidenote):
the repeated if->return format is not the cleanest in my opinion.
I generally try and limit the exit points in functions
There are a lot of intermediate variables being used and the names are super
ambiguous. At the very least try and use some more descriptive
variable names.
comments are always a good idea. Just make sure they don't just explain what the code is doing - instead try and
convey thought process and what is trying to be accomplished.

Simple.Data Select specific columns

I have a call made to Simple.Data where I want to limit the columns that are being brought back. However I am hitting problems..
This works fine:
var db = Database.Open();
var questionIdRow = db.Question.FindByFriendlyId(friendlyId);
if (questionIdRow == null) return Guid.Empty;
return questionIdRow.QuestionId;
However, the following doesn't work (I get a Simple.Data.UnresolvableObjectException 'Column not found')
var db = Database.Open();
var questionIdRow = db.Question.FindByFriendlyId(friendlyId)
.Select(db.Question.QuestionId);
if (questionIdRow == null) return Guid.Empty;
return questionIdRow.QuestionId;
I was under the impression from the Simple.Data documentation that this was all that I needed to do to limit the selected columns. Note that the selection is simply selecting the same column that is referenced later on.
The actual exception is thrown on the var questionIdRow = line.
Can anybody give me some guidance?
this is a common problem, and has actually led to FindBy being deprecated before we even get to 1.0. The problem is that FindBy returns a record straight away, so you can't continue to call query methods on it.
The correct approach is to call FindAllBy and end with a First or FirstOrDefault:
var db = Database.Open();
var questionIdRow = db.Question.FindAllByFriendlyId(friendlyId)
.Select(db.Question.QuestionId)
.FirstOrDefault();
if (questionIdRow == null) return Guid.Empty;
return questionIdRow.QuestionId;

Convert IEnumerable<XElement> to XElement

The return type of my query is IEnumerable<XElement>. how can i convert the resultant data to XElement type? is it possible? could some body help me to understand this.
var resQ = from e in docElmnt.Descendants(xmlns + Constants.T_ROOT)
.Where(x => x.Attribute(Constants.T_ID).Value == "testid")
select e;
I have to pass resQ as a parameter to the below function. in order to do that I have to convert resQ to XElement type.
Database.usp_InsertTestNQuestions(tid, qId, qstn, ans, resQ );
As long as your query only returns a single result, you can either call Single() or First() on the result (also, there's no need for the extra query syntax up top):
// Use if there should be only one value.
// Will throw an Exception if there are no results or more than one.
var resQ = docElmnt.Descendents(xmlns + Constants.T_ROOT)
.Single(x => x.Attribute(Constants.T_ID).Value == "testid");
// Use if there could be more than one result and you want the first.
// Will throw an Exception if there are no results.
var resQ = docElmnt.Descendents(xmlns + Contants.T_ROOT)
.First(x => x.Attribute(Constants.T_ID).Value == "testid");
If you want to handle the case when no results are returned for the query without throwing an Exception, you can use SingleOrDefault (which will still throw an Exception if you get more than one result) or FirstOrDefault.
In addition to Justin's answer, you may want to allow for 0 elements returned or some other condition.
In that case, simply do a:
IEnumerable<XElement> resQ = docElmnt.Descendents(xmlns + Constants.T_ROOT)
.Where(x => x.Attribute(Constants.T_ID).Value == "testid");
if(resQ.Count() == 0) {
//handle no elements returned
} else if(resQ.Count() > 1) {
//handle more than 1 elements returned
} else {
XElement single = resQ.Single();
}
Most of the time I find it's best not to throw an error-- unless having exactly 1 returned is really important.
You can iterate over each element in the query and then call the method with your enumerator.
resQ.ToList().ForEach(e => ...func... );