简体   繁体   中英

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 object 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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