(8) EBJ Learning: Single_table,joined,table_per_class Three inheritance strategies for JPA

Source: Internet
Author: User

One: single_table inheritance Policy

Animal.java

Import java.io.Serializable;

Import Javax.persistence.DiscriminatorColumn;
Import Javax.persistence.DiscriminatorValue;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;
Import javax.persistence.Inheritance;
Import Javax.persistence.InheritanceType;

/**
 * The single table inherits a * */
@Entity
@Inheritance (strategy=inheritancetype.single_table)//specified as single table inheritance
@ Discriminatorcolumn (name= "Animaltype")//Identification field definition
@DiscriminatorValue ("A")//identity of the class public
class Animal {
  @Id
  @GeneratedValue
  private int Id;
  private String name;
  Private String sex;
}

Bird.java

Import Javax.persistence.DiscriminatorValue;
Import javax.persistence.Entity;

@Entity
@DiscriminatorValue ("B") public
class Bird extends animal{
  private int height;
}
Pig.java

Import Javax.persistence.DiscriminatorValue;
Import javax.persistence.Entity;

@Entity
@DiscriminatorValue ("P") public
class Pig extends Animal {
  private int weight;
}

The final table structure


Two Join mappings

Animal.java

Import java.io.Serializable;

Import Javax.persistence.DiscriminatorColumn;
Import Javax.persistence.DiscriminatorValue;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;
Import javax.persistence.Inheritance;
Import Javax.persistence.InheritanceType;

/**
 * Join Map
 * Parent class, subclass corresponds to different table, only special attributes of its extension exist in subclasses (no attributes of parent class)
 * 
 *
/@Entity @Inheritance ( strategy=inheritancetype.joined)//specified as single-table inherit public
class Animal {
  @Id
  @GeneratedValue
  Private int id;
  private String name;
  Private String sex;
}

Bird.java

Import javax.persistence.Entity;

@Entity public
class Bird extends Animal {
  private int height;
}

The final table structure


Three Table_per_class

Animal.java

Import java.io.Serializable;

Import Javax.persistence.DiscriminatorColumn;
Import Javax.persistence.DiscriminatorValue;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;
Import javax.persistence.Inheritance;
Import Javax.persistence.InheritanceType;

/**
 * Table_per_class Policy: Parent class and subclass correspond to different tables, all attributes exist in subclass
 * (contains all attributes inherited from parent Class) * */
@Entity
@ Inheritance (strategy=inheritancetype.table_per_class) public 
CLASS Animal {
  @Id
//  @ Generatedvalue (when using Table_per_class, cannot use generatedvalue)
  private int id;
  private String name;
  Private String sex;
}

Bird.java

Import javax.persistence.Entity;

@Entity public
class Bird extends Animal {
  private int height;
}


The final table structure


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.