JPA Part 4: Attribute Overrides
This is a continuation of Part 1.
Fore eample
This is a very quick blog on @AttributeOverrides. I refactored the Part 1 of this series to add the Address to Department and moved the Contact embeddable attribute to the Person which is a @MappedSuperclass. Now the class relationships are changed as follows.
Fig 1: HR Domain Model
As shown in the above figure 1, It is a challenge to sharing the same Address class among the Asset and Employee (extended from Person) entities because Asset use the TOWN field instead of the SUBURB. It is possible for example,
package au.com.ojitha.blogspot.jpaex1.domain;
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.Id;
import javax.persistence.OneToOne;
import javax.persistence.TableGenerator;
@Entity
public class Asset {
@Id
@TableGenerator(name = "assetNo\_Gen",
table = "ID\_GEN",
pkColumnName = "ID\_NAME",
valueColumnName = "LAST\_VAL",
initialValue = 0,
allocationSize=1)
@GeneratedValue(generator="assetNo\_Gen")
@Column(name="ASSET\_NO")
private int assetNo;
@OneToOne(mappedBy="asset")
private Employee employee;
@Column(name="BUILDING\_NO")
private int building;
@Embedded
@AttributeOverrides(value={
@AttributeOverride(name="suburb", column=@Column(name="TOWN"))
})
private Address address;
public int getAssetNo() {
return assetNo;
}
public void setAssetNo(int assetNo) {
this.assetNo = assetNo;
}
public int getBuilding() {
return building;
}
public void setBuilding(int building) {
this.building = building;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}
As shown in the above listing, Line 32 to 36 shows how to map the Suburb to Town.
You can download the source from the GitHub.
Written with StackEdit.
Comments
Post a Comment
commented your blog