`
heliminIT
  • 浏览: 571 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Hibernate的关联映射单向无连接表的N-1连接

阅读更多

 

Hibernate的关联映射

 

客观世界中的对象很少有孤立存在的,例如人与所在的城市往往存在关联关系,如果已经得到某个人的实例,那么应该可以直接获取该人所在的城市。反之,如果已经得到一个城市的实例,也应该可以访问该城市对应的人---这种实例之间的互相访问就是关联关系。

 

关联关系是面向对象分析、面向对象设计最重要的知识,Hibernate完全可以理解这种关联关系,如果映射得当,Hibernate的关联映射可以大大简化持久层数据的访问。

 

关联关系大致分如下两类:

 

单向关联:只需单向访问关联端。

 

1 -> 1

 

1 -> N

 

N -> 1

 

N -> N

 

双向关联:关联的两端可以互相访问。

 

   1 <-> 1

 

   1 <-> N

 

   N <-> N

 

  1. 单向N-1关联

 

为了让两个持久化类支持这种关联映射,程序应该在N的一端持久化类中增加一个属性,该属性引用1的一端的关联实体。

 

1.1无连接表的N-1关联

 

publicclass Person {

 

   //fields

 

   private Integer id;

 

   private String name;

 

   private String sex;

 

   //引用关联实体的属性

 

   private City city;

 

   //Constructors

 

   /** default Constructor */

 

   public Person(){

 

   }

 

   /** full Constructor */

 

   public Person(Integer id, String name, String sex) {

 

      this.id = id;

 

      this.name = name;

 

      this.sex = sex;

 

   }

 

   //Property accessors

 

   public Integer getId() {

 

      returnid;

 

   }

 

   publicvoid setId(Integer id) {

 

      this.id = id;

 

   }

 

 

 

   public String getName() {

 

      returnname;

 

   }

 

   publicvoid setName(String name) {

 

      this.name = name;

 

   }

 

   public String getSex() {

 

      returnsex;

 

   }

 

   publicvoid setSex(String sex) {

 

      this.sex = sex;

 

   }

 

   public City getCity() {

 

      returncity;

 

   }

 

   publicvoid setCity(City city) {

 

      this.city = city;

 

   }

 

}

 

 

 

publicclass City {

 

   private Integer cid;

 

   private String cname;

 

   private String cdesc;

 

   public City() {

 

   }

 

   public City(Integer cid, String cname, String cdesc) {

 

      this.cid = cid;

 

      this.cname = cname;

 

      this.cdesc = cdesc;

 

   }

 

   public Integer getCid() {

 

      returncid;

 

   }

 

   publicvoid setCid(Integer cid) {

 

      this.cid = cid;

 

   }

 

   public String getCname() {

 

      returncname;

 

   }

 

   publicvoid setCname(String cname) {

 

      this.cname = cname;

 

   }

 

   public String getCdesc() {

 

      returncdesc;

 

   }

 

   publicvoid setCdesc(String cdesc) {

 

      this.cdesc = cdesc;

 

   }

 

  

 

}

 

//Person.hbm.xml

 

<?xml version="1.0" encoding="utf-8"?>

 

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

 

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

 

 

<hibernate-mapping package="cn.jy.hibernate.domain">

 

    <class name="Person" table="persons" catalog="hibernate">

 

        <id name="id" >

 

            <generator class="increment" />

 

        </id>

 

        <property name="name"/>

 

        <property name="sex"/>

 

        <many-to-one name="city" cascade="all"

 

            class="City" column="cid"/>

 

    </class>

 

</hibernate-mapping>

 

 

 

//City.hbm.xml

 

<?xml version="1.0" encoding="utf-8"?>

 

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

 

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

 

 

<hibernate-mapping package="cn.jy.hibernate.domain">

 

    <class name="City" table="cities" catalog="hibernate">

 

        <id name="cid" >

 

            <generator class="increment" />

 

        </id>

 

        <property name="cname"/>

 

        <property name="cdesc"/>

 

    </class>

 

</hibernate-mapping>

 

 

 

 

 

publicclass TestPerson {

 

   privatestatic  SessionFactory sessionFactory; 

 

   static{

 

      Configuration configuration = new Configuration();

 

      configuration.configure("/hibernate.cfg.xml");

 

      sessionFactory = configuration.buildSessionFactory();

 

   }   

 

    @Test

 

    publicvoid testPerson() {

 

       Session session = sessionFactory.openSession();

 

       Transaction transaction = session.beginTransaction();

 

       //创建一个临时状态的City对象

 

       City city = new City();

 

       city.setCname("武汉");

 

       city.setCdesc("九省通衢");

 

       //创建一个Person对象

 

       Person person = new Person();

 

       person.setName("江月");

 

       person.setSex("");

 

       //设置Person对象和City对象之间的关联关系

 

       person.setCity(city);

 

       //持久化Person对象

 

       session.persist(person);

 

       //创建一个临时状态的City对象

 

       City city1 = new City();

 

       city1.setCname("咸宁");

 

       city1.setCdesc("温泉之乡");

 

       //修改持久化状态的Perosn对象

 

       person.setCity(city1);

 

       transaction.commit();

 

       session.close();

 

    }

 

}

 

 

 

 

 

 

 

<?xml version='1.0' encoding='utf-8'?>

 

 

 

<!DOCTYPE hibernate-configuration PUBLIC

 

 

 

        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 

 

 

<hibernate-configuration>  

 

 

 

<session-factory>

 

 

 

   <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">709709</property>

 

 

 

   <property name="hbm2ddl.auto">update</property>

 

 

 

   <property name="show_sql">true</property>

 

 

 

   <mapping resource="cn/jy/hibernate/domain/Person.hbm.xml" />

 

 

 

   <mapping resource="cn/jy/hibernate/domain/City.hbm.xml" />

 

 

 

</session-factory>

 

 

 

</hibernate-configuration>

 

 

 

 

 

 

分享到:
评论

相关推荐

    Hibernate关联映射

    Hibernate 一对一外键单向关联 Hibernate 一对一主键单向关联 Hibernate 一对一连接表单向...Hibernate 一对一连接表双向关联 Hibernate 一对多外键双向关联 Hibernate 一对多连接表双向关联 Hibernate 多对多双向关联

    hibernate学习笔记

    hibernate一对一主键关联映射(单向关联Person----&gt;IdCard) 8 hibernate一对一主键关联映射(双向关联Person&lt;----&gt;IdCard) 9 hibernate一对一唯一外键关联映射(单向关联Person----&gt;IdCard) 10 hibernate一对一...

    Hibernate关联关系映射目录

    Hibernate关联关系映射 单向关联 │ ├─ 一对一外键单向关联 │ ├─ 一对一主键单向关联 │ ├─ 一对一连接表单向关联 │ ├─ 一对多外键单向关联 │ ├─ 一对多连接表单向关联 │ ├─ 多对一外键单向关联 │ ...

    hibernate关系映射1to1及Nto1

    hibernate关系映射系列1,单向1对1映射,意向Nto1 程序说明:生成mysql数据库,建立DB Browser连接后,用户可在程序中自动生成mysql表,非常简单,适合初学者了解hibernate映射机制,有问题可联系flyallen5@hotmail.com

    Hibernate_Annotation关联映射

    和其它许多批注一样,在多对多关联中很多值是自动生成,党双向多对多关联中没有定义任何物理映射时,Hibernate根据以下规则生成相应的值,关联表名:主表表名+下划线+从表表名,关联到主表的外键名:主表名+下划线+...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

     15.2 映射单向多对多关联  15.3 映射双向多对多关联关系  15.3.1 关联两端使用元素  15.3.2 在inverse端使用元素  15.3.3 使用组件类集合  15.3.4 把多对多关联分解为两个一对多关联  15.4 小结  15.5 思考...

    HibernateAPI中文版.chm

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...

    hibernate3.2中文文档(chm格式)

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...

    Hibernate+中文文档

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...

    Hibernate中文详细学习文档

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...

    Hibernate 中文 html 帮助文档

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向...

    Hibernate注释大全收藏

    Hibernate注释大全收藏 声明实体Bean @Entity public class Flight implements Serializable { Long id; @Id public Long getId() { return id; } public void setId(Long id) { this.id = id; } } @Entity ...

    hibernate 体系结构与配置 参考文档(html)

    使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part1.rar

     15.2 映射单向多对多关联  15.3 映射双向多对多关联关系  15.3.1 关联两端使用元素  15.3.2 在inverse端使用元素  15.3.3 使用组件类集合  15.3.4 把多对多关联分解为两个一对多关联  15.4 小结  15.5 思考...

    Hibernate教程

    8.3. 使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一对一(one to one) 8.3.4. 多对多(many to many) 8.4. ...

    最全Hibernate 参考文档

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part4

     15.2 映射单向多对多关联  15.3 映射双向多对多关联关系  15.3.1 关联两端使用元素  15.3.2 在inverse端使用元素  15.3.3 使用组件类集合  15.3.4 把多对多关联分解为两个一对多关联  15.4 小结  15.5 思考...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part3

     15.2 映射单向多对多关联  15.3 映射双向多对多关联关系  15.3.1 关联两端使用元素  15.3.2 在inverse端使用元素  15.3.3 使用组件类集合  15.3.4 把多对多关联分解为两个一对多关联  15.4 小结  15.5 思考...

Global site tag (gtag.js) - Google Analytics