I'm spring boot newbie so my knowledge is lacking.
I am using MySql with JPA repository in spring boot.
I put the data in an ArrayList and saved it using the "SaveAll" method.
I am trying to save by "insert ignore" method due to data conflict problem, is it possible?
Batch insert has been set and is being used.
Adding a custom insert query is very easy to do in Spring Data JPA. All we have to do is to annotate the entity with @SQLInsert. And jpa repositories save and saveAll will take this insert query in consideration.
find here some documentation
Code sample :
import org.hibernate.annotations.SQLInsert;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
@Entity
@Table(name = "users")
@SQLInsert(sql = "INSERT IGNORE INTO users(first_name, last_name, email) " +
"VALUES (?, ?, ?)" )
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "first_name", nullable = false)
@NotBlank
@Size(max = 512)
private String firstName;
@Column(name = "last_name", nullable = false)
@NotBlank
@Size(max = 512)
private String lastName;
@Column(nullable = false)
@NotBlank
@Email
@Size(max = 512)
private String email;
But in my Opinion it is better to use native query for insertions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With