Get the App
SLTechnology News&Howtos  ›  Database  › 

How to write JavaBean when there are foreign keys in the database

Shulou Source: shulou.com Published: 2022-06-01 07:17:45 09月10日 Update

When writing a mapping between a table and an entity class, once a foreign key appears, it is necessary to find another class through the foreign key, and once the foreign key appears, it must be written as a type.

Private Address address

Person table

Public class Person {private String pid; private String pname; private int age; private Address address; public Address getAddress () {return address;} public void setAddress (Address address) {this.address = address;} public String getPid () {return pid;} public void setPid (String pid) {this.pid = pid;} public String getPname () {return pname } public void setPname (String pname) {this.pname = pname;} public int getAge () {return age;} public void setAge (int age) {this.age = age;} / / overwrite the original toString method @ Override public String toString () {return "Person [pid=" + pid + ", pname=" + pname + ", age=" + age + "]";}}

Public class Address {private String aid; / / key private String province; / Provincial private String city; / / City private String district; / / District private String street; / / Street public String getAid () {return aid;} public void setAid (String aid) {this.aid = aid;} public String getProvince () {return province } public void setProvince (String province) {this.province = province;} public String getCity () {return city;} public void setCity (String city) {this.city = city;} public String getDistrict () {return district;} public void setDistrict (String district) {this.district = district;} public String getStreet () {return street } public void setStreet (String street) {this.street = street;}}

Tags: Between entities methods times types streets writing data databases Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft NVidia OPPO Reno MySQL Xiaomi