Hibernate学习09---基于XML配置的一对一单向外键关联
来源:互联网 发布:优化设计方案 编辑:程序博客网 时间:2024/06/10 02:40
与基于Annotation配置的大体类似。
1、pojo类
package com.demo.pojo.dxwj;public class IdCard {private String pid;private String province;public String getPid() {return pid;}public void setPid(String pid) {this.pid = pid;}public String getProvince() {return province;}public void setProvince(String province) {this.province = province;}}
package com.demo.pojo.dxwj;public class Students {private int sid;private String sname;private IdCard cardId;public int getSid() {return sid;}public void setSid(int sid) {this.sid = sid;}public String getSname() {return sname;}public void setSname(String sname) {this.sname = sname;}public IdCard getCardId() {return cardId;}public void setCardId(IdCard cardId) {this.cardId = cardId;}}
2、映射文件
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.demo.pojo.dxwj"><!-- 一个class对应一个类,与表相关联起来 --><class name="IdCard" table="idcard"><!--id对应的是主键 --><id name="pid" column="pid" type="string"><generator class="assigned"></generator></id><!-- property对应的是其它列 --><property name="province" column="province" type="string" /></class></hibernate-mapping>
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.demo.pojo.dxwj"><!-- 一个class对应一个类,与表相关联起来 --><class name="Students" table="students"><!--id对应的是主键 --><id name="sid" column="sid" type="int"></id><!-- property对应的是其它列 --><property name="sname" column="sname" type="string" /><many-to-one name="cardId" column="pid" unique="true"></many-to-one></class></hibernate-mapping>
3、hibernate配置文件
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory name="mysql"><property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/test</property> <property name="connection.username">root</property> <property name="connection.password"></property><property name="dialect">org.hibernate.dialect.MySQL5Dialect</property><!-- sessionFactory.getCurrentSession用到的配置文件 --><property name="hibernate.current_session_context_class">thread</property><!-- 有四个值create,create-drop,update,validate --><property name="hbm2ddl.auto">update</property><property name="show_sql">true</property><!--<mapping class="com.demo.pojo.dxwj.Students"/><mapping class="com.demo.pojo.dxwj.IdCard"/>--><mapping resource="com/demo/pojo/dxwj/Students.hbm.xml"/><mapping resource="com/demo/pojo/dxwj/IdCard.hbm.xml"/></session-factory></hibernate-configuration>
4、测试类
package com.demo.pojo.dxwj;import junit.framework.TestCase;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;public class TestStudentByXML extends TestCase{private SessionFactory sf;public void testSave(){sf = new Configuration().configure().buildSessionFactory();Session s = sf.getCurrentSession();Transaction tx = s.beginTransaction();tx.begin();IdCard idCard = new IdCard();idCard.setPid("11111111");idCard.setProvince("fujian");Students student = new Students();//因为是主键类型是Auto的,所以不用setId..student.setSname("zhangsan");student.setCardId(idCard);//先保存外键所在的对象s.save(idCard);//再保存主键所在的对象s.save(student);tx.commit();}public void testSchemaExport(){SchemaExport se = new SchemaExport(new Configuration().configure());se.create(true, true);}protected void setUp() throws Exception {System.out.println("setUp()....");}protected void tearDown() throws Exception {System.out.println("tearDown()....");}}
- Hibernate学习09---基于XML配置的一对一单向外键关联
- Hiberante学习08---基于Annotation配置的一对一单向外键关联
- hibernate一对一单向外键关联之XML
- Hibernate学习笔记_06_一对一单向外键关联
- hibernate一对一单向外键关联
- hibernate一对一单向外键关联_Annotation
- hibernate一对一单向外键关联_XML
- hibernate Annotation一对一单向外键关联
- hibernate一对一单向外键关联
- hibernate一对一单向外键关联
- hibernate学习:一对一单向外键映射
- 【hibernate框架】关系映射之一对一单向外键关联(XML实现)
- Hibernate关系映射(1)_一对一单向外键关联
- hibernate一对一单向外键关联_Annotation_联合主键
- hibernate annotation 之 一对一 单/双 向外键关联
- hibernate一对一单向外键关联之Annotation
- hibernate一对一单向外键关联(Annatation)
- hibernate--一对一单(双)向外键关联
- IEEE(电子电气工程师协会)802局域网标准
- 故事的开始
- storm 的状态转换函数
- 在win7环境下测试oracle kill long transaction
- 输出javabean集合信息
- Hibernate学习09---基于XML配置的一对一单向外键关联
- 类的静态成员函数带来了什么好处,应该在什么时候使用?
- 稀疏表示(step by step)
- Linux系统下查看目录大小
- awk基本用法
- 话说程序猿好悲剧
- 使用HTML5和CSS3创建一个时髦的联系表单
- 虚函数
- How many 0's?