Persist list of enumeration on JPA, converter not working












0















I'm trying to persist a list of enumeration on the database, using JPA and hibernate.



My model looks like that:



@Id
@Column
String id;

@Column
@Convert(converter = EnumConverter.class)
List<Enum> listEnum;


And I would like to persist that list of enum as String and use a converter (EnumConverter) in order to store it and read it from the database.



My converter uses Jackson in order to serialize and deserialize. Example:



@Converter(autoApply = true)
public class EnumListConverter implements AttributeConverter<List<Enum>, String> {

private final ObjectMapper objectMapper = new ObjectMapper();

@Override
public String convertToDatabaseColumn(final List<Enum> list) {
try {
return objectMapper.writeValueAsString(list);
} catch (JsonProcessingException e) {
return "";
}
}

@Override
public List<Enum> convertToEntityAttribute(final String list) {
try {
return Arrays.asList(objectMapper.readValue(list, Enum.class));
} catch (IOException e) {
return new ArrayList<>();
}
}
}


The problem that I'm having is that is storing it fine, but when I try to get the code, it returns me an array of size 1 (type ArrayList) like the following instead of an array of size 3:



"["Value1","Value2","Value3"]"


I am using a CrudRepository with a simple native query:



SELECT list from the entity;


Does someone know what I'm missing? I would like to have an ArrayList of three values instead of one with one value. When I debug the converter it does it right, so the problem has to be after converting.










share|improve this question




















  • 1





    Storing values as csv in one column is no good db design. learn about normalization

    – Jens
    Nov 14 '18 at 12:25













  • It is very simple exercise. I just wanted to do it fast and use object mapper to store and retrieve it from database.

    – Gerardo
    Nov 14 '18 at 12:27











  • Please post EnumConverter source code.

    – user10639668
    Nov 14 '18 at 12:34











  • @EugenCovaci I added it!

    – Gerardo
    Nov 14 '18 at 13:00











  • @Jens, good point however some databases offer arrays as column types so if that's the case one could consider Vlad Mihalcea's library in order to map SQL arrays, see this:vladmihalcea.com/…

    – garfield
    Nov 14 '18 at 14:41


















0















I'm trying to persist a list of enumeration on the database, using JPA and hibernate.



My model looks like that:



@Id
@Column
String id;

@Column
@Convert(converter = EnumConverter.class)
List<Enum> listEnum;


And I would like to persist that list of enum as String and use a converter (EnumConverter) in order to store it and read it from the database.



My converter uses Jackson in order to serialize and deserialize. Example:



@Converter(autoApply = true)
public class EnumListConverter implements AttributeConverter<List<Enum>, String> {

private final ObjectMapper objectMapper = new ObjectMapper();

@Override
public String convertToDatabaseColumn(final List<Enum> list) {
try {
return objectMapper.writeValueAsString(list);
} catch (JsonProcessingException e) {
return "";
}
}

@Override
public List<Enum> convertToEntityAttribute(final String list) {
try {
return Arrays.asList(objectMapper.readValue(list, Enum.class));
} catch (IOException e) {
return new ArrayList<>();
}
}
}


The problem that I'm having is that is storing it fine, but when I try to get the code, it returns me an array of size 1 (type ArrayList) like the following instead of an array of size 3:



"["Value1","Value2","Value3"]"


I am using a CrudRepository with a simple native query:



SELECT list from the entity;


Does someone know what I'm missing? I would like to have an ArrayList of three values instead of one with one value. When I debug the converter it does it right, so the problem has to be after converting.










share|improve this question




















  • 1





    Storing values as csv in one column is no good db design. learn about normalization

    – Jens
    Nov 14 '18 at 12:25













  • It is very simple exercise. I just wanted to do it fast and use object mapper to store and retrieve it from database.

    – Gerardo
    Nov 14 '18 at 12:27











  • Please post EnumConverter source code.

    – user10639668
    Nov 14 '18 at 12:34











  • @EugenCovaci I added it!

    – Gerardo
    Nov 14 '18 at 13:00











  • @Jens, good point however some databases offer arrays as column types so if that's the case one could consider Vlad Mihalcea's library in order to map SQL arrays, see this:vladmihalcea.com/…

    – garfield
    Nov 14 '18 at 14:41
















0












0








0








I'm trying to persist a list of enumeration on the database, using JPA and hibernate.



My model looks like that:



@Id
@Column
String id;

@Column
@Convert(converter = EnumConverter.class)
List<Enum> listEnum;


And I would like to persist that list of enum as String and use a converter (EnumConverter) in order to store it and read it from the database.



My converter uses Jackson in order to serialize and deserialize. Example:



@Converter(autoApply = true)
public class EnumListConverter implements AttributeConverter<List<Enum>, String> {

private final ObjectMapper objectMapper = new ObjectMapper();

@Override
public String convertToDatabaseColumn(final List<Enum> list) {
try {
return objectMapper.writeValueAsString(list);
} catch (JsonProcessingException e) {
return "";
}
}

@Override
public List<Enum> convertToEntityAttribute(final String list) {
try {
return Arrays.asList(objectMapper.readValue(list, Enum.class));
} catch (IOException e) {
return new ArrayList<>();
}
}
}


