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
@DiscriminatorValue("BOOK")
public class Book extends Product {
public int isbn;
public String title;
public String author;
...
}
import javax.persistence.*;
@Entity
@DiscriminatorValue("COMPACT_DISC")
public class CompactDisc extends Product {
public String title;
public String artist;
...
}
// In our example, lets ignore the AbstractProduct class for a moment
// and assume that Product is the base class (with the "id").
import javax.persistence.*;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) // This is default strategy
@DiscriminatorColumn(name = "PRODUCT_TYPE", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("PRODUCT") // Default = entity name
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int id;
public String name;
public String description;
public double price;
...
}
import javax.persistence.*;
@Entity
@DiscriminatorValue("TRAVEL_GUIDE")
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