0

I have two records in my ArrayList<Object> but when I'm trying to insert these records in database, only the last record gets saved into DB.

I'm using Spring data JPA entityManager.merge() method since I have custom sequenceGenerator to be used for Entities.

summaryList.stream().forEach{(summary -> entityManager.merge(summary)};

On debugging I get two records in summaryList but when I check my DB table, only one record gets inserted.

Sai prateek
  • 11,842
  • 9
  • 51
  • 66
Meenal
  • 105
  • 3
  • 17
  • Have you checked your custom generator earlier? Seems it returns the same value twice. – amseager Feb 09 '19 at 12:22
  • @amseager: I didn't get. Could you please elaborate as I was able to get correct values in database earlier. The custom generator is used only for generating IDs from one of the common Sequence. – Meenal Feb 09 '19 at 12:26
  • since you don't post the entity then what are you expecting? You also dont mention where transaction boundaries are, or what lifecycle state the objects to be persisted are in when calling merge. –  Feb 09 '19 at 15:16
  • @BillyFrost: code is shown below: public void saveTable(List summary) { summary.stream().forEach(sum -> entityManager.merge(sum)); } where Summary is my Entity. – Meenal Feb 11 '19 at 06:19
  • Entity is below: @Id @GeneratedValue(generator = "SUM_ID_SEQ") @GenericGenerator(name = "SUM_ID_SEQ", strategy = "com.sbc.test.SummarySequenceIdGenerator") @Column(name = "SUM_ID", insertable = true, updatable = true, unique = true, nullable = false) private BigInteger mbrshpLdgrSumId; @Column(name = "Name", nullable = false) private String name; – Meenal Feb 11 '19 at 06:24
  • put that in your QUESTION! –  Feb 11 '19 at 07:18
  • Can someone provide some other approach other than using @Version since we have restricted access privileges. – Meenal Feb 11 '19 at 12:41

1 Answers1

0

Hibernate generates a SELECT statement first to fetch the latest state of the underlying database record, and then, it copies the detached entity state onto the newly fetched managed entity. This way, the dirty checking mechanism can detect any state change and propagate it to the database.

While for IDENTITY and SEQUENCE generator strategies, you can practically use merge to persist an entity, for the assigned generator, this would be less efficient.

You can actually fix this issue by adding a version property to your entity.

@Version
private Long version;
Sai prateek
  • 11,842
  • 9
  • 51
  • 66
  • Do I need to set the value for version as well in my service layer. – Meenal Feb 09 '19 at 12:46
  • My question was how will it identify the value of "version". Will that need a new column in DB table. – Meenal Feb 09 '19 at 13:20
  • As I told just add @Version private Long version; rest hibernate will take care – Sai prateek Feb 09 '19 at 14:14
  • Getting error in select query which merge() internally fires, when added @Version since it adds a column version also which is not there in table. – Meenal Feb 11 '19 at 06:26
  • Hibernate should create the column version when you restarts the server – Sai prateek Feb 11 '19 at 07:44
  • It is not creating, maybe since Iam using Spring Data JPA. Please confirm Iam not sure though – Meenal Feb 11 '19 at 09:22
  • see this https://stackoverflow.com/questions/10648515/using-version-in-spring-data-project – Sai prateek Feb 11 '19 at 09:40
  • I agree but my only concern is whether it will go and add one more Column in my table. If that is the case, we have restriction in adding column from our side privilege restriced – Meenal Feb 11 '19 at 11:45