Home > Mobile >  Hibernate One to Many Annotation between user Entity and Address Entity, in the Address table I don&
Hibernate One to Many Annotation between user Entity and Address Entity, in the Address table I don&

Time:09-30

I have create the user Entity which has a list of Addresses (@OneToMany annotation in user entity) and (@ManyToOne annotation in the Address Entity)

the user and address are well created but the I didn't get the association by the user id in the data base.

enter image description here

in the addresses Table I have the column users_id null
enter image description here

this my Request from insomnia:
enter image description here

User Entity:

@OneToMany(mappedBy = "userEntity" , cascade=CascadeType.ALL)
private List<AdressesEntity> adresses ;

Address Entity

@ManyToOne
@JoinColumn(name = "users_id")
private UserEntity userEntity;

My Controoler :

@PostMapping(
        consumes = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE},
        produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE}
        )
public ResponseEntity<UserResponse> CreatUser(@RequestBody UserRequest userRequest) 
        throws Exception {
            if(userRequest.getFirstName().isEmpty()) {
                throw new UserException(ErrorMessages.MISSING_REQUIRED_FIELD.getErrorMessage());
            }
            
            //UserDto userDto = new UserDto();
            //BeanUtils.copyProperties(userRequest, userDto);
            ModelMapper modelMapper = new ModelMapper();
            UserDto userDto = modelMapper.map(userRequest, UserDto.class);
            
            UserDto createUser = userService.CreateUser(userDto);
            
            UserResponse userResponse = modelMapper.map(createUser, UserResponse.class);
    
            return   new ResponseEntity<UserResponse>(userResponse,HttpStatus.CREATED);
}

Service Implementation of CreateUser(UserDto userDto) :

@Override
public UserDto CreateUser(UserDto userDto) {
    // check exiting user
    UserEntity checkExisting_User = userRepository.findByEmail(userDto.getEmail());
    
    if (checkExisting_User != null)
        throw new RuntimeException("user already exist !");

    for (int i = 0; i < userDto.getAdresses().size(); i  ) {

        AdresseDto address = userDto.getAdresses().get(i);
        address.setUserDto(userDto);
        address.setAdesseId(util.generatedValue(30));
        userDto.getAdresses().set(i, address);
    }
    
    userDto.getContact().setUser(userDto);
    userDto.getContact().setContactId(util.generatedValue(30));

    ModelMapper modelMapper = new ModelMapper();
    UserEntity userEntity = modelMapper.map(userDto, UserEntity.class);

    userEntity.setEcryptyPassword(bCryptPasswordEncoder.encode(userDto.getPassword()));
    userEntity.setUserId(util.generatedValue(32));

    UserEntity newUser = userRepository.save(userEntity);

    UserDto userDto1 = modelMapper.map(newUser, UserDto.class);
    return userDto1;

}

How can I do this?

CodePudding user response:

Try replace

@JoinColumn(name = "users_id") @JoinColumn(name="users_id", referencedColumnName="id or user_id is depends what columns you can use")

Problem is you wrong create relationship on entity.

Or use JoinColumn(name = "user_id")

I think is be helpfull.

  • Related