Hibernate学习总结:OneToOne单向主键关联
来源:互联网 发布:c语言奇数阶幻方 编辑:程序博客网 时间:2024/05/18 13:24
说明:
一、还是举那个一夫一妻的例子来讲述一对一单向主键关联。
二、本次使用的Hibernate版本是hibernate-release-4.3.11.Final.
先写Annotation版本:
实体类:
1.Husband.java
package com.buaa.hibernate.bean;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.MapsId;import javax.persistence.OneToOne;@Entitypublic class Husband {private int id;private String name;private Wife wife;@Idpublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@OneToOne@MapsId //4.3.11版本的Hibernate将注解PrimaryKeyJoinColumn→MapsIdpublic Wife getWife() {return wife;}public void setWife(Wife wife) {this.wife = wife;}}Wife.java:
package com.buaa.hibernate.bean;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Wife {private int id;private String name;@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
xml版本:
Husband.hbm.xml:
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.buaa.hibernate.bean"><class name="Husband" table="HUSBAND"> <id name="id" column="wifeId"> <generator class="foreign"> <param name="property">wife</param> </generator> </id> <property name="name"/> <one-to-one name="wife" constrained="true"/></class><class name="Wife"><id name="id"><generator class="native" /></id><property name="name"/></class></hibernate-mapping>此处将两个类写在一个配置文件里面了,并且配置文件里面的<one-to-one/>元素属性constrained一定要设置为true,如果不设置的话,将不会产生外键关联。
配置文件hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- Assume test is the database name --> <property name="connection.url">jdbc:mysql://localhost/test</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <!--<property name="hbm2ddl.auto">create</property>--><!--<mapping resource="com/buaa/hibernate/bean/Husband.hbm.xml"/>--><mapping class="com.buaa.hibernate.bean.Husband"/><mapping class="com.buaa.hibernate.bean.Wife"/></session-factory></hibernate-configuration>
下面是Junit测试类:
package com.buaa.hibernate.bean;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.tool.hbm2ddl.SchemaExport;import org.junit.Test;public class OneToOneTest {@Testpublic void test(){Wife wife = new Wife();wife.setName("lisa");Husband t = new Husband();t.setName("james");t.setWife(wife);Configuration cfg = new Configuration().configure();ServiceRegistry service = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();SessionFactory factory = cfg.buildSessionFactory(service);Session session = factory.openSession();session.beginTransaction();session.save(wife);session.save(t);session.getTransaction().commit();session.close();factory.close();}@Testpublic void SchemaExportTest(){Configuration cfg = new Configuration().configure();//第一个参数是否将sql语句输出到控制台,第二个参数是否在数据库中生成对应的表格new SchemaExport(cfg).create(true, true);}}
0 0
- Hibernate学习总结:OneToOne单向主键关联
- Hibernate学习总结:OneToOne双向主键关联
- Hibernate annotation@OneToOne共享主键关联
- hibernate JPA @OneToOne双向关联共享主键
- Hibernate主键一对一关联映射实例【xml和注解版本@OneToOne@JoinColumn(name="wifeId")--主键单向】(十二)
- Hibernate学习总结:OneToOne单向外键关联
- Hibernate学习总结:OneToOne双向外键关联
- Hibernate 一对一主键单向关联
- Hibernate 一对一主键单向关联
- Hibernate one2one 主键单向关联
- Hibernate 一对一主键单向关联
- Hibernate:一对一单向主键关联
- Hibernate 一对一主键单向关联
- Hibernate学习总结:ManyToOne单向关联
- Hibernate学习总结:OneToMany单向关联
- Hibernate学习总结:ManyToMany单向关联
- Hibernate学习笔记_08_一对一单向、双向主键关联
- Hibernate关联之 一对一主键单向关联
- spring源码剖析(五)利用AOP实现自定义Spring注解
- 在word文档中添加目录(包括项目符号、编号、多级列表使用方法)
- CodeForces346 C. Number Transformation II
- Unity3D学习笔记(7)—— 击球游戏
- 【BC史上最大灾难】一场被我搞砸了的BC
- Hibernate学习总结:OneToOne单向主键关联
- MyEclipse创建JavaWeb应用(配置Tomcat)
- 自己写一个httpclient(一)
- 简谈ready与load的区别
- android 硬件知识-传感器,无线电,和媒体功能的开发
- Vmware安装debian时更换iso文件方法
- hdu 1080(LCS变形)
- 【hdu2594】Simpsons’ Hidden Talents——KMP
- flask下博客系统输入中文乱码问题(ubuntu14.04+mysql5.5.46)