Automatic update after jpa updates database entity object properties under one thing

Record a hidden bug discovered by accident today

Specifically, it is like this: query a record in a thing, such as Student id=1, then modify an attribute of the student object, such as name, and then execute the update statement to modify a field age through jpa For example: update student set age = 1 where id= 1. Finally, it was found that in addition to the age update in the database, the name field was also updated, and an update command executed two updates. The test code is as follows

service

@Service
@Transactional(readOnly = true)
public class LabelConfigService {

    private ISysLabelConfigRepository iSysLabelConfigRepository;


    @Autowired
    public LabelConfigService(ISysLabelConfigRepository iSysLabelConfigRepository){
        this.iSysLabelConfigRepository = iSysLabelConfigRepository;
     
    }

    @Transactional
    public void testUpdate() {
        SysLabelConfig labelConfig = iSysLabelConfigRepository.findOne(2637);
        labelConfig.setRemark("wlllllllllllll");
        //即使不执行下面的update labelConfig依然会update
        iSysLabelConfigRepository.updateSource(labelConfig.getId(),12);
    }

}

repository

public interface ISysLabelConfigRepository extends JpaRepository<SysLabelConfig,Integer>
        ,JpaSpecificationExecutor<SysLabelConfig> {

    @Modifying
    @Query("update SysLabelConfig set source=?2 where id=?1")
    void updateSource(Integer id, Integer source);
}

test

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ApplicationBootstrap.class)
@WebAppConfiguration
public class TestSysLabelConfig {

    @Autowired
    private LabelConfigService labelConfigService;


    @Test
    public void testUpdate(){
        labelConfigService.testUpdate();

    }



}

Execute sql as follows

0:24:16.495 [main] INFO  c.c.j.m.s.s.TestSysLabelConfig - Started TestSysLabelConfig in 23.558 seconds (JVM running for 25.682)
Hibernate: select syslabelco0_.id as id1_17_0_, syslabelco0_.create_time as create_t2_17_0_, syslabelco0_.creator as creator3_17_0_, syslabelco0_.label_category as label_ca4_17_0_, syslabelco0_.label_name as label_na5_17_0_, syslabelco0_.label_sub_category as label_su6_17_0_, syslabelco0_.remark as remark7_17_0_, syslabelco0_.source as source8_17_0_ from sys_label_config syslabelco0_ where syslabelco0_.id=?
Hibernate: update sys_label_config set label_category=?, label_name=?, label_sub_category=?, remark=?, source=? where id=?
Hibernate: update sys_label_config set source=? where id=?

In fact, even if the update statement is not executed, the modified database entity objects will be automatically updated! ! !

Solution

1. Do not modify the properties of the entity object under things

2. Put things on the interface method of the repository

ps: Be careful about the operation of the entity object in things, it is best not to modify the attribute of the entity object or convert the entity object to a vo object and then perform further operations

Guess you like

Origin blog.csdn.net/name_is_wl/article/details/84981462