Java Tutorial/JPA/Secondary Table

Материал из Java эксперт
Перейти к: навигация, поиск

Map One Entity To More Than One Table

File: Student.java



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
@Entity 
@Table(name="Student") 
@SecondaryTable(name="Student_ADDRESS",
    pkJoinColumns=@PrimaryKeyJoinColumn(name="Student_ID"))
public class Student {
    @Id private int id;
    private String name;
    @Column(table="Student_ADDRESS")
    private String street;
    @Column(table="Student_ADDRESS")
    private String city;
    @Column(table="Student_ADDRESS")
    private String state;
    @Column(name="ZIP_CODE", table="Student_ADDRESS")
    private String zip;

    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 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 getStreet() {
        return street;
    }
    public void setStreet(String street) {
        this.street = street;
    }
    public String getZip() {
        return zip;
    }
    public void setZip(String zip) {
        this.zip = zip;
    }
    public String toString() {
        return "Student id: " + getId() + " name: " + getName();
    }
}





Map One Field To Secondary Table

File: Employee.java



import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.SecondaryTables;
@Entity 
@SecondaryTables({
  @SecondaryTable(name="EMP_LOB", pkJoinColumns={
      @PrimaryKeyJoinColumn(name="ID", referencedColumnName="EMP_ID")})
})
public class Employee {
    @Id
    @Column(name="EMP_ID")
    private int id;
    private String name;
    @Basic(fetch=FetchType.LAZY)
    @Lob
    @Column(table="EMP_LOB")
    private byte[] photo;
    
    @Basic(fetch=FetchType.LAZY)
    @Lob
    @Column(table="EMP_LOB")
    private char[] comments;

    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 char[] getComments() {
        return comments;
    }
    public void setComments(char[] comments) {
        this.ruments = comments;
    }
    public byte[] getPhoto() {
        return photo;
    }
    public void setPhoto(byte[] photo) {
        this.photo = photo;
    }
    public String toString() {
        return "Employee id: " + getId() + " name: " + getName();
    }
}





One Entity With Two Secondary Tables

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();
    }
}





Secondary Table With Compound Key

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();
    }
}





Secondary Table With Many To One Relationship

File: Employee.java



import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.SecondaryTables;
class EmployeeId implements Serializable {
  private String name; 
  private int id; 
  public EmployeeId() {}
  public EmployeeId(String country, int id) {
    this.name = country;
    this.id = id;
  }
  public String getCountry() { return name; }
  public int getId() { return id; }
  public boolean equals(Object o) { 
      return ((o instanceof EmployeeId) && 
              name.equals(((EmployeeId)o).getCountry()) &&
              id == ((EmployeeId) o).getId());
  }
  public int hashCode() { 
      return name.hashCode() + id; 
  }
}
@Entity 
@IdClass(EmployeeId.class)
@SecondaryTables({
  @SecondaryTable(name="ORG_STRUCTURE", pkJoinColumns={
      @PrimaryKeyJoinColumn(name="EMP_Name", referencedColumnName="name"),
      @PrimaryKeyJoinColumn(name="ID", referencedColumnName="ID")})
})
public class Employee {
    @Id
    private int id;
    @Id
    private String name;
    @ManyToOne 
    @JoinColumns({
        @JoinColumn(name="MGR_COUNTRY", referencedColumnName="name",
                    table="ORG_STRUCTURE"),
        @JoinColumn(name="MGR_ID", referencedColumnName="ID",
                    table="ORG_STRUCTURE")
    })
    private Employee manager;

    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 String toString() {
        return "Employee id: " + getId() + " name: " + getName();
    }
}





Secondary Table With Primary Key

File: Student.java



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
@Entity 
@Table(name="Student") 
@SecondaryTable(name="Student_ADDRESS",
    pkJoinColumns=@PrimaryKeyJoinColumn(name="Student_ID"))
public class Student {
    @Id private int id;
    private String name;
    @Column(table="Student_ADDRESS")
    private String street;
    @Column(table="Student_ADDRESS")
    private String city;
    @Column(table="Student_ADDRESS")
    private String state;
    @Column(name="ZIP_CODE", table="Student_ADDRESS")
    private String zip;

    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 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 getStreet() {
        return street;
    }
    public void setStreet(String street) {
        this.street = street;
    }
    public String getZip() {
        return zip;
    }
    public void setZip(String zip) {
        this.zip = zip;
    }
    public String toString() {
        return "Student id: " + getId() + " name: " + getName();
    }
}