Serializing List<javax.persistence.Tuple> to JSON - java

I am working on a project using JPA (EclipseLink 2.5.2) and Jersey 2.27 running on Tomcat 8 under Java 8. Currently I retrieve results using mapped entities and also some simple lists (distinct values, key-value pairs). I have implemented server-side paging, filtering and sorting. Now I'm trying to add the ability to dynamically aggregate data sets.
I've searched on SO and other sites for the best way to serialize a list of Tuples returned from my criteria builder query. So far I haven't found anything that answers my question. I can think of three ways to do it barring some good pointers from here:
Use reflection to create the appropriate object and supply it in my query
Run through the results one at a time and write out my own JSON for each element to a StringBuffer
ArrayList<HashMap<String, Object>> where the HashMap is built using the alias and matching result then added to the list, list is serialized as normal
So if there is no "easy" way to serialize a list of tuples, which method above makes the most sense? Option 3 definitely works, but feels like a kludge to me.

Generally using Reflection in business logic does not sound good so you should eliminate first option (1.). (2.) - serialising object one by one sounds good when you want to use streaming API, in your case it is not an option. Third one (3.) looks pretty normal and it a good solution because it is generic and List<Map<String, Object>> fits extremely well to JSON specification:
JSON array - List
JSON object - Map
property - String
any JSON object - Object
You should try to convert query result on EclipseLink level. See JPA 2.0 native query results as map question where is suggested to convert query result to ResultType.Map.

Related

How to use Spring Data Mongo distinct method with query and limit

Looking at the following code:
mongoOps.getCollection("FooBar")
.distinct("_id", query(where("foo").is("bar")).limit(10).getQueryObject());
I would expect this to return only the first 10 distinct _ids of collection FooBar.
But unfortunately, running this against a Collection having more than 10 documents matching the criteria, it returns all of them ignoring the limit(10) specified here.
_id is an ObjectId.
How can I achieve this?
Is it a bug in Spring Data?
I'm already aware how I can achieve this using an aggregate but I'm trying to simplify the code if possible since using an aggregate takes many more lines of code.
FYI: I'm using Spring Data Mongodb 1.10.10 and unfortunately, updating is currently not an option.

Dynamic attributes in DynamoDB?

I am learning about DynamoDB and one of the benefits I have read about NoSQL is that the data does not need to be standardized. I was wondering if it is possible in Java to support inserting into a DynamoDB table with an unknown number and type of attributes. Is there any way in the DynamoDBMapper or JPA that supports this? For example, reading form a Spreadsheet that contains different columns depending on the sheet, but is guaranteed to have two specific columns (hash and range) regardless.
Thank you.
Is there any way in the DynamoDBMapper or JPA that supports this
JPA (or generally any object mapping framework) would map strongly typed objects into the DynamoDB, so the database provides more flexibility than the object framework, no issue in that
While you work with fixed objects, the DynamoDBMapper seems to be good choice
Spreadsheet that contains different columns depending on the sheet
Lets assume you don't know the sheet columns upfront and you need to store 'any column' that you encounter.
IMHO you would have no easy way to map 'any column' into strongly typed Java objects, for that use case I see the best fit a key/value map.
As far I know you cannot store a Map attribute with the DynamoDBMapper (please correct me if I am wrong) , so for working with flexible schema I'd skip JPA or mapper layer completely.

Serializing and deserializing prepared queries in ORMLite

Is there any way to serialize an ORMLite prepared query and then deserialize to restore it to the original form?
In Android passing parameters to activities or fragments must be done in serialized form. Now it seems impossible to pass prepared queries as arguments in a Bundle.
Serializing them is not a problem because there is PreparedStmt<T>.getStatement() but I have not found any way to reverse the process.
There is always the solution of putting the query in a map with a String key, passing the key as an argument and then retrieving the query using that key, but I am searching for a simpler solution.
Is there any way to serialize an ORMLite prepared query and then deserialize to restore it to the original form?
Right now the answer is no and I don't think there is any way for the code to be changed to support it. A prepared query contains points to the DAO, to the internal connection source classes, etc. so it can perform its duties. There is no easy way to serialize all that information at this time.
Serializing them is not a problem because there is PreparedStmt.getStatement() but I have not found any way to reverse the process.
By reverse the process you mean generate a PreparedStmt from the query string? You should be able to do:
GenericRawResults<T> results = dao.QueryRaw(queryString, dao.getRawRowMapper());

mongodb and java driver rebuild objects

I'm trying to understand how mongodb works and I have some questions.
I understand how to delete, insert, update and select but I have some "best practice questions"
1) Did we have to create an index or we can just use the _id wich is auto generated ?
2) If I have for exemple 2 kind of objects (cars and drivers) with a n-n relation between. I have to get 3 collections(car, driver and a collection witch link the two others) ?
3) To rebuild my objects I have to parse my json with the JSON object ?
Thanks for your help
Three good questions. I'll answer each in turn.
1) Did we have to create an index or we can just use the _id wich is
auto generated ?
You should definitely try and (re)use the _id index. This usually means moving one of the unique fields (or primary key, in RDMS speak) in your domain object to be mapped to the _id field. You do have to be careful that the field does not get to large if you will be sharding but that is a separate question to answer.
2) If I have for exemple 2 kind of objects (cars and drivers) with a
n-n relation between. I have to get 3 collections(car, driver and a
collection witch link the two others) ?
No! You only need two collections. One for the cars and one for the drivers. The "join table" is pulled into each of the collections as DBRefs.
Each car document will contain an array of DBRef or document references. Those references contain the database name (optional), collection name and _id for a driver document. You will have a similar set of DBRefs in the driver document to each of the driven cars.
Most of the drivers have support for creating and de-referencing these document references.
3) To rebuild my objects I have to parse my json with the JSON object?
MongoDB lingua franca is actually BSON. You can think of BSON as a typed, binary, easily parsed version of JSON. Most of the drivers have some capability to convert from JSON to their representation of BSON and back. If you are developing in Java then the 10gen driver's utility class is JSON. For the Asynchronous driver it is called Json.
Having said that, unless your data is already JSON I would not convert your data to JSON to convert it to BSON and back again. Instead either look for a ODM (Object-Document-Mapper) for your language of choice or perform the translation for your domain object to the driver's BSON representation directly.
HTH-
Rob.

Manipulating JSON List and Map in Java

I'm working on a project that save/retrieve JSON-type string to a database.
Everything works ok, i.e., save and update is safe for types String,Number and Boolean, but for List and Map, I want to see what is the safe way to manipulate List and Map when the data comes back and forth the database especially when items on the list become large, i.e thousands of items, say for list of "friends" and "followers"
I am also concerned with potential data corruption when processing the a JSON List or Map in Java.
What is the safe way to update a List and Map using JSON.Simple library while not loading everything (every item) in memory.
For example I just need to insert one (1) item in a JSON list string that is stored in a database.
JSON isn't suitable for ORM (object relational mapping). This is why NoSQL databases store JSON as document (i.e. the whole thing). Therefore, JSON.Simple has no support for lazy loading part of the JSON structures.
Relational database don't map well to JSON (except for primitives) as you noticed because the natural data structure for List is a 1:N mapping where the list type has an index column (i.e. position of the element in the list) while Map needs an N:M mapping.
So a better solution might be to store the whole JSON string in a CLOB (instead of trying to break it apart) or saving the whole JSON string in a CLOB + extracting a few key fields so you can index the data properly.
But when you work with the JSON you will either have to write your own OR mapper which supports lazy loading of JSON arrays and maps or you will have to read the whole structure into RAM every time.

Categories

Resources