Eclipse quickly hibernate--4. Inheritance Mappings (2)

Source: Internet
Author: User
Tags date object generator inheritance net one table version access
Inherit the last article, "Eclipse Quick-start hibernate--4." Inheritance mapping (1) has discussed the strategy for each Class hierarchy table (table per class hierarchy), which is mainly about the strategy for each subclass table (table per subclass). Some of the repeated parts are not mentioned here, please refer to the previous article.  1. Create Project ·   Continue to follow the Java project built in the previous article: Inheritancemapping.  2. Writing class documents · Create a new class, package Name: Javamxj.inheritance.two, class name: Animal. You then add variables to the generated code, and then use "Generate Getter and Setter", specifically with Eclipse fast-start hibernate--1.  Getting started with the same way as editing User.java in articles. · This class is a parent class and simply generates a simple table.
Vehicle.java

* * Hibernate-Inheritance mapping (one table per subclass) * Create date 2005-4-9 * @author javamxj (share java Fun) * @link blog:htpp://javamxj.mblogger.cn * HTPP ://blog.csdn.net/javamxj/*/package javamxj.inheritance.two;/** * @hibernate. Class */public class Vehicle {private Long Id;private String name;/** * @hibernate. ID * column= "id" * generator-class= "Hilo" * unsaved-value= "null" */public Long get ID () {return ID;} public void SetId (Long id) {this.id = ID;} /** * @hibernate. Property * length = ' */public ' String getName () {return name;} public void SetName (String name) {this.name = name;}}
· Sub Class Car.javaCar.java

Package javamxj.inheritance.two;/** * @hibernate. Joined-subclass * @hibernate. Joined-subclass-key * column= "id" * * public class Car extends Vehicle {private String seat;/** * @hibernate. Property * Column = "Passenger" * length = "All" */public S Tring Getseat () {return seat;} public void Setseat (String seat) {this.seat = seat;}}

· Sub Class Truck.javaTruck.java

Package javamxj.inheritance.two;/** * @hibernate. Joined-subclass * @hibernate. Joined-subclass-key * column= "id" * * public class Truck extends Vehicle {private String load;/** * @hibernate. Property * column = "Load" * length = "*/public" String Getload () {return load;} public void Setload (String load) {this.load = load;}}

· These two subclasses are very simple, and note the Hibernate.joined-subclass tags that are added. · Okay, now the whole project is structured as follows: 3.  Run Tasks · Double-click the "GENERATE-HBM" task to find a Vehicle.hbm.xml file in the package. If not, press F5 to refresh (it is recommended that you open the Eclipse's Preferences dialog box, and in the Workbench, check the "Automatically refresh Workspace" and "save automatically before building", so that you don't have to refresh it each time).
Vehicle.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 ">
· The point is to look at the "joined-subclass" label. ·  A mapping file information is automatically added to the Hibernate.cfg.xml file: <mapping resource= "Javamxj/inheritance/two/vehicle.hbm.xml"/> Run MySQL first, and then double-click the "Schemaexport" task, and the "Schema-export.sql" file is updated under the project root. When you open this file, you will find that some of the following statements have been added.   CREATE TABLE car (ID bigint not NULL, passenger varchar (in), primary key (ID)) CREATE TABLE truck (ID bigint not NULL,   Load varchar, primary key (ID) CREATE TABLE Vehicle (ID bigint not NULL, name varchar (in), primary key (ID)) · When you switch to the database, you'll find that you've automatically generated the datasheet car, truck, and Vehicle. ·  Comparing the datasheet to the mapping file Vehicle.hbm.xml a better understanding of the strategy of a table for each subclass.  4. Test Procedures · OK, a new Demo.java class under the package Javamxj.inheritance.two is very simple, the first half is to add the data, the second half is a simple test.
Demo.java

* * Hibernate-Inheritance mapping (one table per subclass) * Create date 2005-4-9 * @author javamxj (share java Fun) * @link blog:htpp://javamxj.mblogger.cn * HTPP ://blog.csdn.net/javamxj/*/package javamxj.inheritance.two;import java.util.iterator;import java.util.List;import Net.sf.hibernate.hibernateexception;import Net.sf.hibernate.session;import net.sf.hibernate.SessionFactory; Import Net.sf.hibernate.transaction;import Net.sf.hibernate.cfg.configuration;public class Demo {public static void Main (string[] args) {try {new Demo ();} catch (Hibernateexception He) {he.printstacktrace ();}} Public Demo () throws hibernateexception {sessionfactory sf = new Configuration (). Configure (). Buildsessionfactory (); Session Sess = Sf.opensession (); Transaction tx = null;try {tx = Sess.begintransaction (); Car car = new car (); Car.setname ("Chery QQ"); Car.setseat ("4 people"); Sess.save (car); Truck truck = new Truck (); Truck.setname ("Faw Liberation"); Truck.setload ("10 tons"); Sess.save (truck); Tx.commit (); catch (Hibernateexception e) {if (tx!= null) Tx.rollback (); throw e;} finally {sess.close ();} Sess = Sf.opensession (); tx = null;try {tx = Sess.begintransaction (); List pets = Sess.find ("from" + Vehicle.class.getName ()), for (Iterator it = Pets.iterator (); It.hasnext ();) {Vehicle Vehicle = (Vehicle) it.next (); SYSTEM.OUT.PRINTLN ("model" + vehicle.getname () + "Its class is:" + vehicle.getclass (). GetName ());} Tx.commit ();} catch (Hibernateexception e) {if (tx!= null) Tx.rollback (); throw e;} finally {sess.close ();}}}
·  Running this class, the console output is as follows: · At the same time, the data table produces the following data: summary: Advantages: · Consistency with the object-oriented concept is best. It is best to support polymorphism, and only need to save records in the appropriate table for the role that an object might play. · Easy to modify base classes and add new classes. · This mapping has almost the best spatial savings. The only easy thing is the extra OID to connect to different levels of hierarchy. Disadvantages: · There are a large number of tables in the database. · The data is accessed for a longer period of time. In reading and writing related tasks, this mapping method is quite expensive, and the price increases with the increase of the inheritance level. · Support for reports is poor unless you define a view. Reference: · HIBERNATE-Java-compliant relational database Persistence (8th chapter) · Hibernate Simplified Inheritance mapping · Mapping Objects to relational databases:o/r Mapping in detail Mapping objects to relational databases the next article will talk about each specific class one table strategy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.