开发者

Hibernate annotations using @PrimaryKeyJoinColumn

I am using hibernate annotations in my application. But I am getting one problem.

class A

@Entity
@Table(name = DBConstants.T_A )
@Inheritance(strategy=InheritanceType.JOINED)
public class A {

   //Id
    @Column(name = "id")
    @GeneratedValue(generator = A_SEQ)
    @SequenceGenerator(name = A_SEQ, sequenceName=SeqA_SEQ)
    private long id;

   ....
}

class B

//Entity
@Table(name = "T_B")
@PrimaryKeyJoinColumn(name = "a_id")
public class B extends A{

   String a;
 .....
}

class C

@Entity
@Table(name = "T_C")
@PrimaryKeyJoinColumn(name = "a_id")
public class C extends B
{
...
}

Initially, I am saving the class A. After some time, while saving class C, I set Class A id which was saved already. While trying to save the class C, it creates a new class A object and sets that newly created object value to class C object. I needed the class C o开发者_如何学运维bject with class A object id which is created at first.

I don't understand why a new object of class A is created again. Can anyone please answer to my question what went wrong?


Since you want C to have the same id as an object A that already exists, this is not really entity inheritance.

You basically want a reference to an existing object and you should be using a @OneToOne relationship to map this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