From af683e5c336c5df199f6cd30f19cf8b6e674f9e0 Mon Sep 17 00:00:00 2001
From: Martin Goik <goik@hdm-stuttgart.de>
Date: Thu, 2 May 2013 22:28:39 +0200
Subject: [PATCH] many to many relationship

---
 .../main/java/entity/company7/Persist.java    | 42 +++++++++
 .../entity/company7/ReassignProjects.java     | 44 +++++++++
 .../java/entity/company7/hibernate.cfg.xml    | 22 +++++
 .../java/entity/company7/model/Employee.java  | 89 +++++++++++++++++++
 .../java/entity/company7/model/Laptop.java    | 63 +++++++++++++
 .../java/entity/company7/model/Project.java   | 59 ++++++++++++
 6 files changed, 319 insertions(+)
 create mode 100644 ws/eclipse/HibIntro/src/main/java/entity/company7/Persist.java
 create mode 100644 ws/eclipse/HibIntro/src/main/java/entity/company7/ReassignProjects.java
 create mode 100644 ws/eclipse/HibIntro/src/main/java/entity/company7/hibernate.cfg.xml
 create mode 100644 ws/eclipse/HibIntro/src/main/java/entity/company7/model/Employee.java
 create mode 100644 ws/eclipse/HibIntro/src/main/java/entity/company7/model/Laptop.java
 create mode 100644 ws/eclipse/HibIntro/src/main/java/entity/company7/model/Project.java

