Issue
Kindly look into my code and please help me out on this as i am currently using h2 database and its saving all the data except the Name and the password fields and it is storing it as null
Entity:-
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Users {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String Name;
private String Password;
private String emailAddress;
private String phoneNumber;
private int age;
private char gender;
private String address;
}
UserController:-
@RestController
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/register")
public Users registerUser(@Valid @RequestBody Users users){
return userService.saveUser(users);
}
}
UserServiceImpl:-
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public Users saveUser(Users userModel) {
return userRepository.save(userModel);
}
}
UserService:-
public interface UserService {
Users saveUser(Users userModel);
}
UserRepository:-
@Repository
public interface UserRepository extends JpaRepository<Users, Long> {
}
it is saving the data in h2 database as this:-
ID NAME PASSWORD ADDRESS AGE EMAIL_ADDRESS GENDER PHONE_NUMBER
1 null null this is my address 10 [email protected] M 123456789
Solution
I believe the error is in the casing. It should be name
not Name
and password
and not Password
for the JPA to match the column.
Answered By - grekier
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)