hibernate的关系映射(1)

要注意此处的关系映射指的是对象之间的关系,并不是指数据库的关系(数据库中表与表之间只有外键的关系)(1:1, 1:n, n:n)

   (1) 1:1关系映射

l  11单向外键关联

方法一:annotation

涉及两个注解:@OneToOne@JoinColumn

以丈夫、妻子关联为例(会自动将妻子表的主键当成外键),例如:

@OneToOne

    @JoinColumn(name="wifeId")

    public Wife getWife() {

       return wife

}

public void setWife(Wife wife) {

           this.wife = wife;

}

创建出的两张表分别如下:

create table Husband (id integer not null auto_increment, name

varchar(255), wifeId integer, primary key (id))

create table Wife (id integer not null auto_increment, name varchar(255),

primary key (id))

alter table Husband add index FKAEEA401B389C8730 (wife_id), add constraint FKAEEA401B389C8730 foreign key (wifeId) references Wife (id)

    方法二:xml配置

在要参考的类上对应的Class.hbm.xml文件配置上加上<many-to-one>标签,下面以学生-学生证为例:

<hibernate-mapping>

    <class name=”com.yilong.hibernate.StuIdCard”>

       <id name=”id”>

           <generator class=”native”></generator>

       </id>

       <property name=”num”/>

       <many-to-one name=”student” column=”studentId” unique=”true”>

    </class>

</hibernate-mapping>

    说明:其中name=”student”中的student为类StuIdCard上的其中的一个属性:

Student student;表示将以类Student里的主键为外键。column为外键在表中的字段名;unique=”true”将把many-to-one表示成11

l  11双向外键关联

方法一:annotation

还是以上面的为例,双向外键关联只需要在妻子类的丈夫属性上加上@OneToOne

的注解,并说明以丈夫类的外键为主导即可。双向关联中,mappedBy属性都应该设置。

    @OneToOne(mappedBy="wife")

    public Husband getHusband() {

       return husband;

    }

    public void setHusband(Husband husband) {

       this.husband = husband;

    }

    方法二:xml配置

    还是以学生-学生证为例,只需要在上面的基础上,在Student.hbm.xml文件中设置:

    <hibernate-mapping package="com.yilong.hibernate.model">

    <class name="Student" table="_student">

       <id name="id" column="id"></id>

       <property name="name"></property>

       <property name="age"></property>

      

       <one-to-one name="stuid" property-ref="student"></one-to-one>

    </class>

</hibernate-mapping>

说明:其中name=”stuid”含义为:类Student参考StudentIdCard,然后peoperty-ref=”student”则是以StudentIdCard里的Student student属性为主导。

为了更加清晰,给出类StudentStudentIdCard的属性。

--Student类里面的属性:

private int id;

    private String name;

    private int age;

private StudentIdCard stuid;

--StudentIdCard类里面的属性:

private int id;

private Student student;

小结:双向外键关联必须设置以对方为主导这一特性

ü  Annotation @OneToOne(mappedBy="wife")

ü  XML配置

<one-to-one name="stuid" property-ref="student"></one-to-one>

    注意:单向映射和双向映射在数据库中没什么区别,区别主要表现在程序中,单向映射只能从一方找到另一方,而双向则可以互相找到。

l  1:1单向、双向主键关联(主键关联是指双方的主键一致,用来关联)

(2) 组件映射

即将其中一个类的属性加到另一个类属性中合成一张表。

方法一:annotation

以上述的丈夫-妻子为例,需要注意的方面有:

l  丈夫类中已有id作为主键,因此妻子类不需要id了;

l  将两个类合成一张表,因此妻子类中就不需要加上@Entityhibernate.hbm.xml中也不需要加入关于妻子类的配置<mapping class="com.yilong.hibernate.model.Wife"/>

l  既然合成一张表,那么丈夫妻子类中的属性名就不能重复,否则只会被当成数据库中同一个字段创建;

l  丈夫类中将Wife类当成属性,并在getWife()方法上加入注解@Embedded

@Embedded

    public Wife getWife() {

       return wife;

    }

    这样执行出来创建出的就是HusbandWife类合成的一张表:

create table Husband_Wife (id integer not null auto_increment, name

varchar(255), wifeAge integer not null, wifeName varchar(255),

primary key (id))

方法二:xml配置

<component name=”wife”>

    <property name=”wifename”></property>

    <property name=”age”></property>

</component>

    (3) n:1 单向关联

   方法一:annotation

    n:1含义:n那方的类拥有1那方类的实例对象作为属性。

思路:在n的一方加入1那方的id

    需要设置的内容:

l  n那方的类中加入1那方的类作为属性;

l  添加@ManyToOne注解;

下面以-成员模式为例(一个组包含若干位成员)

@Entity

@Table(name="_user")

public class User {

   

    private int id;

    private String name;

    private Group group;

   

    @ManyToOne

    @JoinColumn(name="groupId")

    public Group getGroup() {

        return group;

    }

    public void setGroup(Group group) {

        this.group = group;

    }

    @Id

    @GeneratedValue

    public int getId() {

        return id;

    }

    public void setId(int id) {

        this.id = id;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

}

方法二:使用xml配置

作为1的一方的类.hbm.xml不需要改变什么

<hibernate-mapping package="com.yilong.hibernate.model">

    <class name="Group" table="_group">

       <id name="id" column="id">

           <generator class="native"></generator>   

       </id>

       <property name="name"></property>

    </class>

</hibernate-mapping>

    作为n的那方的类.hbm.xml则需要添加<many-to-one>标签

<hibernate-mapping package="com.yilong.hibernate.model">

    <class name="User" table="_user">

       <id name="id" column="id">

           <generator class="native"></generator>   

       </id>

       <property name="name"></property>

       <many-to-one name="group" column="gruopId"></many-to-one>

    </class>

</hibernate-mapping>

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章