日期:2014-05-20 浏览次数:20723 次
File: Address.java import javax.persistence.Column; import javax.persistence.Embeddable; @Embeddable public class Address { private String street; private String city; private String state; @Column(name="ZIP_CODE") private String zip; public String getStreet() { return street; } public void setStreet(String address) { this.street = address; } 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; } public String getZip() { return zip; } public void setZip(String zip) { this.zip = zip; } public String toString() { return "Address street: " + getStreet() + ", city: " + getCity() + ", state: " + getState() + ", zip: " + getZip(); } } File: Professor.java import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.SecondaryTable; import javax.persistence.Table; @Entity @Table(name="EMP") @SecondaryTable(name="EMP_ADDRESS", pkJoinColumns=@PrimaryKeyJoinColumn(name="EMP_ID")) public class Professor { @Id private int id; private String name; @Embedded @AttributeOverrides({ @AttributeOverride(name="street", column=@Column(table="EMP_ADDRESS")), @AttributeOverride(name="city", column=@Column(name="CITY", table="EMP_ADDRESS")), @AttributeOverride(name="state", column=@Column(name="STATE", table="EMP_ADDRESS")), @AttributeOverride(name="zip", column=@Column(name="ZIP_CODE", table="EMP_ADDRESS")) }) private Address address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String toString() { return "Professor id: " + getId() + " name: " + getName() + " with " + getAddress(); } }