一
學生表
create table `sample`
`id` bigint not null auto_increment
`name` varchar(
`score` float
primary key (`id`)
);
create unique index `PRIMARY` on `sample`
教師表
create table `sample`
`id` bigint not null auto_increment
`name` varchar(
`salary` float(
primary key (`id`)
);
create unique index `PRIMARY` on `sample`
Person抽象基類
public abstract class Person implements java
private Long id;
private String name;
/**defaultconstructor*/
public Person() {
}
public Long getId() {
returnthis
}
publicvoid setId(Long id) {
this
}
public String getName() {
returnthis
}
publicvoid setName(String name) {
this
}
}
子類分別實現它
如Student類
public class Student extends Person {
private Float score;
public Student() {
super();
}
public Float getScore() {
returnscore;
}
publicvoid setScore(Float score) {
this
}
}
hibernate
<?xml version=
<!DOCTYPE hibernate
<hibernate
<session
<property name=
<property name=
jdbc:mysql://localhost:
</property>
<property name=
org
</property>
<property name=
<property name=
com
</property>
<property name=
<property name=
<mapping resource=
<mapping resource=
</session
</hibernate
由於Person抽象類沒有對應數據庫的表
二
人員表
create table `sample`
`id` bigint not null auto_increment
`name` varchar(
primary key (`id`)
);
學生表
create table `sample`
`id` bigint default
`score` float
primarykey (`id`)
);
教師表
create table `sample`
`id` bigint default
`salary` float
primary key (`id`)
);
兩個子類的實現和前一種完全一樣
<hibernate
<class name=
catalog=
<id name=
<column name=
<generator class=
</id>
<property name=
<column name=
</property>
<joined
table=
<key column=
<property name=
<column name=
</property>
</joined
<joined
table=
<key column=
<property name=
<column name=
</property>
</joined
</class>
</hibernate
這種方式是支持多態查詢的
多態查詢語句
三
人員表
create table `sample`
`id` bigint not null auto_increment
`name` varchar(
`score` float
`salary` float
`type` char(
primary key (`id`)
);
Person
<hibernate
<class name=
catalog=
<id name=
<column name=
<generator class=
</id>
<discriminator column=
<property name=
<column name=
</property>
<subclass name=
discriminator
<property name=
</subclass>
<subclass name=
discriminator
<property name=
</subclass>
</class>
</hibernate
優點是單表查詢
附加
人員表
create table `sample`
`id` bigint not null auto_increment
`name` varchar(
`email` varchar(
`phone` varchar(
primary key (`id`)
);
Person類
publicclass Person implements java
private Long id;
private String name;
private Contact contact;
}
Contact類
publicclass Contact implements java
private String email;
private String phone;
}
注
Person
<hibernate
<class name=
catalog=
<id name=
<column name=
<generator class=
</id>
<property name=
<component name=
<property name=
<property name=
</component>
</class>
</hibernate
這樣的細粒度
假如Contact類包含的字段是重量級的數據
create table `sample`
`id` bigint not null auto_increment
`name` varchar(
`gender` varchar(
`email` varchar(
`phone` varchar(
primary key (`id`)
);
首先定義個基類BasePerson
publicclass BasePerson {
private Long id;
private String name;
}
Person類繼承BasePerson類
PersonDetail類繼承Person類
Person
<class name=
catalog=
<id name=
<column name=
<generator class=
</id>
<property name=
<property name=
</class>
PersonDetail
<class name=
catalog=
<id name=
<column name=
<generator class=
</id>
<property name=
<property name=
<property name=
<property name=
</class>
除了polymorphism=
Query query=getSession()
Query query=getSession()
上面兩句中
注
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28608.html