0

I am trying to save multiple strings into one column into MySQL DB. Using Spring Data JPA with Hibernate.

I have my PersonserviceImpl as

    @Override
public void createnewPerson(AppPerson appPerson) {

    List<AppPersonproperty> personProperties = personpropertyRepository
            .findByPersonid(appPerson.getAppPersontype().getId());

        for (AppPersonproperty app : personProperties) {
            System.out.println(app.getPersonpropertyname());
            entity.setPersonpropertyname(app.getPersonpropertyname());
        }

    personRepository.save(entity);
}

Only the last value gets save from Person's previous names.

Sysout:

SpiderMan
Spidey

Expected value in the column:

SpiderManSpidey

Actual value in the column:

Spidey

What am I doing wrong here?

1 Answer 1

1

Why would JPA append Strings in a column when you are replacing them on the Java side?

If you want them appended in the column you need to append them on the Java side as well. Replacing

entity.setPersonpropertyname(app.getPersonpropertyname());

with

entity.setPersonpropertyname(entity.getPersonpropertyname() + app.getPersonpropertyname());

should do the trick.

Sign up to request clarification or add additional context in comments.

1 Comment

What a dumbo I am.! . Yes you are right Jens. Your trick is working.!!

Your Answer

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.