In the previous example we have seen how to embed an address object in user details object. When the program
is executed, the column names for the address class would be the default names of the member variables. This
works fine when we have one address object. But what if we have two or more address objects? In such case all
the address objects would have the same column names and this leads to confusion. For example we have two address
objects say present address and permanent address. When user enters details for present and permanent address,
there would be two street names, two city names, two state names and the column names would also be same.
To over come this, we can make use of @AttributeOverride annotation. In this annotation we have to give the name
of the member variable which we need to override and also the the column name that we intent to give. The below
example has two address variables: presentAddress and permanentAddress. We change the column names of presentAddress
and leave the column names of the permanantAddress take the column names of the address object.
package com.ram.dao; import javax.persistence.Column; import javax.persistence.Embeddable; @Embeddable public class Address { @Column(name="Street_Name") private String street; @Column(name="City_Name") private String city; @Column(name="State_Name") private String state; 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; } }
package com.ram.dao; import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class UserDetails { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int userId; private String userName; @Embedded @AttributeOverrides({ @AttributeOverride (name="street", column=@Column(name="Present_Street_Name")), @AttributeOverride (name="city", column=@Column(name="Present_City_Name")), @AttributeOverride (name="state", column=@Column(name="Present_State_Name")) }) private Address presentAddress; @Embedded private Address permanentAddress; public Address getPresentAddress() { return presentAddress; } public void setPresentAddress(Address presentAddress) { this.presentAddress = presentAddress; } public Address getPermanentAddress() { return permanentAddress; } public void setPermanentAddress(Address permanentAddress) { this.permanentAddress = permanentAddress; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
No comments:
Post a Comment