简体   繁体   English

Spring Data JPA-for循环不保存实体

[英]Spring Data JPA - for loop not saving entity

I am trying to save multiple strings into one column into MySQL DB. 我正在尝试将多个字符串保存到MySQL DB的一栏中。 Using Spring Data JPA with Hibernate. 将Spring Data JPA与Hibernate结合使用。

I have my PersonserviceImpl as 我有我的PersonserviceImpl作为

    @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. 只有最后一个值会从Person的先前名称中保存。

Sysout: SYSOUT:

SpiderMan
Spidey

Expected value in the column: 列中的期望值:

SpiderManSpidey

Actual value in the column: 列中的实际值:

Spidey

What am I doing wrong here? 我在这里做错了什么?

Why would JPA append String s in a column when you are replacing them on the Java side? 当您在Java端替换String为什么JPA会将String附加在列中?

If you want them appended in the column you need to append them on the Java side as well. 如果希望将它们附加在列中,则还需要将它们附加在Java端。 Replacing 更换

entity.setPersonpropertyname(app.getPersonpropertyname());

with

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

should do the trick. 应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM