I'm having a problem reversing a list object that is fetched from a database using jpa.
I'm using Collections.reverse(myListObject).
What I intend to do is to retrieve the list from database, reverse it, then send it to my jsp page.
I get the below error:
"java.lang.UnsupportedOperationException: Result lists are read-only."
Any solution for this?
Since the list is coming from JPA, the first approach is to avoid reversing the list in memory, doing it on RDBMS side instead. Change the ORDER BY clause of your JPQL if this approach is viable in your situation.
If this cannot be done, for example, because you have no direct control over your JPQL, reverse a copy:
List<MyType> rev = new ArrayList<MyType>(myListObject);
Collections.reverse(rev);
Related
I have a java arraylist that is made like this:
{[{},{}], [{},{}], [{},{}], [{},{}]} of around four thousand records.
I have a particular key through which I want to search in one of the objects in this list and fetch that particular array where that
record matches. The search key is a string.
Is there a solution to this without traversing through the entire list.
It is basically a list that is constructed like this:
List<Object[]> list = new ArrayList<>();
I am using this to fetch the the data from two tables using a join. Individual records of each tables map to these objects.
Say table1: {a:1,b:2,c:3} and table2: {x:1,y:2,z:3}
the data returned would be
{[{a:1,b:2,c:3}, {x:1,y:2,z:3}],[{a:2,b:3,c:4}, {x:2,y:3,z:4}]}
How will I search for say in which array in the list is a=2.
Thanks
If you do not want to be a victim of the linear search, you should consider using another type of data structure than List.
The use case you described seems like a good match for a Map in general. If you want constant time key lookup, consider using HashMap instead.
I am trying to use ActiveJDBC to properly convert an integer[] from my PostgreSQL database into the java equivalent int[]. I get the fetch done properly, but the object that is returned is weblogic.jdbc.wrapper.Array_org_postgresql_jdbc_PgArray. I have yet to find a way to convert that.
I have tried 2 different ways of accessing the data:
First, using the standard record.findFirst("id = ?", id) format. Because I have multiple schemas in my database, I added the #Table notation to my Model.
Second, I tried doing a record.findBySQL("select array from record where id = ?", id). I also tried array::integer[].
Each time I get back the PgArray type. I have searched for a way to convert this type into something else for use, but nothing has worked.
Is there a way to do this? Do I need to use a different way of data retrieval other than ActiveJDBC?
JDBC defines java.sql.Array to handle array typed columns. PgArray is just the Postgres implementation of java.sql.Array.
Given the array object a you can call (Integer[])a.getArray() to get an Integer array from the JDBC array object (if the JDBC driver has decided that it will return you Integer objects and not some other Numbers). A helper method to extract the value and convert to int[] would probably be a good idea.
Don't know if activejdbc has support to do this conversion behind the scenes.
Suppose my document contains two field - from and to. I want to fetch all the documents whose from is lessThanEqual the provided value and to is greaterthanEqual the provided value.
the way I can do that is
List<T> findAllByFromLessThanEqualAndToGreaterThanEqual(Integer from, Integer to)
I am using method query way to achieve it.
Now suppose I have a list of Integer, is there any efficient way to fetch the records in single query.
Something like instead of iterating over the list of Integer and firing the query again and again
List<T> list = someIntList
.stream()
.map(someInt -> someRepository.findAllByFromLessThanEqualAndToGreaterThanEqual(someInt, someInt))
.flatMap(Collection::stream).collect(Collectors.toList());
The above way will hit the database again and again, so is there any more efficient way to achieve it in a single query.
Is possible to order elements in List indirectly (in Java 7)? Assume having elements of list which are post which have atributs (atributs of type Post) id, text, timestamp (millisec from 1970 - just number type long).
The posts are stored in database (MySQL) and they came as result of different SELECTs. It's because posts is something tweets on twitter - posts which added user, posts of users which user follows and maybe some others. The idea is do some SELECTSs, each get result as list and these lists will be added to one list, which I want to order by atribute (timestamp). Is any easy way to sort it indirectly (from higher to lower - to have newest to oldest posts) by this atribute (timestamp)? I know that List have attribute sort and I should probably do something with that.
You probably want to use a Set, and more specifically a SortedSet (the basic implementation of it being a TreeSet), instead of a List. This will require that your post class implement Comparable of itself, however.
Of course, there is always the option to ORDER BY at the database level. This way you can use a classical List.
You can query the database using Order By clause to get the result in order of timestamp, then you won't have to do sorting at java side.
There are two ways to do it. You can do it from query by using ORDER BY column or use comparable and comparator to sort objects. link =
http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/
The recommended way of using merge() is to first get the DTO first before inputting the changes.
public void merge(PersonModel model) {
Person inputDTO = PersonBuilder.build(model)
Person dto = get(pk)
dto.setName(inputDTO.getName())
dto.getChildren().clear()
Iterator<Child> iter = inputDTO .getChildren().Iterator();
while(iter.hasNext()){
dto.getChildren().add(iter.next());
}
dto.merge();
}
Is there a more elegant way of performing such operation translating domain model to dto and merging it so that no data are accidentally deleted.
Example of problem:
Hibernate: prevent delete orphan when using merge();
I find the need to clear the list and adding it very wasteful.
Can someone recommend me a design pattern or a way to code it properly?
Thank you
ADD ON:
1) Possible to use Hibernate Hashset to replace List? Will hibernate hashset replace elements base on primary keys?
any help?
"The recommended way of using merge() is to first get the DTO first before inputting the changes"
Who recommended you to do this?
"Is there a more elegant way of performing such operation translating domain model to dto and merging it so that no data are accidentally deleted."
I don't think you can translate domain objects to DTOs. A DTO is just about data, a domain object is data, behaviour and context. Completely different.
If you don't have behaviour and context in your domain objects (a.k.a. anemic domain model), you don't need an extra DTO layer that just duplicates the objects.
Because you tagged this question with Hibernate and mentioned it in your question, you don't need to call merge yourself because you just got the object from the database and Hibernate will flush the session to synchronize the changes with the database.
"Possible to use Hibernate Hashset to replace List? Will hibernate hashset replace elements base on primary keys?"
I would replace the List with a Hashset, since the table where the data is going to be stored is a set, not a list (you can't have duplicate records). A hashset will not replace elements based on primary keys. A set (any set, Hibernate's implementation is no different) works by preventing duplicates. It uses your equals() and getHashCode() implementation to find out if there is already an object in that set. If that is the case, it won't be added but it keeps the original.