Grouping columns within entity

3918 views jpa
3

I have a table with 2 columns for phone number(area code and number). When I define entity, I can do it with 2 different variable to match column names. However, when i use it in jparepository, I want to search with a phone number (which will be a single string of area code and number).

Is ther a way to group area code and number while defining the entity to have a single variable to hold combined data of 2 columns?

@Entity public class Person{ private String areaCode; private String number; }

answered question

1 Answer

11

Yes, you can define one more String field like below.

private String phoneNumber;

and create a setter method like below.

public void setPhoneNumber(String phoneNumber) {
   this.phoneNumber = this.areaCode+""+this.number; //concatinating the areaCode and number
}

public void getPhoneNumber() {
   return this.phoneNumber;
}

posted this

Have an answer?

JD

Please login first before posting an answer.