diff --git a/ws/eclipse/HibIntro/src/main/java/entity/company7/Persist.java b/ws/eclipse/HibIntro/src/main/java/entity/company7/Persist.java
new file mode 100644
index 000000000..29349ce32
--- /dev/null
+++ b/ws/eclipse/HibIntro/src/main/java/entity/company7/Persist.java
@@ -0,0 +1,42 @@
+package entity.company7;
+
+import hibintro.util.HibernateUtil;
+
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+
+import entity.company7.model.Employee;
+import entity.company7.model.Project;
+
+
+
+/**
+ * Defining many-to-many relationship between
+ * {@link Employee} and {@link Project}
+ *
+ */
+public class Persist {
+  public static void main(String[] args) {
+    final Session session = HibernateUtil.createSessionFactory("entity/company7/hibernate.cfg.xml").openSession();
+    {
+      final Transaction transaction = session.beginTransaction();
+      final Employee  martin = new Employee(123, "Martin Goik"),
+          eve = new Employee(140, "Eve Blix"),
+          jim = new Employee(160, "Jim Clarke");
+      
+      session.save(martin);
+      session.save(eve);
+      session.save(jim);
+
+      final Project water = new Project(1, "Water supply")
+      ,health = new Project(2, "Health management");
+ 
+      session.save(water);
+      session.save(health);
+      
+      martin.addProject(water);
+
+      transaction.commit();
+    }
+  }
+}
\ No newline at end of file
diff --git a/ws/eclipse/HibIntro/src/main/java/entity/company7/ReassignProjects.java b/ws/eclipse/HibIntro/src/main/java/entity/company7/ReassignProjects.java
new file mode 100644
index 000000000..dfae7b4f7
--- /dev/null
+++ b/ws/eclipse/HibIntro/src/main/java/entity/company7/ReassignProjects.java
@@ -0,0 +1,44 @@
+package entity.company7;
+
+import hibintro.util.HibernateUtil;
+
+import org.hibernate.Criteria;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.criterion.Restrictions;
+
+import entity.company7.model.Employee;
+import entity.company7.model.Project;
+
+public class ReassignProjects {
+   
+  public static void main(String[] args) {
+     
+     
+     
+    final Session session = HibernateUtil.createSessionFactory("entity/company7/hibernate.cfg.xml").openSession();
+    {
+      final Transaction transaction = session.beginTransaction();
+      
+      final Employee
+         martin = (Employee) session.createCriteria(Employee.class).add(
+                     Restrictions.eq("staffNumber", 123)).list().get(0)
+           ,eve = (Employee) session.createCriteria(Employee.class).add(
+                     Restrictions.eq("staffNumber", 140)). list().get(0)
+           ,jim = (Employee) session.createCriteria(Employee.class).add(
+                     Restrictions.eq("staffNumber", 160)). list().get(0);
+      
+      final Criteria searchProject = session.createCriteria(Project.class);
+      
+      final Project water = (Project) searchProject.add(Restrictions.eq("no", 1)).list().get(0);
+      martin.removeProject(water);
+      
+      eve.addProject(water);
+      jim.addProject(water);
+      session.save(martin);
+      session.save(eve);
+      session.save(jim);
+      transaction.commit();
+    }
+  }
+}
\ No newline at end of file
diff --git a/ws/eclipse/HibIntro/src/main/java/entity/company7/hibernate.cfg.xml b/ws/eclipse/HibIntro/src/main/java/entity/company7/hibernate.cfg.xml
new file mode 100644
index 000000000..0859dc3b4
--- /dev/null
+++ b/ws/eclipse/HibIntro/src/main/java/entity/company7/hibernate.cfg.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!DOCTYPE hibernate-configuration PUBLIC
+	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
+	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
+
+<hibernate-configuration>
+ <session-factory >
+  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
+  <property name="hibernate.connection.password">XYZ</property>
+  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hdm</property>
+  <property name="hibernate.connection.username">hdmuser</property>
+  <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
+  <property name="hibernate.show_sql">true</property>
+  <property name="hibernate.format_sql">true</property>
+  <property name="hibernate.hbm2ddl.auto">update</property>
+  
+  <mapping class="entity.company7.model.Laptop"/>
+  <mapping class="entity.company7.model.Employee"/>
+  <mapping class="entity.company7.model.Project"/>
+ </session-factory>
+</hibernate-configuration>
diff --git a/ws/eclipse/HibIntro/src/main/java/entity/company7/model/Employee.java b/ws/eclipse/HibIntro/src/main/java/entity/company7/model/Employee.java
new file mode 100644
index 000000000..2390a22c4
--- /dev/null
+++ b/ws/eclipse/HibIntro/src/main/java/entity/company7/model/Employee.java
@@ -0,0 +1,89 @@
+package entity.company7.model;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.ManyToMany;
+import javax.persistence.OneToOne;
+import javax.persistence.Table;
+import javax.persistence.UniqueConstraint;
+
+
+/**
+ * 
+ *         {@link Employee} instances related to {@link Department}
+ * 
+ */
+@Entity
+@Table(uniqueConstraints={@UniqueConstraint(columnNames={"staffNumber"})})
+public class Employee {
+
+  @Id
+  @GeneratedValue
+  public Long getId() {return id;}
+  protected void setId(Long id) {this.id = id;}
+  private Long id;
+
+  public int getStaffNumber() { return staffNumber;}
+  public void setStaffNumber(int staffNumber) { this.staffNumber = staffNumber;}
+  int staffNumber = Integer.MIN_VALUE;
+
+  public String getCname() { return cname;}
+  public void setCname(String cname) { this.cname = cname;}
+  private String cname; // Common name
+
+  /**
+   * One {@link Laptop} per {@link Employee}.
+   * 
+   * @return {@link Laptop}  "owned" by {@link Employee}.
+   */
+  @OneToOne(cascade={CascadeType.ALL})
+  public Laptop getLaptop() { return laptop;}
+  
+  public void setLaptop(final Laptop laptop) {
+    if (getLaptop() == laptop) { return;}
+    if (null != getLaptop()) { getLaptop().setOwnerInternal(null);}
+    setLaptopInternal(laptop);
+    if (null != getLaptop()) {getLaptop().setOwnerInternal(this);}
+  }
+  
+  protected void setLaptopInternal(final Laptop laptop) { this.laptop = laptop;}
+  private Laptop laptop;
+
+  @ManyToMany
+  public Set<Project> getProjects() { return projects;}
+  protected void setProjects(Set<Project> projects) { this.projects = projects;}
+  public void addProject(final Project project) {
+     getProjects().add(project);
+     project.getCollaborators().add(this);
+  }
+  public void removeProject(final Project project) {
+     getProjects().remove(project);
+     project.getCollaborators().remove(this);
+  }
+  
+  private Set<Project> projects = new HashSet<Project>();
+  
+  protected Employee(){}
+
+  public Employee(final int staffNumber, final String cname) {
+    setStaffNumber(staffNumber);
+    setCname(cname);
+  }
+  @Override
+  public boolean equals(Object other) {
+    if (this == other) {
+      return true;
+    } else if (other instanceof Employee) {
+      return getStaffNumber() == ((Employee) other).getStaffNumber();
+    } else {
+      return false;
+    }
+  }
+  @Override
+  public int hashCode() { return getStaffNumber();}
+}
\ No newline at end of file
diff --git a/ws/eclipse/HibIntro/src/main/java/entity/company7/model/Laptop.java b/ws/eclipse/HibIntro/src/main/java/entity/company7/model/Laptop.java
new file mode 100644
index 000000000..fd7d7f89f
--- /dev/null
+++ b/ws/eclipse/HibIntro/src/main/java/entity/company7/model/Laptop.java
@@ -0,0 +1,63 @@
+package entity.company7.model;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+import javax.persistence.Table;
+import javax.persistence.UniqueConstraint;
+
+
+/**
+ * 
+ * Laptops possibly being assigned to {@link Employee}   
+ * 
+ */
+@Entity
+@Table(uniqueConstraints={@UniqueConstraint(columnNames={"idNumber"})})
+public class Laptop {
+
+  @Id
+  @GeneratedValue
+  public Long getId() {return id;}
+  protected void setId(Long id) {this.id = id;}
+  private Long id;
+
+  /**
+   * @return The device's unique part number
+   */
+  public int getIdNumber() { return idNumber;}
+  public void setIdNumber(int idNumber) { this.idNumber = idNumber;}
+  int idNumber;
+
+  @OneToOne (mappedBy="laptop",cascade={CascadeType.ALL})
+  public Employee getOwner() { return owner;}
+  
+  public void setOwner(Employee owner) {
+    if (getOwner() == owner) { return;}
+    if (null != getOwner()) { getOwner().setLaptopInternal(null);}
+    setOwnerInternal(owner);
+    if (null != getOwner()) { getOwner().setLaptopInternal(this);}
+  }
+  protected void setOwnerInternal(Employee owner) { this.owner = owner;}
+  Employee owner;
+
+  protected Laptop(){}
+
+  public Laptop(final int idNumber) {
+    setIdNumber(idNumber);
+  }
+  @Override
+  public boolean equals(Object other) {
+    if (this == other) {
+      return true;
+    } else if (other instanceof Laptop) {
+      return this.getIdNumber() == ((Laptop) other).getIdNumber();
+    } else {
+      return false;
+    }
+  }
+  @Override
+  public int hashCode() { return getIdNumber();}
+}
\ No newline at end of file
diff --git a/ws/eclipse/HibIntro/src/main/java/entity/company7/model/Project.java b/ws/eclipse/HibIntro/src/main/java/entity/company7/model/Project.java
new file mode 100644
index 000000000..2195a71c2
--- /dev/null
+++ b/ws/eclipse/HibIntro/src/main/java/entity/company7/model/Project.java
@@ -0,0 +1,59 @@
+package entity.company7.model;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.ManyToMany;
+import javax.persistence.Table;
+import javax.persistence.UniqueConstraint;
+
+/**
+ * 
+ *         {@link Project} instances related to {@link Employee}
+ * 
+ */
+@Entity
+@Table(uniqueConstraints={@UniqueConstraint(columnNames={"no"})})
+public class Project {
+
+  @Id
+  @GeneratedValue
+  public Long getId() {return id;}
+  protected void setId(Long id) {this.id = id;}
+  private Long id;
+
+  public int getNo() { return no;}
+  public void setNo(int no) { this.no = no;}
+  int no;
+  
+  public String getTitle() { return title;}
+  public void setTitle(String title) { this.title = title;}
+  String title;
+  
+  @ManyToMany(mappedBy="projects")
+  public Set<Employee> getCollaborators() { return collaborators;}
+  public void setCollaborators(Set<Employee> collaborators) { this.collaborators = collaborators;}
+  private Set<Employee> collaborators = new HashSet<Employee>();
+  
+  protected Project(){}
+
+  public Project(final int no, final String title) {
+    setNo(no);
+    setTitle(title);
+  }
+  @Override
+  public boolean equals(Object other) {
+    if (this == other) {
+      return true;
+    } else if (other instanceof Project) {
+      return getNo() == ((Project) other).getNo();
+    } else {
+      return false;
+    }
+  }
+  @Override
+  public int hashCode() { return getNo();}
+}
\ No newline at end of file
-- 
GitLab