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

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>