英文:
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论