一、一Hibernate实体映射(每个具体类映射成一张表)
(1)实体类与表间的关系
Animal t_animal t_pig t_bird
id:int id:int id:int id:int
name:String name:varchar name:varchar name:varchar
sex:boolean sex:big sex:big sex:big
Ping Bird weight:int height:int
weight:int height:int
(2)、创建表语句
CREATE TABLE `animal` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`sex` bit(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `t_bird` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`sex` bit(1) DEFAULT NULL,
`height` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `t_pig` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`sex` bit(1) DEFAULT NULL,
`weight` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into t_pig (name, sex, weight, id) values ('猪猪', true, 100, 1);
Hibernate: insert into t_bird3 (name, sex, height, id) values ('鸟鸟', false, 50, 2);
(3)、创建实体类与映射文件
Animal.java
public class Animal {
private int id;
private String name;
private boolean sex;
public Animal(){
}
//省略set、get方法
}
Bird.java
public class Bird extends Animal {
private int height;
public Bird(){
}
//省略set、get方法
}
Pig.java
public class Pig extends Animal1 {
private int weight;
public Pig1()