Assuming there is a commodity management program.

Only the following types of goods: 1, book. 2, DVD

Have the following common attributes: number, title, price

The book contains its own separate attributes: the number of pages

DVD contains its own separate attributes: the length of time

Class is designed to:

Three categories: product category, sub-category of book categories, sub-class category DVD

One or two tables
Table 2: Book List and DVD form, through a separate operation to complete the book and DVD management operations.

Design and the father of the class and subclass has nothing to do with the operation through a separate sub-class to complete the function.

Second, Table 3
Commodity table: number, title, price

Book Table: Product Number, Page

DVD Table: Number of goods, the length of time

1, the establishment of a database
DROP TABLE item;

DROP TABLE book;

DROP TABLE dvd;

CREATE TABLE item (

id int primary key auto_increment,

title varchar (20) not null,

price double not null

);

CREATE TABLE book (

id int primary key,

page int not null

);

CREATE TABLE dvd (

id int primary key,

time_length double not null

);

2, to generate maps
Only need to generate the mapping table item, the other two types of manual preparation, and preparation of the book and dvd do not need to join the mapping document.

package org.liky.pojo;

public class Item implements java.io.Serializable (

/ / Fields

private Integer id;

private String title;

private Double price;

/ / Constructors

public Item () (

)

public Item (String title, Double price) (

this.title = title;

this.price = price;

)

/ / Property accessors

public Integer getId () (

return this.id;

)

public void setId (Integer id) (

this.id = id;

)

public String getTitle () (

return this.title;

)

public void setTitle (String title) (

this.title = title;

)

public Double getPrice () (

return this.price;

)

public void setPrice (Double price) (

this.price = price;

)

)

Book category of the establishment

package org.liky.pojo;

public class Book extends Item (

private int page;

public int getPage () (

return page;

)

public void setPage (int page) (

this.page = page;

)

)

The establishment of DVD category

package org.liky.pojo;

