Java Tutorial/JPA/Attribute Overrides

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

Attribute Overrides For Embeddable ID

File: Employee.java



import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Transient;
@Entity
public class Employee implements java.io.Serializable {
  private String firstName;
  private EmployeePK pk;
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  @EmbeddedId
  @AttributeOverrides( {
      @AttributeOverride(name = "lastName", column = @Column(name = "LAST_NAME")),
      @AttributeOverride(name = "ssn", column = @Column(name = "SSN")) })
  public EmployeePK getPk() {
    return pk;
  }
  public void setPk(EmployeePK pk) {
    this.pk = pk;
  }
  @Transient
  public String getLastName() {
    return pk.getLastName();
  }
  @Transient
  public long getSsn() {
    return pk.getSsn();
  }
}





Attribute Overrides For Secondary Table

File: Main.java



import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class Main {
  static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
  static EntityManager em = emf.createEntityManager();
  public static void main(String[] a) throws Exception {
    em.getTransaction().begin();

    Student student = new Student();
    student.setId(1);
    student.setName("Tom");
    Address addr = new Address();
    addr.setStreet("street 1");
    addr.setCity("LA");
    addr.setState("CA");
    addr.setZip("asdfasdf");
    student.setAddress(addr);
    em.persist(student);

    em.getTransaction().rumit();
    em.close();
    emf.close();
    Helper.checkData();
  }
}





Subclass With Attribute Overrides

File: Employee.java



import java.util.Date;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Employee {
  @Id
  private int id;
  private String name;
  @Temporal(TemporalType.DATE)
  private Date startDate;
  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 Date getStartDate() {
    return startDate;
  }
  public void setStartDate(Date startDate) {
    this.startDate = startDate;
  }
  public String toString() {
    return "Employee id: " + getId() + " name: " + getName();
  }
}
@Entity
@Table(name="CONTRACT_EMP")
@AttributeOverrides({
    @AttributeOverride(name="name", column=@Column(name="FULLNAME")),
    @AttributeOverride(name="startDate", column=@Column(name="SDATE"))
})
class ContractEmployee extends Employee {
    @Column(name="D_RATE")
    private int dailyRate;
    private int term;
    
    public int getDailyRate() {
        return dailyRate;
    }
    
    public void setDailyRate(int dailyRate) {
        this.dailyRate = dailyRate;
    }
    
    public int getTerm() {
        return term;
    }
    
    public void setTerm(int term) {
        this.term = term;
    }
    public String toString() {
        return "ContractEmployee id: " + getId() + " name: " + getName();
    }
}





Using Attribute Overrides

File: Main.java



import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class Main {
  static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
  static EntityManager em = emf.createEntityManager();
  public static void main(String[] a) throws Exception {
    em.getTransaction().begin();
    Student student = new Student();
    student.setId(1);
    student.setName("name");
    Address addr = new Address();
    addr.setStreet("street1");
    addr.setCity("city1");
    addr.setState("state1");
    addr.setZip("asdfasdf");
    student.setAddress(addr);
    em.persist(student);
    
    Company c = new Company();
    c.setName("III");
    addr = new Address();
    addr.setStreet("street2");
    addr.setCity("city2");
    addr.setState("state2");
    addr.setZip("asdfasdf");
    c.setAddress(addr);
    em.persist(c);
    
    
    em.getTransaction().rumit();
    em.close();
    emf.close();
    Helper.checkData();
  }
}