Last active
August 26, 2015 07:20
-
-
Save dungdm93/22eb53b95030b641e2cb to your computer and use it in GitHub Desktop.
[Java] [JPA] @Inheritance example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
@Entity | |
// TABLE_PER_CLASS can mixed with other strategies. | |
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) | |
// @DiscriminatorColumn & @DiscriminatorValue are no longer take effect | |
// @DiscriminatorColumn(name = "PRODUCT_TYPE", discriminatorType = DiscriminatorType.STRING) | |
// @DiscriminatorValue("PRODUCT") | |
public abstract class AbstractProduct { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
public int id; | |
public String name; | |
public String description; | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
@Entity | |
public class Book extends Product { | |
public int isbn; | |
public String title; | |
public String author; | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
@Entity | |
public class CompactDisc extends Product { | |
public String title; | |
public String artist; | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
@Entity | |
public class Product extends AbstractProduct { | |
public double price; | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.Entity; | |
@Entity | |
public class TravelGuide extends Book { | |
public String country; | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SINGLE_TABLE
JOINED
TABLE_PER_CLASS
@MappedSuperclass
External