1)建立Person类:
public class Person { private Integer id; private String name; private IdCard IdCard; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public IdCard getIdCard() { return IdCard; } public void setIdCard(IdCard card) { this.IdCard = card; } @Override public String toString() { return "Person [id=" + id + ",name=" + name + "]"; } }
2)建立IdCard类:
public class IdCard { private Integer id; private String number; private Person person; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } @Override public String toString() { return "IdCard [id=" + id + ",number=" + number + "]"; } }
3)建立持久化类:
public class perAndIdDao { /** * save方法 */ @Test public void testSave() { Session session = SessionFactoryTools.getSession(); Transaction tx = null; try { tx = session.beginTransaction(); // =============================================== // 新建两个Person对象 Person person1 = new Person(); person1.setName("张三"); Person person2 = new Person(); person2.setName("李四"); // 新建两个IdCard对象 IdCard idCard1 = new IdCard(); idCard1.setNumber("37292501"); IdCard idCard2 = new IdCard(); idCard2.setNumber("37292502"); // 加入关联对象 person1.setIdCard(idCard1); person2.setIdCard(idCard2); idCard1.setPerson(person1); idCard2.setPerson(person2); // 保存 session.save(person1); session.save(person2); session.save(idCard1); session.save(idCard2); // =============================================== tx.commit(); } catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } /** * getById方法 */ @Test public void testGetById() { Session session = SessionFactoryTools.getSession(); Transaction tx = null; try { tx = session.beginTransaction(); // =============================================== // 获取person队象并打印改队象的信息和相关联的idCard信息 Person person = (Person) session.get(Person.class, 1); System.out.println(person + ":"); System.out.println(person.getIdCard()); // 获取idCard对象并打印改队象的信息和相关联的person信息 IdCard idCard = (IdCard) session.get(IdCard.class, 2); System.out.println(idCard + ":"); System.out.println(idCard.getPerson()); // =============================================== tx.commit(); } catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } /** * 解除关联关系的方法 */ @Test public void testRemoveRelation() { Session session = SessionFactoryTools.getSession(); Transaction tx = null; try { tx = session.beginTransaction(); // =============================================== /** * 获取person对象并将其关联的IdCard设置为空 在多对一中, * 因为一方不具有外键管理权。所以解除外键关联是不能实现的 * Person person = (Person) session.get(Person.class, 1); * person.setIdCard(null); */ /** * 获取idCard对象并将其相关联的person设置为空 在多对一中, * 因为多方具有外键的管理权限,所以该外键的解除是能够实现的 */ IdCard idCard = (IdCard) session.get(IdCard.class, 1); idCard.setPerson(null); // =============================================== tx.commit(); } catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } /** * 删除队象的方法 */ @Test public void testDelete() { Session session = SessionFactoryTools.getSession(); Transaction tx = null; try { tx = session.beginTransaction(); // =============================================== /** * 获取person对象并删除该对象 * 在多对一中。因为一方不具有外键的管理权限。 所以删除该对象是不可能实现的 * Person person = (Person) session.get(Person.class, 1); * session.delete(person); */ /** * 获取idCard对象并删除该对象 * 在多对一中。因为多方具有外键爱你的管理权限, * 所以删除该对象是能够实现的 */ IdCard idCard = (IdCard) session.get(IdCard.class, 1); session.delete(idCard); // =============================================== tx.commit(); } catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } }
4)Person.hbm.xml的配置:
<? 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="map_3"> <!--类名:Person 类相应的表名:person 一对一相应类:IdCard 一对一相应的表:idCard 一对一所相应的外键的属性名:person --> <class name="Person" table="person"> <id name="id" type="int" column="id"> <generator class="native" /> </id> <property name="name" type="string" column="name" length="20" /> <one-to-one name="idCard" class="IdCard" property-ref="person"> </one-to-one> </class> </hibernate-mapping>
5)IdCard.hbm.xml的配置:
<? 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="map_3"> <!--类名:IdCard 类相应的表名:idCard 多对一相应类的表名:person 多对一相应的类名:Person 多对一相应的列名:personId 设置为一对一关系:unique="true" --> <class name="IdCard" table="idCard"> <id name="id" type="int" column="id"> <generator class="native" /> </id> <property name="number" type="string" column="number" /> <many-to-one name="person" class="Person" column="personId" unique="true"> </many-to-one> </class> </hibernate-mapping>
6) 主文件的配置:
<!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="foo"> <!-- 配置数据库信息 --> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql:///hibernate0 </property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- 其它配置 --> <property name="hibernate.show_sql">false</property> <property name="hibernate.format_sql">false</property> <property name="hbm2ddl.auto">update</property> <!-- 映射文件配置 --> <mapping resource="map_3/Person.hbm.xml" /> <mapping resource="map_3/IdCard.hbm.xml" /> </session-factory> </hibernate-configuration>
时间: 2024-11-10 16:32:15