简体   繁体   English

使用@PrimaryKeyJoinColumn的休眠注释

[英]Hibernate annotations using @PrimaryKeyJoinColumn

I am using hibernate annotations in my application. 我在应用程序中使用休眠注释。 But I am getting one problem. 但是我遇到一个问题。

class A 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 B级

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

   String a;
 .....
}

class C C级

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

Initially, I am saving the class A . 最初,我要保存class A After some time, while saving class C , I set Class A id which was saved already . 一段时间后,在保存class C ,我设置了已经保存的 A类id 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. 在尝试保存类C时,它将创建一个新的class A对象,并将该新创建的对象值设置为类C对象。 I needed the class C object with class A object id which is created at first. 我需要首先创建的具有class A对象ID的class C对象。

I don't understand why a new object of class A is created again. 我不明白为什么再次创建class A的新对象。 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. 由于您希望C与已经存在的对象A具有相同的ID,因此这实际上不是实体继承。

You basically want a reference to an existing object and you should be using a @OneToOne relationship to map this. 基本上,您需要引用现有对象,并且应该使用@OneToOne关系来映射此对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM