09-05
26
Hibernate映射文件中索引及约束的使用
作者:Java伴侣 日期:2009-05-26
1、添加索引:在一对多的关系中,在多的一方会产生一个外键,这个外键没有自动添加索引,当存在从一的一端产生对多的一端的查询时,有可能会在多的一端造成全表查询问题,数据量巨大时会产生严重的性能问题。可以在多一端的外键上添加索引(index="user_group_id_idx")来解决这个问题。例如:
<?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>
<class name="com.bjsxt.hibernate.User" table="t_user">
<id name="id">
<generator class="sequence">
<param name="sequence">user_id_seq</param>
</generator>
</id>
<property name="name"></property>
<many-to-one name="group" column="group_id" index="user_group_id_idx"></many-to-one>
</class>
</hibernate-mapping>
发出的SQL语句为:
create index user_group_id_idx on t_user (group_id)
2、添加约束:id会产生主键约束,同时会建立索引;key会产生外键,但默认不会建立索引;在class或set上通过使用check关键字来产生check约束(例如:<set name="emailAddresses" table="t_email" check="email_address like '%@%'">);单个字段的唯一键通过(unique="true")设置,多个字段联合唯一通过在需要联合的字段上添加(unique-key="name")来实现,其中对联合唯一索引的unique-key的值必须相同;通过not-null="true"来设置字段的非空约束。
<?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>
<class name="events.Person" table="t_person">
<id name="id" column="person_id">
<generator class="sequence">
<param name="sequence">person_id_seq</param>
</generator>
</id>
<property name="age"></property>
<property name="firstName" unique-key="name"></property>
<property name="lastName" unique-key="name"></property>
<set name="emailAddresses" table="t_email" check="email_address like '%@%'">
<key column="person_id"></key>
<element column="email_address" type="string"></element>
</set>
<set name="events" table="t_person_event">
<key column="person_id"></key>
<many-to-many class="events.Event" column="event_id"></many-to-many>
</set>
</class>
</hibernate-mapping>
产生的SQL语句是:
drop table t_email cascade constraints
drop table t_event cascade constraints
drop table t_person cascade constraints
drop table t_person_event cascade constraints
drop sequence event_id_seq
drop sequence person_id_seq
create table t_email (
person_id number(19,0) not null,
email_address varchar2(255 char),
check (email_address like '%@%')
)
create table t_event (
event_id number(19,0) not null,
title varchar2(255 char),
event_date date,
primary key (event_id)
)
create table t_person (
person_id number(19,0) not null,
age number(10,0),
firstName varchar2(255 char),
lastName varchar2(255 char),
primary key (person_id),
unique (firstName, lastName)
)
create table t_person_event (
event_id number(19,0) not null,
person_id number(19,0) not null,
primary key (person_id, event_id)
)
alter table t_email
add constraint FKA03188117708282F
foreign key (person_id)
references t_person
alter table t_person_event
add constraint FKC7F6A31B7708282F
foreign key (person_id)
references t_person
alter table t_person_event
add constraint FKC7F6A31BF96D1A45
foreign key (event_id)
references t_event
create sequence event_id_seq
create sequence person_id_seq
<?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>
<class name="com.bjsxt.hibernate.User" table="t_user">
<id name="id">
<generator class="sequence">
<param name="sequence">user_id_seq</param>
</generator>
</id>
<property name="name"></property>
<many-to-one name="group" column="group_id" index="user_group_id_idx"></many-to-one>
</class>
</hibernate-mapping>
发出的SQL语句为:
create index user_group_id_idx on t_user (group_id)
2、添加约束:id会产生主键约束,同时会建立索引;key会产生外键,但默认不会建立索引;在class或set上通过使用check关键字来产生check约束(例如:<set name="emailAddresses" table="t_email" check="email_address like '%@%'">);单个字段的唯一键通过(unique="true")设置,多个字段联合唯一通过在需要联合的字段上添加(unique-key="name")来实现,其中对联合唯一索引的unique-key的值必须相同;通过not-null="true"来设置字段的非空约束。
<?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>
<class name="events.Person" table="t_person">
<id name="id" column="person_id">
<generator class="sequence">
<param name="sequence">person_id_seq</param>
</generator>
</id>
<property name="age"></property>
<property name="firstName" unique-key="name"></property>
<property name="lastName" unique-key="name"></property>
<set name="emailAddresses" table="t_email" check="email_address like '%@%'">
<key column="person_id"></key>
<element column="email_address" type="string"></element>
</set>
<set name="events" table="t_person_event">
<key column="person_id"></key>
<many-to-many class="events.Event" column="event_id"></many-to-many>
</set>
</class>
</hibernate-mapping>
产生的SQL语句是:
drop table t_email cascade constraints
drop table t_event cascade constraints
drop table t_person cascade constraints
drop table t_person_event cascade constraints
drop sequence event_id_seq
drop sequence person_id_seq
create table t_email (
person_id number(19,0) not null,
email_address varchar2(255 char),
check (email_address like '%@%')
)
create table t_event (
event_id number(19,0) not null,
title varchar2(255 char),
event_date date,
primary key (event_id)
)
create table t_person (
person_id number(19,0) not null,
age number(10,0),
firstName varchar2(255 char),
lastName varchar2(255 char),
primary key (person_id),
unique (firstName, lastName)
)
create table t_person_event (
event_id number(19,0) not null,
person_id number(19,0) not null,
primary key (person_id, event_id)
)
alter table t_email
add constraint FKA03188117708282F
foreign key (person_id)
references t_person
alter table t_person_event
add constraint FKC7F6A31B7708282F
foreign key (person_id)
references t_person
alter table t_person_event
add constraint FKC7F6A31BF96D1A45
foreign key (event_id)
references t_event
create sequence event_id_seq
create sequence person_id_seq
评论: 0 | 引用: 0 | 查看次数: 853
发表评论