How to build a webserver in Android

Source: Internet
Author: User

    • For object-oriented programming languages, inheritance and polymorphism are the two most basic concepts. Hibernate's inheritance mapping can understand the inheritance relationships between persisted classes. For example: the relationship between people and students. The student inherits the person, may think the student is a special person, if carries on the inquiry to the person, the student's instance also will be obtained.

    • Hibernate supports three types of inheritance mapping strategies:
Mapping with subclass: Each Entity object in the domain model is mapped to a separate table, meaning that inheritance relationships and polymorphism in the domain model are not considered in the relational data model.
mapping with Joined-subclass: using the same table for subclasses in an inheritance relationship requires adding additional fields of the zone's molecular class type to the database table.
mapping with Union-subclass: each class in the domain model maps to a table that describes the inheritance relationship between tables through foreign keys in the relational data model. This is equivalent to building a table in the database according to the structure of the domain model, and using foreign keys to establish the inheritance relationship between tables.


inherit mappings with subclass elements
    • Using the subclass inheritance map enables the use of the same table for the parent and child classes in the inheritance relationship
    • Because the instances of the parent and child classes are all saved in the same table, you need to add a column to the table that distinguishes between each row of records to the instance of which class is low----This column is called the discernibility column (discriminator).
    • Under this mapping strategy, use subclass to map subclasses, use class or subclass's Discriminator-value property to specify the value of the distinguished column
    • None of the sub-class defined fields can have a non-null constraint. If you add a non-null constraint to those fields, the instance of the parent class does not actually have a value in those columns, which causes a database integrity violation that prevents an instance of the parent class from being saved to the database


Inherit mappings with Joined-subclass elements
    • a table of each subclass can be implemented using the inheritance map of the Joined-subclass element
    • With this mapping strategy, the parent class instance is saved in the parent class table, and the child class instances are stored together by the parent class table and the Child class table. because the child class instance is also a special instance of the parent class, it must also contain the properties of the parent class instance. The attributes that are common to the child class and the parent class are then saved in the parent class table, and the added attributes of the subclass are saved in the Child class table.
    • Under this mapping strategy, you do not have to use the discriminator column, but you need to map the common key with the key element for each subclass.
    • the added properties of subclasses can add non-null constraints. because the properties of the child class and the properties of the parent class are not saved in the same table


inherit mappings with Union-subclass elements
    • With the Union-subclass element, you can map each entity object to a separate table.
    • The added attribute of a subclass can have a non-null constraint--- that is, the data of the parent class instance is saved in the parent table, and the child class instance data is saved in the subclass table.
    • the data for the subclass instance is saved only in the subclass table , and there are no records in the parent class table
    • Under this mapping strategy, the fields of the subclass table are more than the mapped fields of the parent table, because the fields of the subclass table are equal to the fields of the parent table, plus the sum of the attributes added to the child class.
    • Under this mapping strategy, Neither the discriminator column nor the key element can be used to map the common primary key.
    • using the Union-subclass mapping strategy is not a primary key generation strategy that uses the identity , because all entity classes in the same class of inheritance need to use the same primary key seed, that is, the primary key for the records corresponding to multiple persisted entities should be contiguous. It is also not appropriate to use the native primary key to generate the policy, because native chooses to use identity or sequence based on the database.




Comparison of three kinds of inheritance mapping methods



inherit mappings with subclass elements
Detailed Examples:Person.java
Package Com.atguigu.hibernate.subclass;public class Person {private Integer id;private String name;private int age; Public Integer GetId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}

Student.java
Package Com.atguigu.hibernate.subclass;public class Student extends Person{private string School;public string Getschool () {return school;} public void Setschool (String school) {this.school = school;}}

Person.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
Package Com.atguigu.hibernate.subclass;import Java.io.fileinputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.sql.blob;import Java.sql.connection;import Java.sql.sqlexception;import Java.util.date;import Java.util.list;import Java.util.set;import Org.hibernate.hibernate;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import Org.hibernate.jdbc.work;import Org.hibernate.service.ServiceRegistry; Import Org.hibernate.service.serviceregistrybuilder;import Org.junit.after;import Org.junit.before;import Org.junit.test;public class Hibernatetest {private sessionfactory sessionfactory;private Session session;private Transaction Transaction; @Beforepublic void init () {Configuration configuration = new Configuration (). Configure ();                            Serviceregistry serviceregistry = new Serviceregistrybuilder (). Applysettings (Configuration.getproperties ()) . Buildserviceregistry ();Sessionfactory = Configuration.buildsessionfactory (serviceregistry); session = Sessionfactory.opensession (); Transaction = Session.begintransaction ();} @Afterpublic void Destroy () {transaction.commit (); Session.close (); Sessionfactory.close ();} /** * Cons: * 1. The distinguished column is used. * 2. A field unique to a subclass cannot add a non-null constraint. * 3.  If the inheritance hierarchy is darker, the data table will have more fields. *//** * Enquiry: * 1. To query the parent class record, you only need to query a data table * 2. For sub-class records, you only need to query one datasheet/@Testpublic void Testquery () {list<person> persons = Session.createquery ("from"). List (); System.out.println (Persons.size ()); List<student> Stus = Session.createquery ("from Student"). List (); System.out.println (Stus.size ()); }/** * Insert Operation: * 1. For subclass objects, you simply insert the records into a single data table. * 2.  The discerning person is listed with Hibernate automatic maintenance. */@Testpublic void Testsave () {Person person = new person ();p erson.setage (one);p erson.setname ("AA"); Session.save ( person); Student stu = new Student (), stu.setage (+), Stu.setname ("BB"), Stu.setschool ("Atguigu"); Session.save (Stu);}}


