更新 Entity 数据从 DTO 使用 Java Spring Boot 可以使用以下方法:

huangapple go评论61阅读模式
英文:

How can I update Entity data from DTO using java spring boot?

问题

I understand that you want a translation of the provided code and text. Here's the translated code:

@Entity
public class Person {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "person_gen")
    @GenericGenerator(name = "person_gen", strategy = "native")
    private Integer id;

    @Past
    @DateTimeFormat(pattern = "dd.MM.yyyy")
    @Column(name = "date_of_birth", columnDefinition = "DATE")
    private LocalDate dateOfBirth;
}

For the HTML-form part, you mentioned your approach for handling GET and POST requests and the challenge you faced when trying to update fields, including the custom query. However, I will not provide translations for the code as it remains the same in both languages.

英文:

Currently I am working on a webserver-project where I have this entity:

@Entity
public class Person {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "person_gen")
    @GenericGenerator(name = "person_gen", strategy = "native")
    private Integer id;


    @Past
    @DateTimeFormat(pattern = "dd.MM.yyyy")
    @Column(name = "date_of_birth", columnDefinition = ColumnDefinition.DATE)
    private LocalDate dateOfBirth;
}

For the HTML-form I wanted to have the localdate as a string, so I wrote a DTO and a Converter, that can convert Person to PersonDTO and the other way round.

My Problem:

(1) A user creates a GET request and receives a website with all person data (packed in a DTO), which he may edit.
(2) The data will be sent back to the server using POST.
(3) The server receives a DTO and converts it back to a Person.

As soon as I call save on the according service/repository, I get a duplicate entry. Instead of that, I want to UPDATE all fields that were edited. I even tryed to write a custom method like this:

@Query("UPDATE Person p SET ... WHERE p.id = :id")
void updateAllFields(@Param(value = "id") Integer id,...);

But also this approach did not work. How can I update? Is there an annotation missing or something?

答案1

得分: 1

Assuming your repository extends JpaRepository<Person, Integer>, then instead of calling repository.save(person) (when person is the input from the front-end), do Person personForUpdate = repository.findById(person.getId()), set all the fields from the given person to personForUpdate, like personForUpdate.setDateOfBirth(person.getDateOfBirth()) and then call repository.save(personForUpdate)

英文:

Assuming your repositry extends JpaRepository<Person, Integer>, then instead of calling repository.save(person) (when person is the input from the front-end), do Person personForUpdate = repository.findById(person.getId()), set all the fields from the given person to personForUpdate, like personForUpdate.setDateOfBirth(person.getDateOfBirth()) and then call repository.save(personForUpdate)

答案2

得分: 0

I found it out!

我的问题是,我有许多字段声明为

@ManyToMany

在这些实体之间有一个关联表。这是问题所在,因为然后更新或删除变得相当困难。
相反,我现在根据此示例进行了建模,使用了JoinColumns:

https://en.wikibooks.org/wiki/Java_Persistence/ManyToMany

它真的运行得很好!

英文:

Found it out!

My problem was, that I had many fields declared as

@ManyToMany

With a join table in between those entities. This was the problem, because then an update or delete is quite difficult.
Instead, I now modeled them according to this example with JoinColumns:

https://en.wikibooks.org/wiki/Java_Persistence/ManyToMany

and it works really nice!

huangapple
  • 本文由 发表于 2023年3月21日 01:41:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定