Java Tutorial/JPA/One To One Mapping — различия между версиями

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

Текущая версия на 05:18, 1 июня 2010

Bidirectional One To One Mapping

File: Main.java



import java.util.List;
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.setName("Joe");
    Address address = new Address();
    address.setCity("city");
    address.setStudent(student);
  //em.persist(address);
    
    student.setAddress(address);
      
    em.persist(student);
    em.flush();
    em.getTransaction().rumit();
    
    
    
    Query query = em.createQuery("SELECT e FROM Address e");
    List<Address> list = (List<Address>) query.getResultList();
    System.out.println(list.get(0).getStudent());
    
    
    em.close();
    emf.close();
    
    Helper.checkData();
  }
}





One To One Mapping Based On Table Based ID

File: Address.java



import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.TableGenerator;
@Entity
public class Address {
  @TableGenerator(name = "Address_Gen", table = "ID_GEN", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL", pkColumnValue = "Addr_Gen", initialValue = 10000, allocationSize = 100)
  @Id
  @GeneratedValue(strategy = GenerationType.TABLE, generator = "Address_Gen")
  private int id;
  private String street;
  private String city;
  private String state;
  private String zip;
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  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 id: " + getId() + ", street: " + getStreet() + ", city: " + getCity()
        + ", state: " + getState() + ", zip: " + getZip();
  }
}





One To One Mapping Bidirectional

File: Student.java



import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Student {
  @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int id;
  private String name;
  @OneToOne (cascade=CascadeType.ALL)
  @JoinColumn(name="DEPT_ID", unique= true, nullable=true, insertable=true, updatable=true)
  private Department department;
  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 Department getDepartment() {
    return department;
  }
  public void setDepartment(Department department) {
    this.department = department;
  }
  public String toString() {
    return "\n\nID:" + id + "\nName:" + name + "\n\n" + department;
  }
}





One To One Mapping Mapped By Field

File: Department.java



import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Department {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;
    @OneToOne(mappedBy="department")
    private Student student;
    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String deptName) {
        this.name = deptName;
    }
    public Student getStudent() {
      return student;
    }
    public void setStudent(Student student) {
      this.student = student;
    }
    public String toString() {
        return "Department id: " + getId() + 
               ", name: " + getName();
    }
}





One To One Mapping Primary Key As Join Column

File: Department.java



import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
@Entity
public class Department {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;
    @OneToOne
    @PrimaryKeyJoinColumn//a primary key column that is used as a foreign key to join to another table
    private Student student;
    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String deptName) {
        this.name = deptName;
    }
    public Student getStudent() {
      return student;
    }
    public void setStudent(Student student) {
      this.student = student;
    }
    public String toString() {
        return "Department id: " + getId() + 
               ", name: " + getName();
    }
}





One To One Mapping With Cascading.All

File: Address.java



import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Address {
  @Id
  private int id;
  private String street;
  private String city;
  private String state;
  private String zip;
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  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 id: " + getId() + ", street: " + getStreet() + ", city: " + getCity()
        + ", state: " + getState() + ", zip: " + getZip();
  }
}





One To One Mapping With Fetch Type Lazy

File: Address.java



import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Address {
  @Id
  private int id;
  private String street;
  private String city;
  private String state;
  private String zip;
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  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 id: " + getId() + ", street: " + getStreet() + ", city: " + getCity()
        + ", state: " + getState() + ", zip: " + getZip();
  }
}





One To One Unidirectional

File: Department.java



import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Dept")
public class Department {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String deptName) {
        this.name = deptName;
    }
    public String toString() {
        return "Department id: " + getId() + 
               ", name: " + getName();
    }
}





Retrieve One To One Mapped Entity

File: Student.java



import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Student {
  @Id
  private int id;
  private String name;
  @OneToOne
  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 "\n\nID:" + id + "\nName:" + name + "\n\n";
  }
}