1

I have many table in which i need to insert duplicate record with different primary key. Here are relationships..

@Entity
class A{
  @Id
  private String USER_ID;
}

@Entity
class B{
  @Id
  private String id;

  @ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
  @JoinColumn(name = "USER_ID")
  A a;
}


I need to insert record in B.

I had get related data from tables B. Create new copy to it.. But reference of A is still same. Change it's primary key(which is not auto generated.)

and when call jpa.save(b) method.. it first hit select query and select all field of A then fire insert query.

This select query is extra one. can anyone know how to avoid this?

pratik deshai
  • 2,770
  • 2
  • 11
  • 13
  • @locus2k: in given forum save is done by Session not by directly jpa.. when i try to do same, it's not fire select query but when is user session.save(b) it is throwing constrain violation for A..(i set A in B which is already in persistent context) – pratik deshai Nov 13 '19 at 13:03
  • @locus2k: when i used session.load it 's not firing select query but throwing constrain violation exception. It's try to save that record again. – pratik deshai Nov 14 '19 at 05:08
  • Could remove the reference and just store the `user_id` as the string. This would cause extra queries later to get the association but would prevent the extra selects up front. – locus2k Nov 14 '19 at 12:55
  • @locus2k: thanks for that link.. actually constrain violation exception is for another field. i used hibernate session to save and load related entity via session.load method will work. – pratik deshai Nov 15 '19 at 04:58

0 Answers0