0

I am using Spring boot 2.0.3.RELEASE(FINCHLEY) `starter-data-jpa 2.0.5.RELEASE. The scenario is pretty basic, build an entity and save it.

Let's go through the code for better context before we go through texts.

Entity class (Asset): The id(primary key) does not generate automatically because we basically get a unique UUID.toString() from another source which is assuredly unique.

import lombok.*;
import org.springframework.data.domain.Persistable;

import javax.persistence.*;
import java.util.List;

@Setter
@Getter
@ToString
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "ASSET", schema = "SCHEMA")
public class Asset implements Persistable<String> {
// Propriety code hence column names removed. 
//Let's just assume column names are fine, since when I am doing a find(), results are cool

    private static final long serialVersionUID = 1L;

    @Id
    private String id;

    private String creditPool;

    private int quantity;

    private int assetStatusTypeId;

    private long assetTypeId;

    private int ageOfAsset;

    //The below snippet is part of Persistable<String> to force a new entry. Newly added following an answer in stackoverflow.

    //Here, update is basically false by default, 
    //so as you can see isNew() will always return true forcing jpa to save a new record

    @Transient
    private boolean update;

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public boolean isNew() {
        return !this.update;
    }
}

As per the comments I have added, you see I added that snippet because we are not autogenerating the id.

Without the snippet and Persistable the issue is same btw.

My custom repository class has the below custom save function because of business reasons.

@Override
   public <S extends T> S save(S entity, long value) {
       entity.someParams(args getting from from a jar) //which is really fine
       return super.save(entity);
   }

Code from which save is being called is below

Asset asset = Asset.builder()
.id(UUID.randomId().toString()) //see always unique
.assetStatusTypeId(a valid id)
.assetTypeId(a valid id)
.creditPool("pool")
.quantity(integer value)
.ageOfAsset(integer value).build();
repo.save(asset);

I have verified the builder mutiple times and, It indeed creates a Asset object without fail.

But the culprit save never gets executed.

I have turned on the showSQL:true to check whether the insert query gets called or not. But I see no such queries.

Hibernate properties:

jpa:
  show-sql: true
  properties:
    hibernate:
      enable_lazy_load_no_trans: true
      show_sql: true
      format_sql: true
      jdbc:
        batch_size: 5

I considered a scenario where even with all the safe checks, JPA might be treating it as a update scenario,

I ignored the save and used persist as well. Even then no insertion.

repo.persist(asset);

Please help on this.

stackoverflow Sources for my workarounds and the duplicates. Any help on this would be very much appreciated.

bibliophilsagar
  • 1,713
  • 1
  • 19
  • 40

1 Answers1

0

You should flush after the save or directly use saveAndFlush(asset).

olivergregorius
  • 109
  • 1
  • 7