java - How to insert correctly data on MySQL db through JPA when tables are linked with OneToOne relationship -
i have doubt , consequently don't know how proceed correctly. have 3 different tables in figure below. i'm using jpa. here code generated automatically netbeans shows entities. question should persist database? if put user
, have automatically persisted phone numbers , client? i'm pretty confused
here code
usercredential user = new usercredential(); user.setusername(username); user.setpassword(utilbean.hashpassword(password)); user.setemail(email); client client = new client(); client.setusercredential(user); client.setname(name); client.setsurname(surname); client.setaddress(address); client.setcity(city); client.setzipcode(zipcode); client.setcountry(country); client.setfidelitypoints(0); clientphonenumbers phonenumber = new clientphonenumbers(); phonenumber.setusername(username); if(homenumber != null || !(homenumber.equals(""))) { phonenumber.setcountrycodehome(areacodehomenumber); phonenumber.sethome(homenumber); } phonenumber.setcountrycodemobile(areacodemobilenumber); phonenumber.setmobile(mobilenumber); client.setclientphonenumbers(phonenumber); em.persist(user);
user credential
@entity @table(name = "user_credential") @namedqueries( { @namedquery(name = "usercredential.findall", query = "select u usercredential u") }) public class usercredential implements serializable { private static final long serialversionuid = 1l; @id @basic(optional = false) @notnull @size(min = 1, max = 16) @column(name = "username") private string username; // @pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="invalid email")//if field contains email address consider using annotation enforce field validation @basic(optional = false) @notnull @size(min = 1, max = 255) @column(name = "email") private string email; @basic(optional = false) @notnull @size(min = 1, max = 255) @column(name = "password") private string password; @basic(optional = false) @notnull @size(min = 1, max = 45) @column(name = "groupname") private string groupname; @column(name = "create_time") @temporal(temporaltype.timestamp) private date createtime; @onetoone(cascade = cascadetype.all, mappedby = "usercredential") private client client; public usercredential() { } public usercredential(string username) { this.username = username; } public usercredential(string username, string email, string password, string groupname) { this.username = username; this.email = email; this.password = password; this.groupname = groupname; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } 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 string getgroupname() { return groupname; } public void setgroupname(string groupname) { this.groupname = groupname; } public date getcreatetime() { return createtime; } public void setcreatetime(date createtime) { this.createtime = createtime; } public client getclient() { return client; } public void setclient(client client) { this.client = client; } @override public int hashcode() { int hash = 0; hash += (username != null ? username.hashcode() : 0); return hash; } @override public boolean equals(object object) { // todo: warning - method won't work in case id fields not set if (!(object instanceof usercredential)) { return false; } usercredential other = (usercredential) object; if ((this.username == null && other.username != null) || (this.username != null && !this.username.equals(other.username))) { return false; } return true; } @override public string tostring() { return "it.volaconoi.entity.usercredential[ username=" + username + " ]"; } }
client
@entity @table(name = "client") @namedqueries( { @namedquery(name = "client.findall", query = "select c client c") }) public class client implements serializable { private static final long serialversionuid = 1l; @id @basic(optional = false) @notnull @size(min = 1, max = 16) @column(name = "username") private string username; @basic(optional = false) @notnull @size(min = 1, max = 45) @column(name = "name") private string name; @basic(optional = false) @notnull @size(min = 1, max = 45) @column(name = "surname") private string surname; @basic(optional = false) @notnull @size(min = 1, max = 45) @column(name = "address") private string address; @basic(optional = false) @notnull @size(min = 1, max = 45) @column(name = "city") private string city; @basic(optional = false) @notnull @size(min = 1, max = 10) @column(name = "zip_code") private string zipcode; @basic(optional = false) @notnull @size(min = 1, max = 45) @column(name = "country") private string country; @basic(optional = false) @notnull @column(name = "fidelity_points") private int fidelitypoints; @onetoone(cascade = cascadetype.all, mappedby = "client") private clientphonenumbers clientphonenumbers; @joincolumn(name = "username", referencedcolumnname = "username", insertable = false, updatable = false) @onetoone(optional = false) private usercredential usercredential; @onetomany(cascade = cascadetype.all, mappedby = "username") private collection<reservation> reservationcollection; public client() { } public client(string username) { this.username = username; } public client(string username, string name, string surname, string address, string city, string zipcode, string country, int fidelitypoints) { this.username = username; this.name = name; this.surname = surname; this.address = address; this.city = city; this.zipcode = zipcode; this.country = country; this.fidelitypoints = fidelitypoints; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getsurname() { return surname; } public void setsurname(string surname) { this.surname = surname; } public string getaddress() { return address; } public void setaddress(string address) { this.address = address; } public string getcity() { return city; } public void setcity(string city) { this.city = city; } public string getzipcode() { return zipcode; } public void setzipcode(string zipcode) { this.zipcode = zipcode; } public string getcountry() { return country; } public void setcountry(string country) { this.country = country; } public int getfidelitypoints() { return fidelitypoints; } public void setfidelitypoints(int fidelitypoints) { this.fidelitypoints = fidelitypoints; } public clientphonenumbers getclientphonenumbers() { return clientphonenumbers; } public void setclientphonenumbers(clientphonenumbers clientphonenumbers) { this.clientphonenumbers = clientphonenumbers; } public usercredential getusercredential() { return usercredential; } public void setusercredential(usercredential usercredential) { this.usercredential = usercredential; } public collection<reservation> getreservationcollection() { return reservationcollection; } public void setreservationcollection(collection<reservation> reservationcollection) { this.reservationcollection = reservationcollection; } @override public int hashcode() { int hash = 0; hash += (username != null ? username.hashcode() : 0); return hash; } @override public boolean equals(object object) { // todo: warning - method won't work in case id fields not set if (!(object instanceof client)) { return false; } client other = (client) object; if ((this.username == null && other.username != null) || (this.username != null && !this.username.equals(other.username))) { return false; } return true; } @override public string tostring() { return "it.volaconoi.entity.client[ username=" + username + " ]"; } }
** phone numbers **
@entity @table(name = "client_phone_numbers") @namedqueries( { @namedquery(name = "clientphonenumbers.findall", query = "select c clientphonenumbers c") }) public class clientphonenumbers implements serializable { private static final long serialversionuid = 1l; @id @basic(optional = false) @notnull @size(min = 1, max = 16) @column(name = "username") private string username; @size(max = 8) @column(name = "country_code_home") private string countrycodehome; @size(max = 45) @column(name = "home") private string home; @basic(optional = false) @notnull @size(min = 1, max = 8) @column(name = "country_code_mobile") private string countrycodemobile; @basic(optional = false) @notnull @size(min = 1, max = 45) @column(name = "mobile") private string mobile; @joincolumn(name = "username", referencedcolumnname = "username", insertable = false, updatable = false) @onetoone(optional = false) private client client; public clientphonenumbers() { } public clientphonenumbers(string username) { this.username = username; } public clientphonenumbers(string username, string countrycodemobile, string mobile) { this.username = username; this.countrycodemobile = countrycodemobile; this.mobile = mobile; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getcountrycodehome() { return countrycodehome; } public void setcountrycodehome(string countrycodehome) { this.countrycodehome = countrycodehome; } public string gethome() { return home; } public void sethome(string home) { this.home = home; } public string getcountrycodemobile() { return countrycodemobile; } public void setcountrycodemobile(string countrycodemobile) { this.countrycodemobile = countrycodemobile; } public string getmobile() { return mobile; } public void setmobile(string mobile) { this.mobile = mobile; } public client getclient() { return client; } public void setclient(client client) { this.client = client; } @override public int hashcode() { int hash = 0; hash += (username != null ? username.hashcode() : 0); return hash; } @override public boolean equals(object object) { // todo: warning - method won't work in case id fields not set if (!(object instanceof clientphonenumbers)) { return false; } clientphonenumbers other = (clientphonenumbers) object; if ((this.username == null && other.username != null) || (this.username != null && !this.username.equals(other.username))) { return false; } return true; } @override public string tostring() { return "it.volaconoi.entity.clientphonenumbers[ username=" + username + " ]"; } }
if want persist client
well, have call user.setclient(client)
before calling persist.
Comments
Post a Comment