首页 > 代码库 > Hibernate 继承表结构

Hibernate 继承表结构

有Product , Book ,Clothes三张表

Product:id,name

Book:  id ,name,pageCount

Clothes: id ,name ,size

 

创建三张表

产品表create table product(    id number(2) primary key,    name varchar2(10));书表create table booktbl(    id number(2) ,    name    varchar2(10),    pageCount    number(3),    foreign key(id) references product(id));create sequence book_seq increment by 1start with 1nomaxvalue nominvalue nocache;CREATE  TRIGGER book_trigger BEFOREINSERT ON booktbl FOR EACH ROW WHEN(new.id is null)beginselect book_seq.nextval into:new.id from dual;end;drop table  booktbl;drop sequence book_seq;brop trigger book_trigger;服装表create table clothestbl(    id number(2) references product(id),    name varchar2(10),    closize number(5));create sequence clothes_seqincrement by 1start with 1nomaxvalue nominvalue nocache;CREATE TRIGGER clo_triggerbeforeINSERT ON clothestbl FOR EACH ROW WHEN(new.id is null)beginselect clothes_seq.nextval into:new.id from dual;end;drop table  clothestbl;drop sequence clothes_seq;brop trigger clo_trigger;

Product.hlm.xml

  <class name="com.amaker.extendmodel.Product" table="Product">           <id name="id">               <generator class="native"></generator>           </id>           <property name="name"></property>                       <joined-subclass name="com.amaker.extendmodel.Book" table="booktbl">                        <key column="id"></key>               <property name="pageCount"></property>           </joined-subclass>                        <joined-subclass name="com.amaker.extendmodel.Clothes" table="clothestbl">                        <key column="id"></key>               <property name="size" column="closize"></property>           </joined-subclass>    </class> 

<mapping resource="com/amaker/extendmodel/Product.hbm.xml"/>

Hibernate 继承表结构