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

Source: Internet
Author: User
Tags abstract date generator net one table string version access
Inherit the first two articles, Eclipse fast-start hibernate--4. The Inheritance map (1) and the Inheritance map (2) have discussed the strategy for each Class hierarchy table (table per class hierarchy) with one table per subclass (table per subclass), which is essentially a table for each concrete class (table per concrete Class).    Some of the repeated parts are not mentioned here, please refer to the first two articles.  This strategy is simple, the abstract base class does not participate in the mapping, and the specific subclass participates in the mapping.  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: Person. 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 note that the parent class is not mapped to a table here.
Person.java

* * Hibernate-Inheritance mapping (one table per concrete Class) * Create Date 2005-4-9 * @author javamxj (share java Fun) * @link blog:htpp://javamxj.mblogger.cn * HTP p://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 () {retur n 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 Student.javaStudent.java

Package javamxj.inheritance.three;/** * @hibernate. Class * table= "Student" */public class Student extends person {private String studentnumber;/** * @hibernate. Property * length = ' */public string Getstudentnumber () {return studentnumber;} public void Setstudentnumber (String studentnumber) {this.studentnumber = Studentnumber;}}

· Sub Class Professor.javaProfessor.java

Package javamxj.inheritance.three;/** * @hibernate. Class * table= "Professor" */public class Professor extends person {pri vate int salary;/** * @hibernate. Property */public int Getsalary () {return salary;} public void setsalary (int salary) {this.salary = salary;}}

· These two subclasses are simple, same as usual, and are simply mapped into a table. · Okay, now the whole project is structured as follows: 3.  Run Tasks · Double-click the "GENERATE-HBM" task to find more than Professor.hbm.xml and Student.hbm.xml two files in the package.  If not, press F5 to refresh (it is recommended that you open Eclipse's Preferences dialog box and check the workbench for automatic refresh workspace and auto save before build) so that you don't have to refresh it every time. This is the mapping file generated through two subclasses, note that no mapping files are generated for the parent class, and that the mapping files for these two subclasses already contain the properties of the parent class.
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 ">· Both mapping file information is automatically added to the Hibernate.cfg.xml file: <mapping resource= "Javamxj/inheritance/three/professor.hbm.xml"/> <mapping resource= "Javamxj/inheritance/three/student.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 Student (ID bigint not NULL, Studentnumber varchar (), name varchar (), primary key (ID)) Create Ta  BLE professor (id bigint not NULL, salary integer, name varchar (in), primary key (ID)) ·  When you switch to the database, you will find that you have automatically generated the datasheet student, professor.  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 concrete Class) * Create Date 2005-4-9 * @author javamxj (share java Fun) * @link blog:htpp://javamxj.mblogger.cn * HTP p://blog.csdn.net/javamxj/*/package javamxj.inheritance.three;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 (); Student Student = new Student (); Student.setname ("John"); Student.setstudentnumber ("1234554321"); Sess.save (Student); Professor Professor = new Professor ();p rofessor.setname ("Dick");p Rofessor.setsalary (4300); Sess.save (professor); 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 person = sess.find (' from ' + Person.class.getName ()); for (Iterator it = Person.iterator (); It.hasnext ();) {Person P = (person) it.next (); System.out.println ("person" + p.getname () + "' Its class is:" + p.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: · The report operation is simple: The table contains all the information for the specific subclass. Disadvantages: · The modification of the class causes the corresponding table changes to the table and its subclasses. ·  When you have multiple subclasses, you create duplicate fields in the database table. 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 says component (Component) mapping.

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.