1 / 27

Relation Mapping

Relation Mapping. EJB 3.0 コース 第 9 回. ここでの目標. 複数のテーブルを組み合わせてデータにアクセスする「 Relation Mapping 」について理解する. @OneToOne Annotation. 一対一の関係の例:自宅は一つ. EJB_EMPLOYEE  社員テーブル. EJB_ADDRESS  住所テーブル. EJB_EMPLOYEE  社員テーブル. @Entity @Table(name="EJB_EMPLOYEE")

zamora
Download Presentation

Relation Mapping

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Relation Mapping EJB 3.0 コース 第9回

  2. ここでの目標 • 複数のテーブルを組み合わせてデータにアクセスする「Relation Mapping」について理解する

  3. @OneToOne Annotation

  4. 一対一の関係の例:自宅は一つ EJB_EMPLOYEE 社員テーブル EJB_ADDRESS 住所テーブル

  5. EJB_EMPLOYEE 社員テーブル @Entity @Table(name="EJB_EMPLOYEE") public class Employee implements Serializable {…… @Id @Column(name="EMP_ID", primaryKey=true) public Integer getId() { return id; } …… @Version @Column(name="VERSION") public int getVersion() { return version; } …… @Column(name="L_NAME", length=80) public String getLastName() { return lastName; } …… @Column(name=“F_NAME", length=80) public String geFirstName() { return firstName; } …… @OneToOne(cascade=ALL, fetch=LAZY) @JoinColumn(name="ADDR_ID") public Address getAddress() { return address; } …… }

  6. EJB_ADDRESS 住所テーブル @Entity @Table(name="EJB_ADDRESS") public class Address implements Serializable {….. @Id @Column(name="ADDRESS_ID", primaryKey=true) public Integer getId() { return id; } …. public void setId(Integer id) { this.id = id; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } @Column(name="P_CODE") public String getPostalCode() { return postalCode; }…. public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } } Default名

  7. 社員のアドレスの取得 public Address getAddressForEmployee(Integer employeeId) { Employee employee = (Employee)em .find("Employee", employeeId); return employee.getAddress(); } public void setAddressForEmployee (Address address, Integer employeeId) { em.persist(address); Employee employee = (Employee)em.find("Employee", employeeId); employee.setAddress(address); } public void updateAddress(Address addressExample) { em.find("Address", addressExample.getId()); em.merge(addressExample); }

  8. @ManyToOne Annotation

  9. 多対一の関係の例:複数の社員が一人の上司を持つ多対一の関係の例:複数の社員が一人の上司を持つ EJB_EMPLOYEE 社員テーブル EJB_EMPLOYEE 社員テーブル

  10. EJB_EMPLOYEE 社員テーブル @Entity @Table(name="EJB_EMPLOYEE") public class Employee implements Serializable {…… @Id @Column(name="EMP_ID", primaryKey=true) public Integer getId() { return id; } …… @Version @Column(name="VERSION") public int getVersion() { return version; } …… @Column(name="L_NAME", length=80) public String getLastName() { return lastName; } …… @Column(name=“F_NAME", length=80) public String geFirstName() { return firstName; } …… @OneToOne @JoinColumn(name="ADDR_ID") public Address getAddress() { return address; } …… @ManyToOne(cascade=PERSIST, fetch=LAZY) @JoinColumn(name="MANAGER_ID",referencedColumnName="EMP_ID") public Employee getManager() { return manager; } …… }

  11. 社員の上司の取得 public Employee getManagerForEmployee(Integer employeeId) { Employee employee = (Employee)em.find("Employee", employeeId); Employee manager = employee.getManager(); if (manager != null) { manager.getAddress(); manager.getManager(); } return manager; }

  12. @OneToMany Annotation

  13. 一対多の関係の例:一人が複数の部下を持つ EJB_EMPLOYEE 社員テーブル EJB_EMPLOYEE 社員テーブル

  14. @OneToMany @JoinColumn(name="MANAGER_ID", referencedColumnName="EMP_ID") public Collection<Employee>getManagedEmployees() { return managedEmployees; } 社員の部下(複数)の取得 public Collection getManagedEmployeesForEmployee(Integer employeeId) { Employee employee = (Employee)em.find("Employee", employeeId); Collection employees = employee.getManagedEmployees(); Iterator employeeIterator = employees.iterator(); while (employeeIterator.hasNext()) { Employee mEmployee = (Employee)employeeIterator.next(); mEmployee.getAddress(); mEmployee.getManager(); } return employees; }

  15. 一対多の関係の例:一人が複数の電話を持つ EJB_EMPLOYEE 社員テーブル EJB_PHONENUMBER 電話番号テーブル

  16. EJB_PHONENUMBER 電話番号テーブル @Entity @Table(name="EJB_PHONENUMBER") public class PhoneNumber implements Serializable { @Id @Column(name="ID", primaryKey=true) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name="NUMB") public String getNumber() { return number; } public void setNumber(String number) { this.number = numbe} public String getType() { return type; } public void setType(String type) { this.type = type; } @Column(name="AREA_CODE") public String getAreaCode() { return areaCode; } public void setAreaCode(String areaCode) { this.areaCode = areaCode; } @ManyToOne(fetch=LAZY) @JoinColumn(name="OWNER_ID", referencedColumnName="EMP_ID") public Employee getOwner() { return owner; } public void setOwner(Employee owner) { this.owner = owner; } }

  17. EJB_EMPLOYEE 社員テーブル @Entity @Table(name="EJB_EMPLOYEE") public class Employee implements Serializable {…… @Id @Column(name="EMP_ID", primaryKey=true) public Integer getId() { return id; } …… @Version @Column(name="VERSION") public int getVersion() { return version; } …… } …… @OneToOne @JoinColumn(name="ADDR_ID") public Address getAddress() { return address; } …… @ManyToOne @JoinColumn(name="MANAGER_ID",referencedColumnName="EMP_ID") public Employee getManager() { return manager; } @OneToMany(cascade=ALL) @JoinColumn(name="OWNER_ID", referencedColumnName="EMP_ID") public Collection<PhoneNumber> getPhoneNumbers() { return phoneNumbers; } }

  18. 社員の電話番号(複数)の取得 public Collection getPhoneNumbersForEmployee(Integer employeeId) { Employee employee = (Employee)em.find("Employee", employeeId); Collection phoneNumbers = employee.getPhoneNumbers(); Iterator pIterator = phoneNumbers.iterator(); while (pIterator.hasNext()) { PhoneNumber pNumber = (PhoneNumber)pIterator.next(); pNumber.getOwner(); } return phoneNumbers; }

  19. @ManyToMany Annotation

  20. EJB_PROJECT プロジェクトテーブル @Entity @Table(name="EJB_PROJECT") public class Project implements Serializable { @Id @Column(name="PROJ_ID", primaryKey=true) public Integer getId() { return projId; } public void setId(Integer id) { this.projId = id; } @Version @Column(name="VERSION") public int getVersion() { return version; } protected void setVersion(int version) { this.version = version; } @Column(name="PROJ_NAME") public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="DESCRIP") public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @OneToOne(fetch=LAZY) @JoinColumn(name="LEADER_ID") public Employee getTeamLeader() { return teamLeader; } public void setTeamLeader(Employee teamLeader) {…. } }

  21. 多対多の関係の例:社員のプロジェクトへの所属多対多の関係の例:社員のプロジェクトへの所属 EJB_PROJ_EMP Association Table

  22. EJB_EMPLOYEE 社員テーブル EJB_PROJECT プロジェクトテーブル

  23. 多対多の関係の表現: Association Table EJB_EMPLOYEE 社員テーブル joinColumns=@JoinColumn(name="EMP_ID", referencedColumnName="EMP_ID"), Association Table EJB_PROJ_EMP inverseJoinColumns=@JoinColumn(name="PROJ_ID", referencedColumnName="PROJ_ID") EJB_PROJECT プロジェクトテーブル

  24. @ManyToMany(cascade=PERSIST) @AssociationTable(table=@Table(name="EJB_PROJ_EMP"), joinColumns=@JoinColumn(name="EMP_ID", referencedColumnName="EMP_ID"), inverseJoinColumns=@JoinColumn(name="PROJ_ID", referencedColumnName="PROJ_ID") ) public Collection<Project> getProjects() { return projects; } public void setProjects(Collection<Project> projects) { this.projects = projects; } プロジェクトの生成 public void createProject(String name, String description) { Project project= new Project(); project.setName(name); project.setDescription(description); em.persist(project); }

  25. プロジェクトへの社員の追加 public void addEmployeeToProject( Integer employeeId, Integer projectId) { Employee employee = (Employee)em.find("Employee", employeeId); Project project = (Project)em.find("Project", projectId); project.addTeamMember(employee); employee.addProject(project); }

  26. プロジェクト所属の社員の獲得 public Collection getEmployeesForProject(Integer projectId){ Project project = (Project)em.find(“Project”, projectId); Iterator employeeIterator = project.getTeamMembers().iterator(); while (employeeIterator.hasNext()) { Employee employee = (Employee)employeeIterator.next(); employee.getAddress(); employee.getManager(); } return project.getTeamMembers(); }

  27. 社員登録の削除 public void removeEmployee(Integer employeeId) { Employee employee = (Employee) em.find("Employee", employeeId); Collection<Project> projects = employee.getProjects(); for (Project project : projects) { project.removeTeamMember(employee); } Collection<Employee> employees = employee.getManagedEmployees(); for (Employee emp : employees) { emp.setManager(null); } Employee manager = employee.getManager(); if (manager != null) { manager.removeManagedEmployee(employee); } em.remove(employee); }

More Related