Explicit type conversion between spring child and super java.util

In spring, I use jdbcTemplate, but I have a problem that it returns a Linkedcaseinsensitivemap when querying a List, when I do the following, I still get spring linkedcaseinsensitivemap, even if I passed it to java util List and define the left side of the job as java.util.List.

First, how is this possible?

final java.util.List<Map<String, Object>> list = (java.util.List<Map<String, Object>>) jdbc .queryForList("SELECT * FROM customer"); 

So how can this type of upcaste be achieved? without having to declare a second list, allocate memory for it, and then manually place objects in java.util.List?

Since LinkedCaseInsensitive is a subclass of a java object, Im is having difficulty figuring out how to distinguish a super object, which is a java list. How to achieve this is a mystery at the moment.

since there is currently no way to know which brokers will use our AMQ, the goal is too strictly related to jms objects, so I can not start sending spring objects, since jms should be our standard, also keep in mind that I have no way to implement AMQProtocol, I need to send basic java objects,

Since serialization in JSON was proposed, I will explain why it does not work in this case, why I need to send "as-are" objects to the recipient, since they will be placed in the Notes document.

 for (int i = 1; i <= metadata.getColumnCount(); i++) { String columnName = metadata.getColumnName(i); Object values = sqlConnection.getRset().getObject(i); doc.replaceItemValue(columnName, values); } 

So SO'ers, how to achieve this more beautiful? please help thanks in advance!

+1
source share
2 answers

An SQL select can return multiple rows, and each row has multiple columns selected. The queryForList method that you call returns a list, for each selected row, the map matching column name corresponds to the column value.

A map and a list are interfaces, so Spring can choose any implementation that he likes. He chooses the LinkedCaseInsensitiveHashMap for the Map, because this map will list the keys in insertion order. Thus, the order in which the columns were selected is not lost.

If you want to send a list of results to a recipient you know little about, you can probably best serialize it to JSON and send it as a text message.

You can serialize to JSON using a library like Gson or Jackson2 . You create a serializer and feed it with the object you want to convert to String. So, for example, in Gson, where the serializer class is called Gson:

 TextMessage message; // initialize message and headers however you like // then serialize it to String: Gson gson = new Gson(); String json = gson.toJson(list); // and set it in the message: message.setText(json); 

(You can also let Spring JmsTemplate do this for you using a MessageConverter, which converts to JSON, but I would appreciate that this is a little harder to work with.)

Alternatively, if you want to customize the map that you send as an ObjectMessage, you can use another request method that allows you to specify a custom RowMapper that creates the java.util.Map implementation to your liking. Please note: if you use TreeMap, it sorts the columns in alphabetical order, and if you use HashMap, it will put them in random order.

Then the receiver can unpack the JSON back into Java objects. gson.fromGson (json) will return a list of maps.

+1
source

This is the only way I figured out how to have a child to become its parent class without going into methods - in my described scenario, do the following:

 final java.util.ArrayList<java.util.Map<String, Object>> javaList = new java.util.ArrayList<java.util.Map<String, Object>>(); final java.util.List<java.util.Map<String, Object>> list = jdbc .queryForList("SELECT * FROM customer"); javaList.addAll(list); 

But it does not seem good to me, how can this be achieved in a more correct way?

0
source

Source: https://habr.com/ru/post/1439207/


All Articles