Home > OS >  How to update details of farmer in spring boot through the UserServiceImpl class?
How to update details of farmer in spring boot through the UserServiceImpl class?

Time:10-10

How to update details of farmer through the userServiceImpl class? We cant use the farmerServiceImpl as we have to pass the username as the input not the id and username is not present in farmerClass.

User class:

public class User {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;
        @NotBlank(message = "Username is mandatory")
        @Size(max = 20)
        private String username;
        @NotBlank(message = "Password is mandatory")
        @Size(max = 50)
        private String password;
        
        @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST})
        @JoinTable(name = "user_roles",
            joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
        private Set<Role> roles;
        
        @OneToOne(fetch = FetchType.LAZY,
                cascade =  {CascadeType.REMOVE,CascadeType.PERSIST},
                mappedBy = "user")
        @JsonIgnore
        private Farmer farmer;
    }

Farmer Class:

public class Farmer {
    @Id
    @GeneratedValue
    private int farmerId;
    
    @NotBlank
    @Size(max = 120)
    private String name;    
    @NotBlank
    @Size(max = 120)
    private String address;
    @NotBlank
    @Size(max = 12)
    private String phoneNo;

    // OneToOne unidirectional relationship 
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "id")
    private User user;
    

}

UserServiceImpl.java :

@Override
    public User updateUser(String username, User user) {
<<<How to update details of farmer class through this method>>>
    return userRepository.save(userDetails);

}

CodePudding user response:

  1. Retrieve the User by passing the username as argument.
  2. Then you can make any changes to the farmer class since it is also a member of the User class.
  3. Save the user class, since you are using cascade type persist any changes made to the farmer will be saved in the farmer table as well.

Your method will look something like this:

public User updateUser(String username, Farmer farmer) {
    User user = userRepository.findByUsername(username);

    //If you want to update only a particular field of the Farmer class
    user.getFarmer().setName("someName");

    //Or if you are receiving a farmer object as method parameter then you can use something like this
    Integer id = user.getFarmer().getFarmerId();
    user.setFarmer(farmer);
    user.getFarmer().setFarmerId(id);

    //save the user class
    userRepository.save(user);
}

CodePudding user response:

There are two approaches for this , you can use farmerServiceImpl too for that you will have to use SQL query with join like below

@Qyery(value="select f.* from farmer f join user u on f.farmerid=u.id where u.username=:username", native query=true)
Optional<Farmer> getFarmerByUsername(@Param("username") String username)

You can use the above query to retrieve the farmer who has username as provided like below

public User updateUser(String username, Farmer farmer) {
    Farmer  farmer=farmerRepository.getFarmerByUsername("username");
    farmer.setName("some name");
    farmer.setAddress("address");
    farmer.setPhoneNo("9999999999");
    //Then just save the farmer object
    farmerRepository.save(farmer); 
          
      }

The other approach is by using userRepository as below:

public User updateUser(String username, Farmer farmer) {
    User user = userRepository.findByUsername(username);

Farmer farmer=user.getFarmer();
farmer.setName("some name");
    farmer.setAddress("address");
    farmer.setPhoneNo("9999999999");
    //Then just save the farmer object
    farmerRepository.save(farmer);
}
  • Related