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 1
start with 1
nomaxvalue nominvalue nocache;

CREATE  TRIGGER book_trigger BEFORE
INSERT ON booktbl FOR EACH ROW WHEN(new.id is null)
begin
select 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_seq
increment by 1
start with 1
nomaxvalue nominvalue nocache;

CREATE TRIGGER clo_trigger
before
INSERT ON clothestbl 
FOR EACH ROW 
WHEN(new.id is null)
begin
select 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"/>

posted @ 2016-09-24 16:19  式微胡不归  阅读(271)  评论(0编辑  收藏  举报