public class Dvd extends Item (

private double timeLength;

public double getTimeLength () (

return timeLength;

)

public void setTimeLength (double timeLength) (

this.timeLength = timeLength;

)

3, modified mapping file
Modified mapping file, by adding sub-class configuration

<? xml version = "1.0" encoding = "utf-8"?>

<! DOCTYPE hibernate-mapping PUBLIC "- / / Hibernate / Hibernate Mapping DTD 3.0 / / EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<! --

Mapping file autogenerated by MyEclipse Persistence Tools

->

<hibernate-mapping>

<class name="org.liky.pojo.Item" table="item" catalog="testdb">

<id name="id" type="java.lang.Integer">

<column name="id" />

<generator />

</ id>

<property name="title" type="java.lang.String">

<column name="title" length="20" not-null="true" />

</ property>

<property name="price" type="java.lang.Double">

<column name = "price" precision = "22" scale = "0"

not-null = "true" />

</ property>

<! - Item category that contains a sub-class for the Book, Book-type with the corresponding relation table book ->

<joined-subclass name="org.liky.pojo.Book" table="book">

<! - In book form through the field id associated with the item table relations ->

<key column="id"> </ key>

<! - Said in the Book category and the page attribute of the page table book are the relationship between fields ->

<property name="page" type="java.lang.Integer">

<column name="page" not-null="true"> </ column>

</ property>

</ joined-subclass>

<joined-subclass name="org.liky.pojo.Dvd" table="dvd">

<key column="id"> </ key>

<property name="timeLength" type="java.lang.Double">

<column name="time_length" not-null="true"> </ column>

</ property>

</ joined-subclass>

</ class>

</ hibernate-mapping>

4, the preparation of the background code
Item only the establishment of the DAO interface type operation

5, test
/ / Insert book

/ / Book book = new Book ();

/ / Book.setTitle ( "Harry Potter");

/ / Book.setPrice (32.5);

/ / Book.setPage (500);

/ /

/ / Try (

/ / Itemdao.doCreate (book);

/ /) Catch (Exception e) (

/ / / / TODO Auto-generated catch block

/ / E.printStackTrace ();

/ /)

/ / Insert the DVD

Dvd dvd = new Dvd ();

dvd.setTitle ( "Terminator");

dvd.setPrice (5.0);

dvd.setTimeLength (2.0);

try (

itemdao.doCreate (dvd);

) Catch (Exception e) (

/ / TODO Auto-generated catch block

e.printStackTrace ();

)

try (

List all = itemdao.findAll (1, 10);

Iterator iter = all.iterator ();

while (iter.hasNext ()) (

Item item = (Item) iter.next ();

if (item instanceof Book) (

Book book = (Book) item;

System.out.println (book.getTitle () + "->" + book.getPage ());

) Else if (item instanceof Dvd) (

Dvd dvd = (Dvd) item;

System.out.println (dvd.getTitle () + "->" + dvd.getTimeLength ());

)

)

) Catch (Exception e) (

/ / TODO Auto-generated catch block

e.printStackTrace ();

)

Query performance is very low, so do not use.

Third, a table
Table 1: there is only one Item List

Item table: number, title, price, number of pages, the length of time to mark the spaces

1, the establishment of table
DROP TABLE item;

DROP TABLE book;

DROP TABLE dvd;

CREATE TABLE item (

id int primary key auto_increment,

title varchar (20) not null,

price double not null,

page int,

time_length double,

flag varchar (20) not null

);

The use of varchar type flag will automatically be processed by Hibernate

2, generated type mapping

3, modified mapping file
<? xml version = "1.0" encoding = "utf-8"?>

<! DOCTYPE hibernate-mapping PUBLIC "- / / Hibernate / Hibernate Mapping DTD 3.0 / / EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<! --

Mapping file autogenerated by MyEclipse Persistence Tools

->

<hibernate-mapping>

<class name="org.liky.pojo.Item" table="item" catalog="testdb">

<id name="id" type="java.lang.Integer">

<column name="id" />

<generator />

</ id>

<! - The definition of the value of flag to mark the field for the corresponding bit flag, the type of processing in accordance with the String type ->

<discriminator column="flag"> </ discriminator>

<property name="title" type="java.lang.String">

<column name="title" length="20" not-null="true" />

</ property>

<property name="price" type="java.lang.Double">

<column name = "price" precision = "22" scale = "0"

not-null = "true" />

</ property>

<! - Item category that contains a sub-class Book, if the Book operation, the flag value of the book ->

<subclass name="org.liky.pojo.Book" discriminator-value="book">

<property name="page" type="java.lang.Integer">

<column name="page"> </ column>

</ property>

</ subclass>

<! - Item category that contains a sub-class Dvd, if the operation Dvd, the flag value of the dvd ->

<subclass name="org.liky.pojo.Dvd" discriminator-value="dvd">

<property name="timeLength" type="java.lang.Double">

<column name="time_length"> </ column>

</ property>

</ subclass>

</ class>

</ hibernate-mapping>

4, test
Any other code will not need to modify the background, Hibernate will handle it on its own.

/ / Insert book

/ / Book book = new Book ();

/ / Book.setTitle ( "Harry Potter");

/ / Book.setPrice (32.5);

/ / Book.setPage (500);

/ /

/ / Try (

/ / Itemdao.doCreate (book);

/ /) Catch (Exception e) (

/ / / / TODO Auto-generated catch block

/ / E.printStackTrace ();

/ /)

/ / Insert the DVD

/ / Dvd dvd = new Dvd ();

/ / Dvd.setTitle ( "Terminator");

/ / Dvd.setPrice (5.0);

/ / Dvd.setTimeLength (2.0);

/ /

/ / Try (

/ / Itemdao.doCreate (dvd);

/ /) Catch (Exception e) (

/ / / / TODO Auto-generated catch block

/ / E.printStackTrace ();

/ /)

try (

List all = itemdao.findAll (1, 10);

Iterator iter = all.iterator ();

while (iter.hasNext ()) (

Item item = (Item) iter.next ();

if (item instanceof Book) (

Book book = (Book) item;

System.out.println (book.getTitle () + "->"

+ Book.getPage ());

) Else if (item instanceof Dvd) (

Dvd dvd = (Dvd) item;

System.out.println (dvd.getTitle () + "->"

+ Dvd.getTimeLength ());

)

)

) Catch (Exception e) (

/ / TODO Auto-generated catch block

e.printStackTrace ();

)

If you want to use a single table is bound to use this approach.