'Force Spring Data JPA to fire INSERT query without SELECT for detached entity

I am using Spring Data JPA with Hibernate in which I am operating on the detached entity. So when I save the detached entity hibernated internally call the merge method which executes first SELECT statement and then the INSERT statement. (The entity is not in the DB but for other reason the entity is in detached state)

custRepository.save(billingInfo);   

Lets say billingInfo is detached entity, when spring jpa save method is called, it will trigger merge method internally resulting SELECT statement execution first and then INSERT statement.

Is there is a way to tell hibernate to execute only the INSERT query for the detached entity?

I am already implementing the Persistable interface which I am managing the state of the entity.

Below is the entity

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Table(name= "BLI_INFO")
public class BillingInfo implements Persistable<long> {
    @Id
    @Column(name = "BLI_ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_gen")
    @SequenceGenerator(sequenceName = "BLI_ID_SEQ", name = "id_gen", allocationSize = 1)
    private long billingId;

    ...
    // rest of the fields
    ...

    @Transient
    private boolean isNew = false;

    @PrePersist
    @PostLoad
    void markNotNew() {
        this.isNew = false;
    }

    @Override
    public boolean isNew(){
       return true;
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source