2016-05-10 19 views
0

私は基本クラスのPersonクラスとPersonクラスを継承しています。ここではいくつかの短い抜粋は以下のとおりです。これは、ID 1および2、1と従業員に2つのエントリが生成されますJPAの継承:同じIDを挿入する方法

Employee e1 = new Employee(...); 
Person p1 = new Person(e1); 
Boss b = new Boss(...); 
Person p2 = new Person(b); 
Employee e2 = new Employee(...); 
Person p3 = new Person(e2); 
e1.save(); 
p1.save(); 
b.save(); 
p2.save(); 
e2.save(); 
p3.save(); 

@Entity 
public class Person extends Model{ 

    // ATTRIBUTES 
    @Id 
    @Column(columnDefinition = "integer") 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private int id; 
    @Column(columnDefinition = "varchar(20) not null") 
    protected String firstName; 
    @Column(columnDefinition = "varchar(20) not null") 
    protected String lastName; 
    @Column(columnDefinition = "varchar(20) not null") 
    protected String password; 
    @Column(columnDefinition = "varchar(50) not null") 
    protected String eMail; 

@Entity 
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 
public class Boss extends Person { 

    // ATTRIBUTES 
    @Column(insertable = false, updatable = false) 
    private String dtype; 
    @OneToMany(mappedBy = "boss") 
    private List<Employee> listEmployee; 

@Entity 
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 
public class Employee extends Person { 

    // ATTRIBUTES 
    @Column(insertable = false, updatable = false) 
    private String dtype; 
    @Column(columnDefinition = "Varchar(20)") 
    private String position; 
    @ManyToOne 
    private Boss boss; 

は、今私は、従業員、ボスと再び従業員を保存したいですid1とid3を持つPersonのid 1,2,3のエントリのエントリ。 e1はEmployeeではid 1、Personではid 1、Bossではid 1、Personではid 2を持ち、e2はEmployeeでid 2を持ちます。人のid 3。

私のprogrammにグローバルカウンタを使用せずにEmployeeとPersonに同じIDを持つEmployeeを挿入することはできますか?

パトリック

答えて

0

あなたは人のインスタンスを作成し、永続化する必要性を全く行わないありがとう - 実際に人はおそらく抽象クラスでなければなりません。 (あなたが現在持っていません)正しいマッピングを考えると、あなたが必要とする必要があるすべては以下の通りです:

あなたのマッピングで
Employee e1 = new Employee(...); 
Boss b = new Boss(...); 
Employee e2 = new Employee(...); 

e1.save(); 
b.save(); 
e2.save(); 

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)フォームのボスと従業員を削除して、Personクラスに追加します。あなたは上司と従業員から以下を削除することができるように

はまた、あなたはTABLE_PER_CLASSためDiscrimatorを必要としません:

@Column(insertable = false, updatable = false) private String dtype; 
関連する問題