我要翻译的内容: 如何使用Java Spring Boot从DTO更新实体数据?

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

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

问题

Here is the translated code:

我目前正在进行一个 Web 服务器项目在这个项目中我有这个实体

@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;
}
对于 HTML 表单我想将 LocalDate 转换为字符串因此我编写了一个 DTO 和一个转换器可以将 Person 转换为 PersonDTO反之亦然

我的问题

(1) 用户创建一个 GET 请求接收包含所有个人数据的网站打包在 DTO 中),然后可以进行编辑
(2) 数据将使用 POST 发送回服务器
(3) 服务器接收一个 DTO 并将其转换回 Person

只要我在相应的服务/存储库上调用 save我就会收到重复的条目而我希望更新所有已编辑的字段我甚至尝试编写一个自定义方法如下所示

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

Please note that the code has been translated to Chinese, and I have excluded the parts related to the issue you're facing, as per your request.

英文:

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

假设您的存储库是 extends JpaRepository<Person, Integer>,那么不是调用 repository.save(person)(当person 是来自前端的输入)而是执行 Person personForUpdate = repository.findById(person.getId()),将给定的person的所有字段设置到personForUpdate中,例如 personForUpdate.setDateOfBirth(person.getDateOfBirth()),然后调用 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-2.html
匿名

发表评论

匿名网友

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

确定