Eclipse快速上手Hibernate--4. 继承映射(3)
2008-02-23 09:50:49来源:互联网 阅读 ()
Person.java
/* * Hibernate - 继承映射(每个具体类一个表) * 创建日期 2005-4-9 * @author javamxj(分享java快乐) * @link Blog: htpp://javamxj.mblogger.cn * htpp://blog.csdn.net/javamxj/ */package javamxj.inheritance.three;public abstract class Person { private Long id; private String name; /** * @hibernate.id * column="ID" * generator-class="hilo" * unsaved-value="null" */ public Long getId() { return id; } public void setId(Long id) { this.id = id; } /** * @hibernate.property * length = "24" */ public String getName() { return name; } public void setName(String name) { this.name = name; }}
· 子类Student.javaStudent.java
package javamxj.inheritance.three;/** * @hibernate.class * table="Student" */public class Student extends Person { private String studentNumber; /** * @hibernate.property * length = "24" */ public String getStudentNumber() { return studentNumber; } public void setStudentNumber(String studentNumber) { this.studentNumber = studentNumber; }}
· 子类Professor.javaProfessor.java
package javamxj.inheritance.three;/** * @hibernate.class * table="Professor" */public class Professor extends Person { private int salary; /** * @hibernate.property */ public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; }}
· 这两个子类都很简单,跟平常的写法相同,只是简单的映射成一个表。 · 好了,这时整个项目的结构如下: 3. 运行任务 · 双击“generate-hbm”任务,会发现在包中多了Professor.hbm.XML和Student.hbm.xml两个文件。如果没有,按F5键刷新一下(这里建议打开Eclipse的“首选项”对话框,在“工作台”中勾选“自动刷新工作空间”和“在构建之前自动保存”这两项,这样以后不用每次都刷新了)。· 这是通过两个子类所生成的映射文件,注意没有生成父类的映射文件,而且这两个子类的映射文件都已经包含了父类的属性。
Professor.hbm.xml
<?xml version="1.0" encoding="GBK"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping> <class name="javamxj.inheritance.three.Professor" table="Professor" dynamic-update="false" dynamic-insert="false" select-before-update="false" optimistic-lock="version" > <id name="id" column="ID" type="java.lang.Long" unsaved-value="null" > <generator class="hilo"> <!-- To add non XDoclet generator parameters, create a file named hibernate-generator-params-Professor.xml containing the additional parameters and place it in your merge dir. --> </generator> </id> <property name="salary" type="int" update="true" insert="true" Access="property" column="salary" /> <property name="name" type="java.lang.String" update="true" insert="true" access="property" column="name" length="24" /> <!-- To add non XDoclet property mappings, create a file named hibernate-properties-Professor.xml containing the additional properties and place it in your merge dir. --> </class></hibernate-mapping>
Student.hbm.xml
<?xml version="1.0" encoding="GBK"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping> <class name="javamxj.inheritance.three.Student" table="Student" dynamic-update="false" dynamic-insert="false" select-before-update="false" optimistic-lock="version" > <id name="id" column="ID" type="java.lang.Long" unsaved-value="null" > <generator class="hilo"> <!-- To add non XDoclet generator parameters, create a file named hibernate-generator-params-Student.xml containing the additional parameters and place it in your merge dir. --> </generator> </id> <property name="studentNumber" type="java.lang.String" update="true" insert="true" access="property" column="studentNumber" length="24" /> <property name="name" type="java.lang.String" update="true" insert="true" access="property" column="name" length="24" /> <!-- To add non XDoclet property mappings, create a file named hibernate-properties-Student.xml containing the additional properties and place it in your merge dir. --> </class></hibernate-mapping>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Eclipse快速上手Hibernate--4. 继承映射(2) 2008-02-23
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash