I have a hierarchy of classes as follows
Enterprise (abstract)
^
|_ Vendor
|_ Implementer
|_ Customer
each of the three subclasses have a relation one to many with class
ContactPerson so I have to put three keys in
ContactPerson table which always have 2 of them null
is there a way to eliminate these 3 keys and use only one key using
MappedSuperclass or by any other way ?
here is my entity classes
Code:
/**
* Enterprise.java
*/
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
@MappedSuperclass
public abstract class Enterprise {
private String address;
private String phone;
private String poBox;
private String website;
public void setAddress(String address) {
this.address = address;
}
@Column(name="ADDRESS", length=250)
public String getAddress() {
return address;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Column(name="PHONE", length=100 )
public String getPhone() {
return phone;
}
public void setPoBox(String poBox) {
this.poBox = poBox;
}
@Column(name="POBOX", length=50)
public String getPoBox() {
return poBox;
}
public void setWebsite(String website) {
this.website = website;
}
@Column(name="WEBSITE", length=50)
public String getWebsite() {
return website;
}
}
Vendor.java
Code:
/**
* @(#) Vendor.java
*/
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Entity;
@Entity
@Table(name = "VENDOR")
public class Vendor extends Enterprise implements java.io.Serializable {
private Integer id;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
private String name;
private Set contacts;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "vendor", targetEntity = Contact.class)
public Set getContacts() {
return contacts;
}
public void setContacts(Set contacts) {
this.contacts = contacts;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "NAME", length = 100, nullable = false)
public String getName() {
return name;
}
}
Customer.java
Code:
/**
* @(#) Customer.java
*/
import java.io.Serializable;
import java.util.Set;
import javax.persistence.AssociationOverride;
import javax.persistence.AttributeOverride;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name ="CUSTOMER")
public class Customer extends Enterprise implements Serializable {
private Integer id;
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
private String name;
private Set contacts;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "customer", targetEntity = Contact.class)
public Set getContacts() {
return contacts;
}
public void setContacts(Set contacts) {
this.contacts = contacts;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "NAME", length = 100,nullable=false)
public String getName() {
return name;
}
}
ContactPerson
Code:
/**
* @(#) ContactPerson.java
*/
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name ="CONTACT")
public class ContactPerson implements Serializable {
private Integer id;
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
private String name;
private String phone;
private String email;
private String extension;
private String mobile;
private String fax;
private String title;
private Customer customer;
private Implementor implementor;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CUSOMER_ID")
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "IMPLEMENTOR_ID")
public Implementor getImplementor() {
return implementor;
}
public void setImplementor(Implementor implementor) {
this.implementor = implementor;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "VENDOR_ID")
public Vendor getVendor() {
return vendor;
}
public void setVendor(Vendor vendor) {
this.vendor = vendor;
}
private Vendor vendor;
public void setName(String name) {
this.name = name;
}
@Column(name="NAME", length=100,nullable=false)
public String getName() {
return name;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Column(name="PHONE", length=20)
public String getPhone() {
return phone;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name="EMAIL", length=100)
public String getEmail() {
return email;
}
public void setExtension(String extension) {
this.extension = extension;
}
@Column(name="EXT", length=20)
public String getExtension() {
return extension;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
@Column(name="MOBILE", length=20)
public String getMobile() {
return mobile;
}
public void setFax(String fax) {
this.fax = fax;
}
@Column(name="FAX", length=20)
public String getFax() {
return fax;
}
public void setTitle(String title) {
this.title = title;
}
@Column(name="TITLE", length=100)
public String getTitle() {
return title;
}
}