|
ToDo:
|
ふつーに起きたが、調子悪くって2℃寝。
なんか年末にも色々あって、ヨーク考えたら確かに短期的、中期的、長期的視野に欠けているなぁって思いました。
自分の立ち位置と先の見通しを考えて行動しましょうね>ヲレ。
そう考えると…以下略。
package org.hikazoh; import java.io.Serializable; import javax.persistence.*; import java.util.*; @Entity @Table(name="Department") @NamedQuery(name="org.hikazoh.matchName",query="from org.hikazoh.Department Department where name = :name") public class Department implements Serializable { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="ID") private Integer iD; @Column(name="Name") private String name; @OneToMany(targetEntity=Employee.class) @JoinColumn(name="DepartmentID") private Set↑</employee>なTagが出ているのはご愛嬌。(ナンデダー??)employees; public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getEmployees() { return employees; } public void setEmployees(Set employees) { this.employees = employees; } public Integer getID() { return iD; } public void setID(Integer id) { iD = id; } }
package org.hikazoh; import java.io.Serializable; import javax.persistence.*; @Entity @Table(name="Employee") 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; @Column(name="DepartmentID") private Integer departmentID; 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 Integer getDepartmentID() { return departmentID; } public void setDepartmentID(Integer departmentID) { this.departmentID = departmentID; } }登録するには…
package org.hikazoh; import org.hibernate.*; import org.hibernate.cfg.AnnotationConfiguration; import java.util.*; public class SimpleTest { public static void main(String[] args) { SessionFactory sessionFactory = new AnnotationConfiguration() .addPackage("org.hikazoh") .addAnnotatedClass(Employee.class) .addAnnotatedClass(Department.class) .buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = null; 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.setDepartmentID(dept.getID()); session.save(emp); tx.commit(); }catch(Exception e){ e.printStackTrace(); System.out.println("[" + e.getLocalizedMessage() + "]"); if ( tx != null ){ tx.rollback(); } }finally{ session.close(); } sessionFactory.close(); } }HQLによる簡単な検索
package org.hikazoh; import org.hibernate.*; import org.hibernate.cfg.AnnotationConfiguration; import java.util.*; public class SimpleLoad { public static void main(String[] args) { SessionFactory sessionFactory = new AnnotationConfiguration() .addPackage("org.hikazoh") .addAnnotatedClass(Department.class) .addAnnotatedClass(Employee.class) .buildSessionFactory(); Transaction tx = null; Session session = sessionFactory.openSession(); try{ tx = session.beginTransaction(); Query query = session.getNamedQuery("org.hikazoh.matchName"); query.setString("name", "Sales"); Listlist =(List ) query.list(); for(Department dept:list){ System.out.println("[DepartmentName]= " + dept.getName()); for(Employee emp : dept.getEmployees()){ System.out.println("Emp First name = " + emp.getFirstName()); } } }catch(Exception e){ e.printStackTrace(); System.out.println("[" + e.getLocalizedMessage() + "]"); }finally{ session.close(); } sessionFactory.close(); } }
この考え方に賛同する部分が多いです。
直感を信じろ、自分を信じろ、好きを貫け、人を褒めろ、人の粗探ししてる暇があったら自分で何かやれ。って記事に触発されているヒトは周りにも居ます。ヲレみたいな人間でも色々と相談はされます。その時に言っているのは…
人間だもの、霞食っては生きてゆけません。明日で人間辞めるなら、それでいいけど…終わりまでのプランを考えた上で、今の立ち位置振る舞いを考えることだよ。
ってことですな。>自戒。
package org.hikazoh; import java.io.Serializable; import javax.persistence.*; @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; 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; } }組織
package org.hikazoh; import java.io.Serializable; import javax.persistence.*; import java.util.*; @Entity @Table(name="Department") @NamedQuery(name="org.hikazoh.matchName",query="from org.hikazoh.Department Department where name = :name") public class Department implements Serializable { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="ID") private Integer iD; @Column(name="Name") private String name; @OneToMany(targetEntity=Employee.class) @JoinColumn(name="DepartmentID") private Setエントリーはemployees; public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getEmployees() { return employees; } public void setEmployees(Set employees) { this.employees = employees; } public Integer getID() { return iD; } public void setID(Integer id) { iD = id; } }
package org.hikazoh; import org.hibernate.*; import org.hibernate.cfg.AnnotationConfiguration; import java.util.*; public class SimpleTest { public static void main(String[] args) { SessionFactory sessionFactory = new AnnotationConfiguration() .addPackage("org.hikazoh") .addAnnotatedClass(Employee.class) .addAnnotatedClass(Department.class) .buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = null; 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); session.save(emp); tx.commit(); }catch(Exception e){ e.printStackTrace(); System.out.println("[" + e.getLocalizedMessage() + "]"); if ( tx != null ){ tx.rollback(); } }finally{ session.close(); } sessionFactory.close(); } }検索は…従業員からと組織からをHQLで簡単に…
package org.hikazoh; import org.hibernate.*; import org.hibernate.cfg.AnnotationConfiguration; import java.util.*; public class SimpleLoad { public static void main(String[] args) { SessionFactory sessionFactory = new AnnotationConfiguration() .addPackage("org.hikazoh") .addAnnotatedClass(Department.class) .addAnnotatedClass(Employee.class) .buildSessionFactory(); Transaction tx = null; Session session = sessionFactory.openSession(); try{ tx = session.beginTransaction(); Query query = session.getNamedQuery("org.hikazoh.matchName"); query.setString("name", "Sales"); Listlist =(List ) query.list(); for(Department dept:list){ System.out.println("[DepartmentName]= " + dept.getName()); for(Employee emp : dept.getEmployees()){ System.out.println("Emp First name = " + emp.getFirstName()); } } query = session.getNamedQuery("org.hikazoh.Employee"); query.setString("lastname", "Zou"); List emps = (List )query.list(); for(Employee emp : emps){ System.out.println("[Emp First name]" + emp.getFirstName()); System.out.println("Department name]" + emp.getDepartment().getName()); } }catch(Exception e){ e.printStackTrace(); System.out.println("[" + e.getLocalizedMessage() + "]"); }finally{ session.close(); } sessionFactory.close(); } }