|
ToDo:
|
ひかぞーは大喜びだったらしい。何時までもピュアなココロを持っていて欲しいな。
CREATE TABLE `test`.`Employee` ( `ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, `FistName` VARCHAR(45) NOT NULL, `LastName` VARCHAR(45) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE = InnoDB;Hibernate AnnotationのModel
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; 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; } }サンプルコードは…
package org.hikazoh; import org.hibernate.*; import org.hibernate.cfg.AnnotationConfiguration; public class SimpleTest { public static void main(String[] args) { SessionFactory sessionFactory = new AnnotationConfiguration() .addPackage("org.hikazoh") .addAnnotatedClass(Employee.class) .buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); Employee emp = new Employee(); emp.setFirstName("Hika"); emp.setLastName("Zou"); session.save(emp); tx.commit(); }catch(Exception e){ if ( tx != null ){ tx.rollback(); } }finally{ session.close(); } sessionFactory.close(); } }