每一个人(Person)对应一个身份证号(IdCard)
1 package cqvie.yjq.domain; 2 3 import java.util.Date; 4 5 import javax.persistence.Column; 6 import javax.persistence.Entity; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.Id; 9 import javax.persistence.JoinColumn;10 import javax.persistence.OneToOne;11 import javax.persistence.Table;12 13 import org.hibernate.annotations.GenericGenerator;14 15 16 @Entity17 @Table(name = "id_card", catalog = "test")18 public class IdCard implements java.io.Serializable {19 20 private static final long serialVersionUID = -5388108961244621175L;21 22 @Id23 @GenericGenerator(name = "generator", strategy = "uuid")24 @GeneratedValue(generator = "generator")25 @Column(name = "card_id", unique = true, nullable = false)26 private String id;27 @Column(name = "validate")28 private Date validateDte;29 @OneToOne30 @JoinColumn(name = "per")31 private Person person;32 33 public String getId() {34 return id;35 }36 public void setId(String id) {37 this.id = id;38 }39 public Date getValidateDte() {40 return validateDte;41 }42 public void setValidateDte(Date validateDte) {43 this.validateDte = validateDte;44 }45 public Person getPerson() {46 return person;47 }48 public void setPerson(Person person) {49 this.person = person;50 }51 52 }
1 package cqvie.yjq.domain; 2 3 import javax.persistence.Column; 4 import javax.persistence.Entity; 5 import javax.persistence.GeneratedValue; 6 import javax.persistence.Id; 7 import javax.persistence.JoinColumn; 8 import javax.persistence.OneToOne; 9 import javax.persistence.Table;10 11 import org.hibernate.annotations.GenericGenerator;12 13 @Entity14 @Table(name = "person", catalog = "test")15 public class Person implements java.io.Serializable {16 17 private static final long serialVersionUID = 3860690163559279293L;18 19 @Id20 @GenericGenerator(name = "generator", strategy = "uuid")21 @GeneratedValue(generator = "generator")22 @Column(name = "per_id", unique = true, nullable = false)23 private String id;24 @Column(name = "name", nullable = false, length = 20)25 private String name;26 @OneToOne27 @JoinColumn(name = "id_c")28 private IdCard idCard;29 30 31 public String getId() {32 return id;33 }34 public void setId(String id) {35 this.id = id;36 }37 public String getName() {38 return name;39 }40 public void setName(String name) {41 this.name = name;42 }43 public IdCard getIdCard() {44 return idCard;45 }46 public void setIdCard(IdCard idCard) {47 this.idCard = idCard;48 }49 50 }
1 package cqvie.yjq.View; 2 3 import java.util.Date; 4 5 import org.hibernate.Session; 6 import org.hibernate.Transaction; 7 import org.hibernate.cfg.AnnotationConfiguration; 8 import org.hibernate.cfg.Configuration; 9 import org.hibernate.tool.hbm2ddl.SchemaExport;10 11 import cqvie.yjq.Util.HibernataUtil;12 import cqvie.yjq.domain.IdCard;13 import cqvie.yjq.domain.Person;14 15 public class Test {16 17 public static void main(String[] args) {18 19 //调用建表语句20 exportDDL();21 //添加一组person22 Session session = null;23 Transaction tx = null;24 try {25 session = HibernataUtil.getCurrentSession();26 tx = session.beginTransaction();27 28 Person p1 = new Person();29 p1.setName("张三");30 31 IdCard idCard = new IdCard();32 idCard.setValidateDte(new Date());33 p1.setIdCard(idCard);34 idCard.setPerson(p1);35 session.save(p1);36 session.save(idCard);37 38 tx.commit();39 } catch (Exception e) {40 if(tx != null) {41 tx.rollback();42 }43 } finally {44 if(session != null && session.isOpen()) {45 session.close();46 }47 }48 49 }50 51 //建表语句52 public static void exportDDL() { 53 Configuration configuration = new AnnotationConfiguration().configure(); 54 SchemaExport sexport = new SchemaExport(configuration); 55 sexport.setFormat(true);//格式化输出 56 sexport.setDelimiter(";"); //每句sql都以;结尾 不然导入sql的时候会出现错误 57 sexport.setOutputFile("D:\\auto.sql"); 58 sexport.create(true, true); 59 } 60 }
1 2 5 6 78 9 10 32 3311 org.hibernate.dialect.MySQLDialect12 1314 jdbc:mysql://localhost:3306/test15 16root 17123 1819 com.mysql.jdbc.Driver20 21MySQL 22true 23 24true 25 26thread 27 28create 2930 31
1 package cqvie.yjq.Util; 2 3 import java.util.List; 4 5 import org.hibernate.Query; 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.Transaction; 9 import org.hibernate.cfg.AnnotationConfiguration; 10 11 final public class HibernataUtil { 12 13 private static SessionFactory sessionFactory = null; 14 //使用线程局部模式 15 private static ThreadLocalthreadLocal = new ThreadLocal (); 16 private HibernataUtil() {}; 17 static { 18 sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 19 } 20 //获取全新的session 21 public static Session openSession() { 22 return sessionFactory.openSession(); 23 } 24 //获取和线程关联的session 25 public static Session getCurrentSession() { 26 Session session = threadLocal.get(); 27 //判断是否得到 28 if(session == null) { 29 session = sessionFactory.openSession(); 30 //把session对象设置到threadLocal,相当于已经和线程绑定 31 threadLocal.set(session); 32 } 33 return session; 34 } 35 36 //提供一个统一的修改和删除方法(批量) 37 public static void executeUpdate(String hql, String[] parameters) { 38 Session s = null; 39 Transaction ts = null; 40 try { 41 s = openSession(); 42 ts = s.beginTransaction(); 43 Query query = s.createQuery(hql); 44 //先判断是否有参数要绑定 45 if(parameters != null && parameters.length > 0) { 46 for(int i=0;i 0) { 93 for(int i=0;i 0) {121 for(int i=0;i