对象之间的关系:
关系映射之间的关系只的是对象之间的关系,并不指数据库表的关系(外键关系)这儿解决的问题是当对象之间的关系之一时,数据库表该如何映射,编程上如何对待。
一对一(主键关联,和单向的外键关联)
①单向(主键,外键)Husband和Wife表,a,主键关联 b,外键关联(wife为parent) ,c,主键关联 d,外键关联(husband为parent)
一对一单向外键关联:
annotation:@oneToOne @JoinColumn(name="wifeId")
xml : <many-to-one unique="true"/>
一对一单向主键关联:annotation:@PrimaryKeyJoinColumn(name="id",referencedColumnName="id")
xml: <one-to-one name="stu" constrained="true"/>
②双向(主键,外键)
一对一双向外键关联:
annotation:在husband类中:@oneToOne
在wife类中:@OneToOne(mappedBy="wife")对方为主导,对于一对一双向外键关联必须写mappedBy
xml : <many-to-one name="stu" unique="true" column="stuid" not-null="true"/>
<one-to-one name="idCard
property-ref="stu"></one-to-one>
<generator class="foreign">
<param name="property">stu</param>
</generator>
一对一双向主键关联:
annotation:
@OneToOne
@PrimaryKeyJoinColumn(name="id",referencedColumnName="id")
@OneToOne(mappedBy="wife")
@PrimaryKeyJoinColumn(name="id",columnDefinition="id")
a ,Husband的id与wife的id相互关联 b,husband的外键wifeid和wife中husbandid
联合主键关联(不重要):
Annotation:
@OneToOne
@JoinColumns(
{
@JoinColumn(name="wifid",referencedColumnName="id"),
@JoinColumn(name="wifename",referencedColumnName="name")
}
)
③中间表 建立一张中间表
一对多
①单向:
annotation:在一的一方加集合(Set),这种处理方式,hibernate把它当做了多对多关系处理,在数据库中建立了一张关系表。
@OneToMany
@JoinColumn(name="groupid")
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
Xml:
<set name="users">
<key column="groupid"></key>
<one-to-many class="User"/>
</set>
②双向(多对一的双向关联,一对多的双向关联是一样的)
多对一:
①单向(manytoone的单向关联):在多的一方加外键。1NF,要有主键,列不可分;2NF联合主键不能存在部分依赖3NF不能存在传递依赖。设计原则:不能产生冗余。
annotation:@ManyToOne
xml:<many-to-one name="g" column="groupid"></many-to-one>
②双向:(多对一的双向关联,一对多的双向关联是一样的)
Annotation:@ManyToOne
xml:
<set name="aliases" table="person_aliases" order-by="lower(name) asc">
<key column="person"/>
<element column="name" type="string"/>
</set>
多对多
①单向
annotation: @ManyToMany
xml :
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses" table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId"
class="Address"/>
</set>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
②双向:
Annotation:
/**
*ManyToMany指明多对多关系
*JoinTable生成中间表,name指定中间表的名字,joinColumns
*指定参考这张表的主键的中间表的属性名,inverseJoinColumns
*指定参考相关表的主键的中间表的属性名。
*
*/
@ManyToMany
@JoinTable(name="t_t",joinColumns={
@JoinColumn(name="t_id")
},inverseJoinColumns={@JoinColumn(name="s_id")})
@ManyToMany(mappedBy="students")
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="com.xie.hibernate.modal" >
<class name="Teacher" table="t_teacher">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<set name="students" table="t_s">
<key column="t_id"></key>
<many-to-many column="s_id" class="Student"/>
</set>
</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="com.xie.hibernate.modal" >
<class name="Student" table="t_student">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<set name="teachers" table="t_s" inverse="true">
<key column="s_id"/>
<many-to-many column="t_id" class="Teacher"/>
</set>
</class>
</hibernate-mapping>