inherit mappings with Joined-subclass elements
Detailed Examples:Person.java
Package Com.atguigu.hibernate.joined.subclass;public class Person {private Integer id;private String name;private int Age;public Integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}

Student.java
Package Com.atguigu.hibernate.joined.subclass;public class Student extends Person{private string School;public string Getschool () {return school;} public void Setschool (String school) {this.school = school;}}

Person.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
Package Com.atguigu.hibernate.joined.subclass;import Java.util.list;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import Org.hibernate.service.serviceregistry;import Org.hibernate.service.serviceregistrybuilder;import Org.junit.After ; Import Org.junit.before;import Org.junit.test;public class Hibernatetest {private Sessionfactory sessionfactory; Private Session session;private Transaction Transaction; @Beforepublic void init () {Configuration configuration = new Configuration (). Configure ();                            Serviceregistry serviceregistry = new Serviceregistrybuilder (). Applysettings (Configuration.getproperties ()) . Buildserviceregistry (); sessionfactory = Configuration.buildsessionfactory (serviceregistry); session = Session Factory.opensession (); transaction = Session.begintransaction ();} @Afterpublic void Destroy () {transaction.commit (); Session.close (); Sessionfactory.close ();} /** * Advantages: * 1. No need to use theThe list of other people. * 2. A field unique to a subclass can add a non-null constraint. * 3.  There are no redundant fields. *//** * Enquiry: * 1. Query the parent class record, make a LEFT outer join query * 2.  For sub-class records, make an inner join query. */@Testpublic void testquery () {list<person> persons = Session.createquery ("from"). List (); System.out.println (Persons.size ()); List<student> Stus = Session.createquery ("from Student"). List (); System.out.println (Stus.size ()); }/** * Insert Operation: * 1.  You need to insert at least two data tables for the subclass object. */@Testpublic void Testsave () {Person person = new person ();p erson.setage (one);p erson.setname ("AA"); Session.save ( person); Student stu = new Student (), stu.setage (+), Stu.setname ("BB"), Stu.setschool ("Atguigu"); Session.save (Stu);}}


inherit mappings with Union-subclass elements
Person.java
Package Com.atguigu.hibernate.union.subclass;public class Person {private Integer id;private String name;private int Age ;p ublic Integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}

Student.java
Package Com.atguigu.hibernate.union.subclass;public class Student extends Person{private string School;public string Getschool () {return school;} public void Setschool (String school) {this.school = school;}}

Person.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
Package Com.atguigu.hibernate.union.subclass;import Java.util.list;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import Org.hibernate.service.serviceregistry;import Org.hibernate.service.serviceregistrybuilder;import Org.junit.After ; Import Org.junit.before;import Org.junit.test;public class Hibernatetest {private Sessionfactory sessionfactory; Private Session session;private Transaction Transaction; @Beforepublic void init () {Configuration configuration = new Configuration (). Configure ();                            Serviceregistry serviceregistry = new Serviceregistrybuilder (). Applysettings (Configuration.getproperties ()) . Buildserviceregistry (); sessionfactory = Configuration.buildsessionfactory (serviceregistry); session = Session Factory.opensession (); transaction = Session.begintransaction ();} @Afterpublic void Destroy () {transaction.commit (); Session.close (); Sessionfactory.close ();} @Testpublic void TestupDate () {String hql = "UPDATE person p SET p.age ="; Session.createquery (HQL). Executeupdate ();} /** * Advantages: * 1. There is no need to use the discrimination column. * 2. A field unique to a subclass can add a non-null constraint. * * Disadvantage: * 1. There is a redundant field * 2. If you update the fields of the parent table, the update is less efficient *//** * query: * 1. To query the parent class record, you need to summarize the parent and child table records together to make the query.  Performance is slightly worse. * 2. For sub-class records, you only need to query one datasheet/@Testpublic void Testquery () {list<person> persons = Session.createquery ("from"). List (); System.out.println (Persons.size ()); List<student> Stus = Session.createquery ("from Student"). List (); System.out.println (Stus.size ()); }/** * Insert Operation: * 1. For subclass objects, you simply insert the records into a single data table. */@Testpublic void Testsave () {Person person = new person ();p erson.setage (one);p erson.setname ("AA"); Session.save ( person); Student stu = new Student (), stu.setage (+), Stu.setname ("BB"), Stu.setschool ("Atguigu"); Session.save (Stu);}}



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.