|
ToDo:
|
package org.hikazoh; import java.io.Serializable; import javax.persistence.*; import java.util.*; @Entity @Table(name="Employee") @NamedQuery(name="org.hikazoh.Employee",query="from org.hikazoh.Employee where lastName =:lastname") public class Employee implements Serializable { @Id @Column(name="ID") @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer iD; @Column(name="FirstName") private String firstName; @Column(name="LastName") private String lastName; @ManyToOne(targetEntity=Department.class) @JoinColumn(name="departmentID",referencedColumnName="ID") private Department department; @ManyToMany(targetEntity=Skill.class) @JoinTable(name="Emp_Skill", joinColumns={@JoinColumn(name="EmpID")}, inverseJoinColumns={@JoinColumn(name="SkillID")}) private Setスキルskills; public Integer getID() { return iD; } public void setID(Integer id) { iD = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } public Set getSkills() { return skills; } public void setSkills(Set skills) { this.skills = skills; } }
package org.hikazoh; import java.io.Serializable; import javax.persistence.*; import java.util.*; @Entity @Table(name="Skill") public class Skill implements Serializable { @Id @Column(name="ID") @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer Id; @Column(name="NAME") private String Name; @ManyToMany(targetEntity=Employee.class) @JoinTable(name="Emp_Skill", joinColumns={@JoinColumn(name="SkillID")}, inverseJoinColumns={@JoinColumn(name="EmpID")}) private Setエントリーサンプル(関連テーブルに関しては、双方向だからかな?どっちかのSetに入れておけばちゃんとINSERTしてくれました)employees; public String getName() { return Name; } public void setName(String name) { Name = name; } public Set getEmployees() { return employees; } public void setEmployees(Set employees) { this.employees = employees; } public Integer getId() { return Id; } }
package org.hikazoh; import java.util.*; import org.hibernate.*; import org.hibernate.cfg.AnnotationConfiguration; public class ManyToManyTest { public static void main(String[] args) { SessionFactory sessionFactory = new AnnotationConfiguration() .addPackage("org.hikazoh") .addAnnotatedClass(Skill.class) .addAnnotatedClass(Employee.class) .addAnnotatedClass(Department.class) .buildSessionFactory(); Transaction tx = null; Session session = sessionFactory.openSession(); try{ tx = session.beginTransaction(); Department dept = new Department(); dept.setName("Sales"); session.save(dept); Employee emp = new Employee(); emp.setFirstName("Hika"); emp.setLastName("Zou"); emp.setDepartment(dept); Skill skill = new Skill(); skill.setName("ServerManagement"); session.save(emp); Setemps = new HashSet (); emps.add(emp); skill.setEmployees(emps); session.save(skill); tx.commit(); }catch(Exception e){ e.printStackTrace(); System.out.println("[" + e.getLocalizedMessage() + "]" ); if ( tx != null ){ tx.rollback(); } }finally{ session.close(); } sessionFactory.close(); } }
上下巻を本日読了。タブンいま訳しなおすと4日間の濃密な日々はカナーリ官能な表現になりそう。内容的には好きな作品です。村上春樹の源流を辿ったような気がします。