ITPub博客

首页 > Linux操作系统 > Linux操作系统 > [转载]Hibernate的继承关系

[转载]Hibernate的继承关系

原创 Linux操作系统 作者:dinner1007 时间:2019-05-21 21:51:06 0 删除 编辑
Hibernate的继承关系
一、每个子类对应一个数据表(Table per concrete class)
学生表
create table `sample`.`student`(
`id` bigint not null auto_increment,
`name` varchar(20) default '' not null,
`score` float,
primary key (`id`)
);
create unique index `PRIMARY` on `sample`.`student`(`id`);
教师表
create table `sample`.`teacher`(
`id` bigint not null auto_increment,
`name` varchar(20) default '' not null,
`salary` float(6,2),
primary key (`id`)
);
create unique index `PRIMARY` on `sample`.`teacher`(`id`);
Person抽象基类
public abstract class Person implements java.io.Serializable {
private Long id;
private String name;
/**defaultconstructor*/
public Person() {
}
public Long getId() {
returnthis.id;
}
publicvoid setId(Long id) {
this.id = id;
}
public String getName() {
returnthis.name;
}
publicvoid setName(String name) {
this.name = name;
}
}
子类分别实现它,并添加额外的属性和相应getttersetter方法。
Student类:
public class Student extends Person {
private Float score;
public Student() {
super();
}
public Float getScore() {
returnscore;
}
publicvoid setScore(Float score) {
this.score = score;
}
}
hibernate.cfg.xml
xml version='1.0' encoding='UTF-8'?>
DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.username">rootproperty>
<property name="connection.url">
jdbc:mysql://localhost:3306/sample
property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
property>
<property name="connection.password">12345678property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
property>
<property name="show_sql">trueproperty>
<property name="current_session_context_class">threadproperty>
<mapping resource="powerwind/bean/Student.hbm.xml" />
<mapping resource="powerwind/bean/Teacher.hbm.xml" />
session-factory>
hibernate-configuration>
由于Person抽象类没有对应数据库的表,也没有对应的映射文件,在HQL查询中也就不支持多态查询。感觉上,Person抽象类的作用只是减少JAVA代码的编写而已。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-131331/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-08-23

  • 博文量
    1396
  • 访问量
    1027023