I have spring boot application that is connected with the PostgreSQL database using spring-data-jpa
, here is my entity with nearly 40 fields
Now for saving the entity into database, I'm just using the studentRepository.save
method
studentRepository.save(new StudentEntity());
DAO Entity
@Table
@Entity
public class StudentEntity {
@Id
@Generate( using database sequenece)
private long studentId;
private String studentName;
private String dept;
private String age;
..
..
}
Repository
public interface StudentRepository implements JPARepository<Long, Student> {
}
But now I have requirement, if there is any student record in table with name
and dept
I should not insert the new record, I know I can use PostgreSQL ON CONFLICT
with native query for this, but if I use native query I have to specify all 40 fields in query and as method arguments which looks ugly.
Is there any way to make it simpler?
Example of native query
@Query(value = "insert into Users (name, age, email, status) values (:name, :age, :email, :status)", nativeQuery = true)
void insertUser(@Param("name") String name, @Param("age") Integer age, @Param("status") Integer status, @Param("email") String email);