Hibernate的继承关系(1)

发表于:2007-06-11来源:作者:点击数: 标签:
一、每个子类对应一个数据表(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

一、每个子类对应一个数据表(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;

}

}

子类分别实现它,并添加额外的属性和相应gettter和setter方法。如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">root</property>

<property name="connection.url">

jdbc:mysql://localhost:3306/sample

</property>

<property name="dialect">

org.hibernate.dialect.MySQLDialect

</property>

<property name="connection.password">12345678</property>

<property name="connection.driver_class">

com.mysql.jdbc.Driver

</property>

<property name="show_sql">true</property>

<property name="current_session_context_class">thread</property>



<mapping resource="powerwind/bean/Student.hbm.xml" />

<mapping resource="powerwind/bean/Teacher.hbm.xml" />



</session-factory>

</hibernate-configuration>


共4页: 1 [2] [3] [4] 下一页

原文转自:http://www.ltesting.net

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
...