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

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);
    }

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);
    }

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!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),