gavanwanggw

导航

hibernate之关于一对一单向,双向关联映射


【hibernate】之关于一对一单向,双向关联映射


首先我们来看,Hibernate官方对于一对一单向关联的解释:


基于外键关联的单向一对一关联和单向多对一关联差点儿是一样的。

唯一的不同就是单向一对一关

联中的外键字段具有唯一性约束。


ok,那我们也能够这样理解,一对一事实上就是多对一关联的特殊形式,我们知道Hibernate的配置方式有两种,各自是AnnotationsXML两种配置方式!


Annotations的一对一单向关联映射


人(User)和身 份 证号(Card)

@Entity
@Table(name="t_user")
publicclass User {
    private Integer id;
    private String name;
    private Card card;
     
    @OneToOne
    @JoinColumn(name="card_id",unique=true)//name是自己定义关联外键的列名
    public Card getCard() {
       returncard;
    }
    publicvoid setCard(Card card) {
       this.card = card;
    }
    @Id
    @GeneratedValue
    public Integer getId() {
       returnid;
    }
    publicvoid setId(Integerid) {
       this.id = id;
    }
     
    @Column(name="name")
    public String getName() {
       returnname;
    }
    publicvoid setName(Stringname) {
       this.name = name;
    }


@Entity
@Table(name="t_card")
publicclass Card {
    private Integer id;
    private String num;
     
    @Id
    @GeneratedValue
    public Integer getId() {
       returnid;
    }
    publicvoid setId(Integerid) {
       this.id = id;
    }
    @Column(name="card_id")
    public String getNum() {
       returnnum;
    }
    publicvoid setNum(Stringnum) {
       this.num = num;
    }
}

XML一对一单向关联

PersonGroup,一个人仅仅能在一个组

publicclass Group {
    private Integer id;
    private String name;
    public Integer getId() {
       returnid;
    }
    publicvoid setId(Integerid) {
       this.id = id;
    }
    public String getName() {
       returnname;
    }
    publicvoid setName(Stringname) {
       this.name = name;
    }
}

publicclass Person {
    private Integer id;
    private String name;
    private Integer age;
    private Group group;
    public Group getGroup() {
       returngroup;
    }
    publicvoid setGroup(Groupgroup) {
       this.group = group;
    }
    public Integer getId() {
       returnid;
    }
    publicvoid setId(Integerid) {
       this.id = id;
    }
    public String getName() {
       return name;
    }
    public void setName(Stringname) {
       this.name = name;
    }
    public Integer getAge() {
       return age;
    }
    publicvoid setAge(Integer age){
       this.age = age;
    }
}

<?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="csg.hibernate.entity">
    <class name="Group" table="t_group">
       <id name="id">
           <column name="id"/>
           <generator class="native" />
       </id>
       <property name="name" />
    </class>
</hibernate-mapping>

<?

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="csg.hibernate.entity"> <class name="Person" table="t_person"> <id name="id"> <column name="id"/> <generator class="native" /> </id> <property name="name" /> <property name="age" /> <many-to-one name="group" column="group_id" unique="true" not-null="true" /> </class> </hibernate-mapping>

Ok,到这里,大家就有点疑惑,为什么我们的Annotaions配置採用的是one-to-one而我们的xml配置採用的是many-to-one呢?

当然XML配置是我參考Hibernate的文档而来,由于一对一本身就是多对一的特殊形式,

可是配置many-to-one又给我们一种暗示。多个***号相应一个人。所以Hibernate依据这样的情况提供unique唯一性来确认!


Annotations一对一双向关联映射


UserCard


@Entity
@Table(name="t_user")
publicclass User {
    private Integer id;
    private String name;
    private Card card;
    @OneToOne
    @JoinColumn(name="card_id",unique=true)//name主要是自己定义关联外键的列名
    public Card getCard() {
       returncard;
    }
    publicvoid setCard(Cardcard) {
       this.card = card;
    }
    @Id
    @GeneratedValue
    public Integer getId() {
       returnid;
    }
    publicvoid setId(Integerid) {
       this.id = id;
    }
    @Column(name="name")
    public String getName() {
       returnname;
    }
    publicvoid setName(Stringname) {
       this.name = name;
    }
}

@Entity
@Table(name="t_card")
publicclass Card {
    private Integer id;
    private String num;
    private User user;
     
    @OneToOne(mappedBy="card")//mappedBy的意思是指定User中的card作为关联外键。否则User和Card都会出现外键
    public User getUser() {
       returnuser;
    }
    publicvoid setUser(Useruser) {
       this.user = user;
    }
    @Id
    @GeneratedValue
    public Integer getId() {
       returnid;
    }
    publicvoid setId(Integerid) {
       this.id = id;
    }
    @Column(name="card_id")
    public String getNum() {
       returnnum;
    }
    publicvoid setNum(Stringnum) {
       this.num = num;
    }
}

Ok?那么我们能够看到在Annotaions中的配置都是ont-to-one,那么我们在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="csg.hibernate.entity">
    <class name="Person" table="t_person">
       <id name="id">
           <column name="id"/>
           <generator class="native" />
       </id>
       <property name="name" />
       <property name="age" />
       <many-to-one name="group" column="group_id" unique="true" not-null="true" />
    </class>
</hibernate-mapping>

<?

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="csg.hibernate.entity"> <class name="Group" table="t_group"> <id name="id"> <column name="id"/> <generator class="native" /> </id> <property name="name" /> <!-- many-to-one这样的配置会分别在两个表中都产生外键,造成数据的多余,通常我们採用one-to-one的形式在xml中配置 --> <many-to-one name="Person" column="person_id" unique="true" /> <!-- <one-to-onename="person"property-ref="group"/> --> </class> </hibernate-mapping>


ok,到这里一对一的单双向关联映射基本上就是这样。随笔之作,有问题。请留言!







posted on 2017-07-03 14:30  gavanwanggw  阅读(230)  评论(0编辑  收藏  举报