The problem that I'm having is that is storing it fine, but when I try to get the code, it returns me an array of size 1 (type ArrayList) like the following instead of an array of size 3:



"["Value1","Value2","Value3"]"


I am using a CrudRepository with a simple native query:



SELECT list from the entity;


Does someone know what I'm missing? I would like to have an ArrayList of three values instead of one with one value. When I debug the converter it does it right, so the problem has to be after converting.










share|improve this question
















I'm trying to persist a list of enumeration on the database, using JPA and hibernate.



My model looks like that:



@Id
@Column
String id;

@Column
@Convert(converter = EnumConverter.class)
List<Enum> listEnum;


And I would like to persist that list of enum as String and use a converter (EnumConverter) in order to store it and read it from the database.



My converter uses Jackson in order to serialize and deserialize. Example:



@Converter(autoApply = true)
public class EnumListConverter implements AttributeConverter<List<Enum>, String> {

private final ObjectMapper objectMapper = new ObjectMapper();

@Override
public String convertToDatabaseColumn(final List<Enum> list) {
try {
return objectMapper.writeValueAsString(list);
} catch (JsonProcessingException e) {
return "";
}
}

@Override
public List<Enum> convertToEntityAttribute(final String list) {
try {
return Arrays.asList(objectMapper.readValue(list, Enum.class));
} catch (IOException e) {
return new ArrayList<>();
}
}
}


The problem that I'm having is that is storing it fine, but when I try to get the code, it returns me an array of size 1 (type ArrayList) like the following instead of an array of size 3:



"["Value1","Value2","Value3"]"


I am using a CrudRepository with a simple native query:



SELECT list from the entity;


Does someone know what I'm missing? I would like to have an ArrayList of three values instead of one with one value. When I debug the converter it does it right, so the problem has to be after converting.







java jpa spring-data-jpa






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 15:28









Lorelorelore

2,02761427




2,02761427










asked Nov 14 '18 at 12:24









GerardoGerardo

476




476








  • 1





    Storing values as csv in one column is no good db design. learn about normalization

    – Jens
    Nov 14 '18 at 12:25













  • It is very simple exercise. I just wanted to do it fast and use object mapper to store and retrieve it from database.

    – Gerardo
    Nov 14 '18 at 12:27











  • Please post EnumConverter source code.

    – user10639668
    Nov 14 '18 at 12:34











  • @EugenCovaci I added it!

    – Gerardo
    Nov 14 '18 at 13:00











  • @Jens, good point however some databases offer arrays as column types so if that's the case one could consider Vlad Mihalcea's library in order to map SQL arrays, see this:vladmihalcea.com/…

    – garfield
    Nov 14 '18 at 14:41
















  • 1





    Storing values as csv in one column is no good db design. learn about normalization

    – Jens
    Nov 14 '18 at 12:25













  • It is very simple exercise. I just wanted to do it fast and use object mapper to store and retrieve it from database.

    – Gerardo
    Nov 14 '18 at 12:27











  • Please post EnumConverter source code.

    – user10639668
    Nov 14 '18 at 12:34











  • @EugenCovaci I added it!

    – Gerardo
    Nov 14 '18 at 13:00











  • @Jens, good point however some databases offer arrays as column types so if that's the case one could consider Vlad Mihalcea's library in order to map SQL arrays, see this:vladmihalcea.com/…

    – garfield
    Nov 14 '18 at 14:41










1




1





Storing values as csv in one column is no good db design. learn about normalization

– Jens
Nov 14 '18 at 12:25







Storing values as csv in one column is no good db design. learn about normalization

– Jens
Nov 14 '18 at 12:25















It is very simple exercise. I just wanted to do it fast and use object mapper to store and retrieve it from database.

– Gerardo
Nov 14 '18 at 12:27





It is very simple exercise. I just wanted to do it fast and use object mapper to store and retrieve it from database.

– Gerardo
Nov 14 '18 at 12:27













Please post EnumConverter source code.

– user10639668
Nov 14 '18 at 12:34





Please post EnumConverter source code.

– user10639668
Nov 14 '18 at 12:34













@EugenCovaci I added it!

– Gerardo
Nov 14 '18 at 13:00





@EugenCovaci I added it!

– Gerardo
Nov 14 '18 at 13:00













@Jens, good point however some databases offer arrays as column types so if that's the case one could consider Vlad Mihalcea's library in order to map SQL arrays, see this:vladmihalcea.com/…

– garfield
Nov 14 '18 at 14:41







@Jens, good point however some databases offer arrays as column types so if that's the case one could consider Vlad Mihalcea's library in order to map SQL arrays, see this:vladmihalcea.com/…

– garfield
Nov 14 '18 at 14:41














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300183%2fpersist-list-of-enumeration-on-jpa-converter-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300183%2fpersist-list-of-enumeration-on-jpa-converter-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine