Hibernate4映射之一: One2One双向外键映射(xml方式)
来源:互联网 发布:java jsp里注释快捷键 编辑:程序博客网 时间:2024/05/10 09:57
Person实体public class Person {private int id;private String name;private int age;private Address address;..........}
Address实体public class Address {private int id;private String addr; private Person person;//双向.....}
xml配置文件
Person.hbm.xml<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.hibernate.model"><class name="Person" table="t_person"><id name="id" column="id"><generator class="native" /></id><property name="name" column="name" length="20" /><property name="age" column="age" /><many-to-one name="address" column="addressId" unique="true" not-null="true" /></class></hibernate-mapping>
Address.hbm.xml<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.hibernate.model"><class name="Address" table="t_address"><id name="id" column="id"><generator class="native" /></id><property name="addr" column="addr"/> <one-to-one name="person" property-ref="address" /></class></hibernate-mapping>
上面property-ref属性作用是将控制权交给Person中的address。
保存@Testpublic void testSave(){session = sessionFactory.openSession();session.beginTransaction();Address address = new Address();address.setAddr("beijing");Person p = new Person();p.setName("lisi");p.setAge(33);p.setAddress(address);session.save(address);session.save(p);session.getTransaction().commit();session.close();}
加载数据,双向映射,通过Person加载Address@Testpublic void testLoad(){session = sessionFactory.openSession();session.beginTransaction();Person p = (Person)session.load(Person.class, 1);System.out.println(p.getAddress().getAddr());session.getTransaction().commit();session.close();}
加载数据,双向映射,通过Address加载Person@Testpublic void testLoad1(){session = sessionFactory.openSession();session.beginTransaction();Address p = (Address)session.load(Address.class, 1);System.out.println(p.getPerson().getName());session.getTransaction().commit();session.close();}
hibernate.cfg.xml文件<?xml version="1.0" encoding="utf-8" ?><!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><!-- Database connection settings --><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property><property name="connection.username">root</property><property name="connection.password">admin</property><!-- JDBC connection pool (use the built-in) --><property name="connection.pool_size">1</property><!-- SQL dialect --><property name="dialect">org.hibernate.dialect.MySQLDialect</property><!-- Enable Hibernate's automatic session context management --><property name="current_session_context_class">thread</property><!-- Disable the second-level cache --><property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property><!-- Echo all executed SQL to stdout --><property name="show_sql">true</property><property name="format_sql">true</property><!-- <property name="hibernate.id.new_generator_mappings">false</property> --> <!-- Drop and re-create the database schema on startup --><property name="hbm2ddl.auto">update</property><mapping resource="com/hibernate/model/Person.hbm.xml"/><mapping resource="com/hibernate/model/Address.hbm.xml"/></session-factory></hibernate-configuration>
- Hibernate4映射之一: One2One双向外键映射(xml方式)
- Hibernate4映射之一: One2One双向外键映射(注解方式)
- Hibernate4映射之一: One2One单向外键映射(xml方式)
- Hibernate4映射之一: One2One单向外键映射(注解方式)
- Hibernate4映射之二: one2many双向映射(xml方式)
- Hibernate4映射之三: many2many双向映射(xml方式)
- Hibernate4映射之二: one2many双向映射(注解方式)
- Hibernate4映射之三: many2many双向映射(注解方式)
- Hibernate4映射之二: many2One单向映射(xml方式)
- Hibernate4映射之二: one2many单向映射(xml方式)
- Hibernate4映射之三: many2many单向映射(xml方式)
- Hibernate中,One2One映射关系的XML实现方式
- Hibernate 映射关系 ---One2One 外键关联
- Hibernate4一对一关系映射(唯一外键方式)
- Hibernate中的one2one映射实例二(一对一主键关联映射双向)
- Hibernate中的one2one的单双向关系映射
- Hibernate4映射之二: many2One单向映射(注解方式)
- Hibernate4映射之二: one2many单向映射(注解方式)
- IE下使用smsx.cab组件实现打印和打印预览功能
- this编译类型是Base,运行类型Derived
- 你的商品描述做好了吗?
- 服务端linux +svn+apache 实现客户端svn用户自主修改密码
- linux消息队列进程通信
- Hibernate4映射之一: One2One双向外键映射(xml方式)
- dwr spring 注解的时候报错:Id is required for element 'annotation-config' when used as a top-level tag
- 简易的制作Linux启动U盘
- Excel多条件求和 & SUMPRODUCT函数用法详解
- 自己用的一款模板解析程序V0.1(支持插件和扩展)(思路讨论和使用体验)
- 用锁定机制(lock关健字)实现线程的同步。
- eclipse配置tomcat的问题,不能加载配置文件!
- 水滴石穿C语言之指针、数组和函数
- 将普通表转变为分区表