Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert list in entity to single string column in database

I have a VARCHAR field in my database, and the value of this field is val1,val2,val3.

Is it possible to set this into an ArrayList<String> attribute of an entity using comma as split delimiter?

like image 296
Fra83 Avatar asked Dec 02 '15 17:12

Fra83


People also ask

How do you convert a list element to a string in Java?

The toString() method on a list returns a string that is surrounded by square brackets and has commas between items. The idea is to get rid of square brackets using the substring() method and comma and space replace using the replaceAll() method.

How do I convert a list of strings to a list of objects?

Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList); Any Collection can be passed as an argument to the constructor as long as its type extends the type of the ArrayList , as String extends Object .

How do you convert a list of strings to one string in Java?

Using StringBufferCreate an empty String Buffer object. Traverse through the elements of the String array using loop. In the loop, append each element of the array to the StringBuffer object using the append() method. Finally convert the StringBuffer object to string using the toString() method.

Can we convert list of string to string in Java?

We use the toString() method of the list to convert the list into a string.


2 Answers

If you use JPA 2.1, then you can create an AttributeConverter:

@Converter public class StringListConverter implements AttributeConverter<List<String>, String> {    @Override   public String convertToDatabaseColumn(List<String> list) {     // Java 8     return String.join(",", list);      // Guava     return Joiner.on(',').join(list);    }    @Override   public List<String> convertToEntityAttribute(String joined) {     return new ArrayList<>(Arrays.asList(joined.split(",")));   }  } 

You can use this converter in your entity:

@Column @Convert(converter = StringListConverter.class) private List<String> strings; 

For before JPA 2.1 you could do this by hand:

@Entity private MyEntity {   ...   private String strings;    public List<String> getStrings() {     return Arrays.asList(strings.split(","));   }    public void setStrings(List<String> list) {     strings = String.join(",", list);   } } 

I wrap Arrays.asList in an ArrayList in the converter, because the result is stored in the attribute and any change to that list will be written back to the database - thus I need a changeable list (I can't add anything to the result of Arrays.asList). In the before 2.1 solution the result is not connected with the attribute and a changeable list would not be synchronized with the attribute.

To query for an entity that contains a specific item in such an attribute, see my answer here

like image 179
Tobias Liefke Avatar answered Sep 20 '22 21:09

Tobias Liefke


Yes this is possible.

With Hibernate 4.3.x+ you can define an AttributeConverter although I am pretty sure this will not work for early Hibernate versions because of the List type. See this for an example: http://www.thoughts-on-java.org/jpa-21-how-to-implement-type-converter/

The other way to make this work is by implementing a custom UserType and annotating the field/getter with org.hibernate.annotations.Type. Here is a nice write up of this with examples: http://blog.xebia.com/understanding-and-writing-hibernate-user-types/

Yet another way which is JPA compatible is to have two fields, the List annotated with javax.persistence.Transient and the String but then you have manage the state synchronization between these two field in PrePersist and PreUpdate listeners yourself. Here an example for using listeners: http://alexandregama.org/2014/03/23/entity-listeners-and-callback-methods-jpa/

like image 21
Christian Beikov Avatar answered Sep 20 '22 21:09

Christian Beikov