Is it possible to store an ArrayList in a Java Database?

3

Given Banco de Dados using Java , can you store a ArrayList object in Banco de Dados MySql ? If yes, how can I store this type of data? And how can I retrieve it from the database? If possible, could you show a simple example?

    
asked by anonymous 02.03.2016 / 02:44

2 answers

7

The question is asked inappropriately. You can understand that you have a list of values and want to save it to a MySQL database, but these are two completely different worlds and you need to understand how they relate to each other.

The correct answer depends on several factors.

Data model

The database knows nothing about Java and does not need to know. So, first, you must define a data model capable of storing the values.

The most "correct" model would be to store the values each in a record, perhaps in a table with a 1:N relationship (one to many) with a main table, in case the list is bound to another record. p>

By your comment, you want to store the values in a single field. While this is generally more "comfortable" from the programmer's point of view, it can generate several side effects. The simplest case is if you need to search for records by looking up values from that list, because in that case you can not simply use a WHERE clause, but you'll have to go through all the records.

If this is not a problem, your template will probably contain a VARCHAR field to store the list in text format. In that case, the question could simply be about converting a list to text and then back to a list.

Storing a list in a text field

If the idea is just to store integers and you are using Java 8, you can easily convert a list to a String, like this:

List<Integer> lista = Arrays.asList(1, 2, 3);
String texto = lista.stream()
        .map(String::valueOf)
        .collect(Collectors.joining(","));

The code basically takes the integer list, converts it to a list of strings, and joins everything by separating it with a comma.

Then, to get the text and rebuild the list, you can do this:

List<Integer> novaLista = Arrays.stream(texto.split(","))
        .map(Integer::valueOf)
        .collect(Collectors.toList());

The code breaks the text by separating by commas, converts each item to a Integer , and finally retrieves the result in a list.

You need to be careful about the size of the field, not to exceed the number of characters.

In addition, this approach is not secure for other data types that may contain commas in the values.

Storing the List Using Serialization

Another approach would be to serialize ArrayList using the Java serialization API, storing the object as a sequence of bytes, which could later be deserialized.

It's just an idea and there are other answers here in the OS that teach you how to do this.

This approach is flexible because it allows you to store any type of list, as long as the objects within it are also serializable.

The problem is that a sequence of bytes in the database is not a very nice thing to deal with.

Storing the list using JSON

Another more flexible alternative is to convert ArrayList to a JSON object and store it in the database. JSON is an object representation format in text, for example:

{ itens = [ 1, 2, 3 ] }

This approach is the most flexible because it allows any type of data to be stored.

The downside is you have to use a more complex API to convert between ArrayList and JSON. There are several answers here in the OS that teach how to generate JSON or vice versa.

    
02.03.2016 / 07:20
0

If you're working with JPA , I believe that Oracle documentation approach the subject very clearly.

In the link above, it is simplistically exemplified how the annotation that does the "heavy work" will be manipulated.

    
02.03.2016 / 04:06