1)类中的每一个主键属性都对应到数据库表中的每一个主键列。Hibernate要求联合主键的实体实现Serializable接口,并且重写hashCode与equals方法,原因在于使用get或load方法的时候需要先构建出来该实体的对象,并且将查询依据(联合主键)设置进去,然后作为get或者load方法的第二个参数传进去即可。重写这两个方法的原因在于Hibernate要根据数据库的联合主键来判断某两行记录是否是一样的,如果一样那么就认为是同一个对象,如果不一样,那么就认为是不同对象。
2)将主键所对应的属性提出一个类(称之为主键类)
<?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 > <class name="com.chm.test.Student" table="student"> <composite-id> <key-property name="idcard" column="idcard" type="integer"></key-property> <key-property name="name" column="name" type="string"></key-property> </composite-id> <property name="age" column="age" type="integer"></property> </hibernate-mapping>也可以将主键属性单独提取出来一个类,并在composite-id标签上指明即可.
1.使用component标签
<class name="com.chm.test.Student" table="student"> <id name="id" column="id" type="integer"> <generator class="increment"></generator> </id> <property name="name" column="name" type="string"></property> <property name="age" column="age" type="integer"></property> <component name="addres" class="com.chm.test.Address"> <property name="homeAddr" column="homeAddr" type="string"></property> <property name="schoolAddr" column="schoolAddr" type="string"></property> </component> </class>所有属性存在一张表中.生成sql语句为:create table student (id integer not null, name varchar(255), age integer, homeAddr varchar(255), schoolAddr varchar(255), primary key (id));
2.使用composite-element标签(了解)
<class name="com.chm.test.Student" table="student"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="name" column="name" type="string"></property> <property name="age" column="age" type="integer"></property> <set name="contacts" table="contact"> <key column="student_id"></key> <composite-element class="com.chm.test.Contact"> <property name="method" column="method" type="string"></property> <property name="address" column="address" type="string"></property> </composite-element> </set> </class>
生成两张表,生成的sql为:create table contact (student_id varchar(255) not null, method varchar(255), address varchar(255));
create table student (id varchar(255) not null, name varchar(255), age integer, primary key (id));
alter table contact add index FK38B724203B7CAEB1 (student_id), add constraint FK38B724203B7CAEB1 foreign key (student_id) references student (id);
Student类和Teacher类都继承Person,Person类中有公共属性id和name,Student中有单独属性cardid,Teacher类中有单独属性salary
1)每个子类一张表
Student.hbm.xml
<class name="com.chm.test.Student" table="student"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="name" column="name" type="string"></property> <property name="cardid" column="cardid" type="string"></property> </class>Teacher.hbm.xml
<class name="com.chm.test.Teacher" table="teacher"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="name" column="name" type="string"></property> <property name="salary" column="salary" type="int"></property> </class>注意:多态查询的时候HQL为 "from com.chm.test.Person",因为Person没有对应的hbm文件,所以 类要带上包名.
2)一张表存储继承体系中所有类的信息
Person.hbm.xml文件:
<class name="com.chm.test.Person" table="person"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <discriminator column="personType" type="string"></discriminator> <property name="name" column="name" type="string"></property> <subclass name="com.chm.test.Student" discriminator-value="student"> <property name="cardid" column="cardid" type="string"></property> </subclass> <subclass name="com.chm.test.Teacher" discriminator-value="teacher"> <property name="salary" column="salary" type="string"></property> </subclass> </class>
生成表的sql语句为:create table person (id varchar(255) not null, personType varchar(255) not null, name varchar(255), cardid varchar(255), salary varchar(255), primary key (id));
3)每个类一张表,公共信息放在父类表中
Person.hbm.xml文件:
<class name="com.chm.test.Person" table="person"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="name" column="name" type="string"></property> <joined-subclass name="com.chm.test.Student" table="student"> <key column="id"></key> <property name="cardid" column="cardid" type="string"></property> </joined-subclass> <joined-subclass name="com.chm.test.Teacher" table="teacher"> <key column="id"></key> <property name="salary" column="salary" type="string"></property> </joined-subclass> </class>
create table person (id varchar(255) not null, name varchar(255), primary key (id));
[hibernatetool] create table student (id varchar(255) not null, cardid varchar(255), primary key (id));
[hibernatetool] create table teacher (id varchar(255) not null, salary varchar(255), primary key (id));
[hibernatetool] alter table student add index FK8FFE823BAB5FC6B9 (id), add constraint FK8FFE823BAB5FC6B9 foreign key (id) references person (id);
[hibernatetool] alter table teacher add index FKAA31CBE2AB5FC6B9 (id), add constraint FKAA31CBE2AB5FC6B9 foreign key (id) references person (id);