与基于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()...."); } }