博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate一对一双向关联(注解)
阅读量:5059 次
发布时间:2019-06-12

本文共 7110 字,大约阅读时间需要 23 分钟。

每一个人(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 }
实体类IdCard
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 }
实体类Person
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 }
测试类Test
1 
2 5 6
7
8 9
10
11 org.hibernate.dialect.MySQLDialect12
13
14 jdbc:mysql://localhost:3306/test15
16
root
17
123
18
19 com.mysql.jdbc.Driver20
21
MySQL
22
true
23
24
true
25 26
thread
27
28
create
29
30
31
32 33
Hibernate.cfg.xml
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 ThreadLocal
threadLocal = 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
工具类HibernateUtil

 

 

转载于:https://www.cnblogs.com/yjq520/p/6119790.html

你可能感兴趣的文章
Activity启动过程中获取组件宽高的五种方式
查看>>
java导出Excel表格简单的方法
查看>>
SQLite数据库简介
查看>>
利用堆实现堆排序&优先队列
查看>>
Mono源码学习笔记:Console类(四)
查看>>
Android学习路线(十二)Activity生命周期——启动一个Activity
查看>>
《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇03:暂停游戏》
查看>>
CPU,寄存器,一缓二缓.... RAM ROM 外部存储器等简介
查看>>
windows下编译FreeSwitch
查看>>
git .gitignore 文件不起作用
查看>>
Alan Turing的纪录片观后感
查看>>
c#自定义控件中的事件处理
查看>>
App.config自定义节点读取
查看>>
unity3d根据手机串号和二维码做正版验证
查看>>
二十六、Android WebView缓存
查看>>
django Models 常用的字段和参数
查看>>
linux -- 嵌入式linux下wifi无线网卡驱动
查看>>
SVN使用教程总结
查看>>
SQL中varchar和nvarchar有什么区别?
查看>>
OpenCV矩阵运算总结
查看>>