Skip to content

Instantly share code, notes, and snippets.

@dungdm93
Last active August 26, 2015 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dungdm93/22eb53b95030b641e2cb to your computer and use it in GitHub Desktop.
Save dungdm93/22eb53b95030b641e2cb to your computer and use it in GitHub Desktop.
[Java] [JPA] @Inheritance example
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;
...
}
import javax.persistence.*;
@Entity
public class Book extends Product {
public int isbn;
public String title;
public String author;
...
}
import javax.persistence.*;
@Entity
public class CompactDisc extends Product {
public String title;
public String artist;
...
}
import javax.persistence.*;
@Entity
public class Product extends AbstractProduct {
public double price;
...
}
import javax.persistence.Entity;
@Entity
public class TravelGuide extends Book {
public String country;
...
}
@dungdm93
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment