<?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.one.Animal" table="Animal" dynamic-update="false" dynamic-insert="false" select-before-update="false" optimistic-lock="version" discriminator-value="Animal" > <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-Animal.xml containing the additional parameters and place it in your merge dir. --> </generator> </id> <discriminator column="ANIMAL_TYPE" type="string" length="10" /> <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-Animal.xml containing the additional properties and place it in your merge dir. --> <subclass name="javamxj.inheritance.one.Cat" dynamic-update="false" dynamic-insert="false" discriminator-value="Cat" > <property name="furColor" type="java.lang.String" update="true" insert="true" access="property" column="furColor" length="24" /> <!-- To add non XDoclet property mappings, create a file named hibernate-properties-Cat.xmlcontaining the additional properties and place it in your merge dir. --> </subclass> <subclass name="javamxj.inheritance.one.Dog" dynamic-update="false" dynamic-insert="false" discriminator-value="Dog" > <property name="category" type="java.lang.String" update="true" insert="true" access="property" column="category" length="24" /> <!-- To add non XDoclet property mappings, create a file named hibernate-properties-Dog.xmlcontaining the additional properties and place it in your merge dir. --> </subclass> </class></hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0" encoding="GBK"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"><!-- Generated file - Do not edit! --><hibernate-configuration><!-- a SessionFactory instance listed as /jndi/name --><session-factory><!-- properties --><property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property><property name="show_sql">true</property><property name="use_outer_join">false</property><property name="connection.username">root</property><property name="connection.password">javamxj</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/HibernateTest?useUnicode=true&characterEncoding=GBK</property><!-- mapping files --><mapping resource="javamxj/inheritance/one/Animal.hbm.xml"/></session-factory></hibernate-configuration>
· 雙擊“schemaexport”任務,在項目根目錄下,會產生一個“schema-export.sql”檔案。schema-export.sqldrop table if exists Animaldrop table if exists hibernate_unique_keycreate table Animal ( ID bigint not null, ANIMAL_TYPE varchar(10) not null, name varchar(24), furColor varchar(24), category varchar(24), primary key (ID))create table hibernate_unique_key ( next_hi integer )insert into hibernate_unique_key values ( 0 ) · 切換到資料庫中,會發現已經自動產生了資料表animal(表hibernate_unique_key是由於採用hilo主鍵策略產生的)。 5. 測試程式 · 好了,在包javamxj.inheritance.one下建立一個Demo.java類,很簡單,前半部分是添加資料,後半部分是簡單的測試。
Demo.java