I am a beginner in Spring Boot and I have two table which having One to One relationship. The problem is when I am trying to get user record by its user-id I am getting an exception like below.
I pasted all my code. What is the mistake in my code?
Users
@Entity@Table(name = "users")public class User implements Serializable {@Id//@GeneratedValue(strategy = GenerationType.IDENTITY)@GeneratedValue(strategy= GenerationType.AUTO)private Long id;@NotNull@Size(max = 65)@Column(name = "first_name")private String firstName;@Size(max = 65)@Column(name = "last_name")private String lastName;@NotNull@Email@Size(max = 100)@Column(unique = true)private String email;@NotNull@Size(max = 128)private String password;@OneToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL,mappedBy = "user")private UserProfile userProfile;// Hibernate requires a no-arg constructorpublic User() {}public User(String firstName, String lastName, String email, String password) {this.firstName = firstName;this.lastName = lastName;this.email = email;this.password = password;}// Getters and Setters (Omitted for brevity)public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public UserProfile getUserProfile() {return userProfile;}public void setUserProfile(UserProfile userProfile) {this.userProfile = userProfile;}}
UsersProfile
@Entity@Table(name = "user_profiles")public class UserProfile implements Serializable{@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;@Column(name = "phone_number")@Size(max = 15)private String phoneNumber;@Enumerated(EnumType.STRING)@Column(length = 10)private Gender gender;@Temporal(TemporalType.DATE)@Column(name = "dob")private Date dateOfBirth;@Size(max = 100)private String address1;@Size(max = 100)private String address2;@Size(max = 100)private String street;@Size(max = 100)private String city;@Size(max = 100)private String state;@Size(max = 100)private String country;@Column(name = "zip_code")@Size(max = 32)private String zipCode;@OneToOne(fetch = FetchType.LAZY, optional = false)@JoinColumn(name = "user_id", nullable = false)private User user;public UserProfile() {}public UserProfile(String phoneNumber, Gender gender, Date dateOfBirth, String address1, String address2, String street, String city, String state, String country, String zipCode) {this.phoneNumber = phoneNumber;this.gender = gender;this.dateOfBirth = dateOfBirth;this.address1 = address1;this.address2 = address2;this.street = street;this.city = city;this.state = state;this.country = country;this.zipCode = zipCode;}// Getters and Setters (Omitted for brevity)public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getPhoneNumber() {return phoneNumber;}public void setPhoneNumber(String phoneNumber) {this.phoneNumber = phoneNumber;}public Gender getGender() {return gender;}public void setGender(Gender gender) {this.gender = gender;}public Date getDateOfBirth() {return dateOfBirth;}public void setDateOfBirth(Date dateOfBirth) {this.dateOfBirth = dateOfBirth;}public String getAddress1() {return address1;}public void setAddress1(String address1) {this.address1 = address1;}public String getAddress2() {return address2;}public void setAddress2(String address2) {this.address2 = address2;}public String getStreet() {return street;}public void setStreet(String street) {this.street = street;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getState() {return state;}public void setState(String state) {this.state = state;}public String getCountry() {return country;}public void setCountry(String country) {this.country = country;}public String getZipCode() {return zipCode;}public void setZipCode(String zipCode) {this.zipCode = zipCode;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}}
UserRepository
public interface UserRepository extends JpaRepository<User, Long> {User findById(int id);}
Controller
@PostMapping(value = "/getUser")@ResponseBodypublic User getUSer(@RequestParam int userID) {User user = userRepository.findById(userID);return user;}
Error
java.lang.IllegalArgumentException: Parameter value [6] did not match expected type [java.lang.Long (n/a)]at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:54) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:27) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]at org.hibernate.query.internal.QueryParameterBindingImpl.validate(QueryParameterBindingImpl.java:90) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]at org.hibernate.query.internal.QueryParameterBindingImpl.setBindValue(QueryParameterBindingImpl.java:55) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:493) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:106) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.setParameter(CriteriaQueryTypeQueryAdapter.java:385) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.setParameter(CriteriaQueryTypeQueryAdapter.java:59) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]at org.springframework.data.jpa.repository.query.QueryParameterSetter$NamedOrIndexedQueryParameterSetter.lambda$setParameter$3(QueryParameterSetter.java:111) ~[spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE]at org.springframework.data.jpa.repository.query.QueryParameterSetter$ErrorHandling$1.execute(QueryParameterSetter.java:175) ~[spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE]at org.springframework.data.jpa.repository.query.QueryParameterSetter$NamedOrIndexedQueryParameterSetter.setParameter(QueryParameterSetter.java:111) ~[spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE]at org.springframework.data.jpa.repository.query.ParameterBinder.lambda$bind$0(ParameterBinder.java:79) ~[spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE]at java.util.ArrayList.forEach(Unknown Source) ~[na:1.8.0_191]
Best Answer
Check your Database and Check the datatype of the Id if it's int you can't send long
When it comes to providing a phone number, it is essential to ensure that it matches the expected input. However, in this case, the phone number provided did not meet the required criteria. This situation can occur due to various reasons, such as incorrect formatting or missing digits.
To resolve this issue, it is crucial to double-check the phone number and compare it with the expected input format. Make sure that all the necessary digits are included and that there are no additional characters or spaces. Additionally, consider any specific formatting requirements, such as using parentheses or hyphens to separate the digits.
If the phone number still does not match the expected input after reviewing and correcting any potential errors, it might be necessary to seek further assistance. Contact the relevant service provider or organization to inquire about the specific phone number format they require or any additional steps that need to be followed.
By ensuring that the phone number matches the expected input, you can avoid potential issues and ensure smooth communication. Take the time to carefully input the phone number and verify its accuracy to prevent any inconvenience or delays that may arise from an incorrect phone number format.