Home > Java > Should I create an update function for each property, or should I allow partial updates?

Should I create an update function for each property, or should I allow partial updates?

WBOY
Release: 2024-02-05 21:33:07
forward
1455 people have browsed it
Question content

How should the update process be implemented? Is it recommended to do a forced update of all properties (first example) or should I add a null check condition to allow partial updates (second example)?

First example

@transactional
    @override
    public teamdto updateteambyid(long id, teamdto teamdto) throws apiexception {
        optional<team> teamoptional = teamrepository.findbyid(id);

        if (teamoptional.ispresent()) {
            team existingteam = teamoptional.get();

            // if objects id are sent in json, set attributes via id.
            if (teamdto != null) {

                if(teamdto.getcity() != null){
                    if (teamdto.getcity().getid() != null) {
                        citydto citydto = cityserviceimp.getcitybyid(teamdto.getcity().getid());
                        existingteam.setcity(citymapper.citydtotocity(citydto));
                    } else if(teamdto.getcity().getname() != null){
                        existingteam.setcity(citymapper.citydtotocity(cityserviceimp.createcity(teamdto.getcity())));
                    }
                }
                if(teamdto.getstadium() != null){
                    if (teamdto.getstadium().getid() != null) {
                        stadiumdto stadiumdto = stadiumserviceimp.getstadiumbyid(teamdto.getstadium().getid());
                        existingteam.setstadium(stadiummapper.stadiumdtotostadium(stadiumdto));
                    } else if(teamdto.getstadium().getname() != null){
                        existingteam.setstadium(stadiummapper.stadiumdtotostadium(stadiumserviceimp.createstadium(teamdto.getstadium())));
                    }
                }
                if(teamdto.getdivision() != null){
                    if (teamdto.getdivision().getid() != null) {
                        divisiondto divisiondto = divisionserviceimp.getdivisionbyid(teamdto.getdivision().getid());
                        existingteam.setdivision(divisionmapper.divisiondtotodivision(divisiondto));
                    } else if(teamdto.getdivision().getname() != null){
                        existingteam.setdivision(divisionmapper.divisiondtotodivision(divisionserviceimp.createdivision(teamdto.getdivision())));
                    }

                }

                return teammapper.teamtoteamdto(teamrepository.save(existingteam));
            }
        }
        throw new teamnotfoundexception(id);
    }
Copy after login

Second example

@Transactional
    @Override
    public TeamDTO updateTeamById(Long id, TeamDTO teamDTO) throws ApiException {
        Team team = teamMapper.teamDTOToTeam(teamDTO);
        Optional<Team> teamOptional = teamRepository.findById(id);

        if (teamOptional.isPresent()) {
            Team existingTeam = teamOptional.get();
            
            existingTeam.setName(team.getName());
            existingTeam.setDivision(team.getDivision());
            existingTeam.setCity(team.getCity());
            existingTeam.setStadium(team.getStadium());
            existingTeam.setHomeMatches(team.getHomeMatches());
            existingTeam.setAwayMatches(team.getAwayMatches());
            
            return teamMapper.teamToTeamDTO(teamRepository.save(existingTeam));
            }
        throw new TeamNotFoundException(id);
    }
Copy after login

This is a crud personal project. I have the same question about the post method.


Correct answer


With correctly defined classes and relationships you can simply use the Second approach which is clean and easy to understand .

By proper relationship definition, I mean the mapping between classes (whether they are one-to-one or one-to-many etc.) and their levels Connection type . In your case you can use CascadeType.ALL which will propagate all database operations done on the parent class to the child class. So when you save the parent object, your child objects are also saved. Even if your subclass doesn't exist in the database, it will create one for you.

Now, your first approach also works in a few cases. For example, suppose you are sending a request containing some city data, where for a given ID, the name provided is different from the name in the database. So for your approach you don't update the different names in the database but get the actual data from the database and set it. But if you follow the second method, it will overwrite the existing data of city name.
To overcome the problem in the second approach, you can change the cascade to CascadeType.PERSIST, which will only propagate save operations to subclasses, not update or delete operations.

The above is the detailed content of Should I create an update function for each property, or should I allow partial updates?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template