1

I'm using a native query on my repository. I want to insert object data, but the query doesn't understand the object fields. How to solve this problem, I don't want to have a method with a lot of parameters.

My class:

public class StudentDTO {
        private long numberzachetka;
        private String fio;
        private Date entrydate;
        private int course;
        private int numbergroup;
        private long specialty;
        private long faculty;
//getters, setters..}

Native query:

@Query(value ="insert into student(numberzachetka,fiostudent,entrydate,course,numbergroup,specialtykey,facultynumber)" +
            " values" +
            " (:student.numberzachetka,:student.fio,:student.entrydate,:student.course,:student.numbergroup,:student.specialty,:student.faculty)", nativeQuery = true)
    void addNewStudent(@Param("student") StudentDTO studentDTO);

Student entity:

@Entity
@Table(name="student")
public class Student  {

    @Id
    @Column(name="numberzachetka", nullable = false)
    private long numberzachetka;

    @Column(name="fiostudent", nullable = false, length = 100)
    private String fio;

    @Temporal(TemporalType.DATE)
    @Column(name = "entrydate", nullable = false)
    private Date entrydate;

    @Column(name="course", nullable = false)
    private int course;

    @Column(name="numbergroup", nullable = false)
    private int numbergroup;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "specialtykey", nullable = false)
    private Specialty specialty;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "facultynumber", nullable = false)
    private Faculty faculty;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "student")
    private Set<Performance> performances;
5
  • Why? This beats the purpose of using JPA in the first place. Just save the student. Commented Mar 14, 2020 at 11:40
  • Student entity has one to many and many to one connections, I think it wouldn't work Commented Mar 14, 2020 at 11:43
  • Yes it would. That is the whole purpose of JPA. Commented Mar 14, 2020 at 11:45
  • can you share the error log ? Commented Mar 14, 2020 at 12:08
  • @M.Deinum Nice, it works. Commented Mar 14, 2020 at 12:21

2 Answers 2

1
@Query(value ="insert into student(numberzachetka,fiostudent,entrydate,course,numbergroup,specialtykey,facultynumber)" +
            " values" +
            " (:student.numberzachetka,:student.fio,:student.entrydate,:student.course,:student.numbergroup,:student.specialty,:student.faculty)", nativeQuery = true)
    void addNewStudent(@Param("student") StudentDTO studentDTO);

This is not the right way to save value in db using JPA repository. If you want to save value in DB there is very simpler way, follow below steps:

  1. Copy all the values from the studentDTO to Student object using below code.

Student student = new Student(); BeanUtils.copyProperties(studentDto,student);

  1. create StudentRepository interface

@Repository public interface StudentRepository extends JpaRepository<Student, Long> { }

  1. @Autowired above Repository in the service class

@Autowired private StudentRepository studentRepository;

  1. Save the Student object using repository predefined save method.

studentRepository.save(student);

These are the minimal change you want to save object in db, instead of going through native SQL query.

Sign up to request clarification or add additional context in comments.

Comments

0

Within the entity you could have a constructor accepting the DTO. The constructor transfers the DTO attributes to the fields of the entity. Then you can just use the JpaRepository save method. You don't need the select statement. Keep in mind to add also an empty constructor which is required by JPA.

Another Idea which I use in my projects is to use mapstruct to transfert attributes from DTOs to entities.

The way you present your idea is not possible. JPA does not know how to map a DTO to an entity.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.