4

I am trying to write an Java- Array List into postgres database. But i am not able to DO, i don't get error as well. I am trying to post data

"skill":["java,dotnet"]

using postman tool but on Post nothing happens and i don't see any data in db.I also don't get any error in my console.please help me out on this i m wondering how to do this

I am able to read Array data from database but cannot write

 /*
 * Spring Entity:
 */
 @Column(name="skill" , columnDefinition = "text[]")
 @Convert(converter =ListToStringConverter.class)
 private List<String> skill=new ArrayList<>(); 
 ublic List<String> getSkill() {
 return skill;
 }
 public void setSkill(List<String> skill) {
 this.skill= skill;
 }
 @Converter
 public class ListToStringConverter implements AttributeConverter<List<String>, String> { 
 @Override
 public String convertToDatabaseColumn(List<String> attribute) {
 if (attribute == null || attribute.isEmpty()) {
 return "";
 }
 return StringUtils.join(attribute, ",");
 }
 @Override
 public List<String> convertToEntityAttribute(String dbData) {
 if (dbData == null || dbData.trim().length() == 0) {
 return new ArrayList<String>();
 }
 String[] data = dbData.split(",");
 return Arrays.asList(data);
 }
 }
Controller Code:

This is my Controller code to create a object in database and i use a interface service that is call from controller to Save to db-Postgres which is a JPA repo

 @RequestMapping(value = "/reqCreate", method = RequestMethod.POST)
 public ResponseEntity<Requirement> addRequirement(@RequestBody Requirement requirement) {
 reqService.save(requirement); 
 return new ResponseEntity<Requirement>(requirement, HttpStatus.CREATED);
 }
Service:
 public void save(Requirement r) { 
 reqRepo.save(r);
 }
Kohei TAMURA
5,1408 gold badges29 silver badges53 bronze badges
asked Jul 7, 2017 at 13:52
5
  • Hi and welcome to SO! You showed us how you handle data conversion between List and String. But how do you actually access the database? Please edit your question and add that code to it. Do you have any error messages or exceptions? Commented Jul 7, 2017 at 14:00
  • Does it work without columnDefinition = "text[]"? Commented Jul 7, 2017 at 14:42
  • care to say whether your TABLE schema was created with a column of type text[] ? Commented Jul 7, 2017 at 17:07
  • Hi Lupz i get the follwoing error code :ERROR: column "skill" is of type text[] but expression is of type character varying Hint: You will need to rewrite or cast the expression. Commented Jul 8, 2017 at 11:55
  • @Carrie were you able to find a solution for this. I am facing the same issue and not sure how to go about it. Commented Nov 18, 2017 at 9:34

1 Answer 1

4

The columnDefinition seems to be wrong.

You want to convert Collection into String, but you still declare column of array type.

Replace "text[]" with "text" and check if it works?

answered Jul 7, 2017 at 14:51
Sign up to request clarification or add additional context in comments.

2 Comments

Tough luck i am still not able to save to Database:-( i can only read this in my console###[INFO] 2017年07月08日 09:26:17:441 [org.springframework.web.servlet.DispatcherServlet] - FrameworkServlet 'dispatcherServlet': initialization started ###[INFO] 2017年07月08日 09:26:17:502 [org.springframework.web.servlet.DispatcherServlet] - FrameworkServlet 'dispatcherServlet': initialization completed in 60 ms
Make sure you let hibernate recreate the schema with new column definition.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.