2014年1月20日 星期一

[JPA] @Inheritance, @DiscriminatorColumn and @DiscriminatorValue

@Entity
@Table(name = "car")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public abstract class Car { .... }


car table 中的資料可以以 type 區別為不同意義的物件

例如可以建立下列物件:

@Entity
@DiscriminatorValue("bus")
public class Bus extends Car { ... }

type = bus 的物件


@Entity
@DiscriminatorValue("truk")
public class Truk extends Car { ... }

type = truk 的物件


@DiscriminatorValue("xxxx" )
會自動存入 type = xxxx


沒有留言:

張貼留言