简体   繁体   中英

Java : getting a referenced data base object and then modifying it

here is my question, given this code:

studentOutcomeService.getAll()
                     .get(getStudentOutcomeByID(
                            selectedStudentOutcome.getId().intValue()))
                     .setDescription(description);

studentOutcomeService.getAll()
                     .get(getStudentOutcomeByID(
                            selectedStudentOutcome.getId().intValue()))
                     .setSequenceNumber(Integer.valueOf(sequenceNumber));

studentOutcomeService.getAll()
                     .get(getStudentOutcomeByID(
                            selectedStudentOutcome.getId().intValue()))
                     .setShortName(shortName);

studentOutcomeService.getAll()
                     .get(getStudentOutcomeByID(
                            selectedStudentOutcome.getId().intValue()))
                     .setIdentifier(identifier);

I am unhappy with the fact that I am querying the database 4 times to constantly update a field.

I trued creating a StudentOutcome object and storing what was found in there, and then modifying that Object I created, but it seems like when a new object is created despite being set equal to the one found in the database, using the =, operator, the changes made to the new StudentOutcome object does NOT apply to what it was initially set equal to.

How can I make this more efficient? I know there is better way.

Try either

StudentOutcome toUpdate = studentOutcomeService.getAll().get(
    getStudentOutcomeByID(selectedStudentOutcome.getId()));
toUpdate.setDescription(description);
toUpdate.setSequenceNumber(Integer.valueOf(sequenceNumber));
toUpdate.setShortName(shortName);
toUpdate.setIdentifier(identifier);

or even just

StudentOutcome toUpdate = getStudentOutcomeByID(selectedStudentOutcome.getId());
toUpdate.setDescription(description);
toUpdate.setSequenceNumber(Integer.valueOf(sequenceNumber));
toUpdate.setShortName(shortName);
toUpdate.setIdentifier(identifier);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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