Hibernate绑定在插入时返回null。

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

Hibernate binding returns null on insert

问题

我在这里寻求帮助,因为我已经在互联网上搜索了几个小时,但没有找到答案。也许这里的某个人会对我的问题有解决方案。

我刚开始使用 Spring Boot,所以请不要对我太苛刻,我可能在文档中遗漏了一些信息。
我试图使用一个类似这样的 JSON 文件向我的数据库添加数据。在“OneToMany”关系和相反的“ManyToOne”关系中,一切都正常工作(我遵循了这里的文档)。
我将 Cascade.ALL 设置为在实体之间进行传播。
正如你所看到的,Hibernate 会将绑定值设置为 null,就像它从未被插入过一样,尽管它似乎在前一步中已插入。
许多主题都涉及到这个问题,对于其中许多主题,它们已经通过设置 null = false 选项来解决,但在我这里不起作用。

信息:
一个联系人可以有多个电子邮件地址,而一个电子邮件地址只属于一个联系人。

我的代码:

Emails

  1. @NoArgsConstructor
  2. @AllArgsConstructor
  3. @Getter
  4. @Setter
  5. @Entity
  6. @Table(name = "Email")
  7. public class Email implements Serializable {
  8. @Id
  9. @GeneratedValue
  10. @Column(name = "idEmail")
  11. private int id;
  12. @Column(name = "Email")
  13. @JsonProperty("mail")
  14. private String email;
  15. @ManyToOne(fetch = FetchType.LAZY)
  16. @JoinColumn(name = "Contact_idContact")
  17. private Contact contact;
  18. }

Contact

  1. @Entity
  2. @Table(name = "Contact")
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. @Getter
  6. @Setter
  7. public class Contact implements Serializable {
  8. @GeneratedValue
  9. @Id
  10. @Column(name = "idContact", nullable = false, unique = true)
  11. private int id;
  12. @Column(name = "Nom")
  13. private String nom;
  14. @Column(name = "Prenom")
  15. private String prenom;
  16. @Column(name = "Mobile")
  17. private String mobile;
  18. @OneToMany(cascade = CascadeType.ALL, mappedBy = "contact")
  19. private Set<Email> emails = new HashSet<>();
  20. }

我的 JSON 提交:

  1. {
  2. "contacts": [
  3. {
  4. "nom": "myName",
  5. "prenom": "MyFirstName",
  6. "mobile": "06666666666",
  7. "emails": [
  8. {
  9. "mail": "mail1@test.com"
  10. },
  11. {
  12. "mail": "mail2@test.com"
  13. }
  14. ],
  15. }
  16. ]
  17. }

POST 方法

  1. @PostMapping("/add")
  2. public ResponseEntity<Void> addContact(@RequestBody Contact contact) {
  3. Contact contact1 = contactService.save(contact);
  4. URI location = ServletUriComponentsBuilder
  5. .fromCurrentRequest()
  6. .path("/{id}")
  7. .buildAndExpand(contact1.getId())
  8. .toUri();
  9. return ResponseEntity.created(location).build();
  10. }

错误:

  1. 2020-10-15 16:40:16.501 DEBUG 13608 --- [nio-9000-exec-2] org.hibernate.SQL : insert into Contact (Mobile, Nom, Prenom, idContact) values (?, ?, ?, ?)
  2. 2020-10-15 16:40:16.502 TRACE 13608 --- [nio-9000-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [06666666666]
  3. 2020-10-15 16:40:16.503 TRACE 13608 --- [nio-9000-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [VARCHAR] - [MyName]
  4. 2020-10-15 16:40:16.504 TRACE 13608 --- [nio-9000-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [VARCHAR] - [MyFirstName]
  5. 2020-10-15 16:40:16.505 TRACE 13608 --- [nio-9000-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [4] as [INTEGER] - [46]
  6. 2020-10-15 16:40:16.510 DEBUG 13608 --- [nio-9000-exec-2] org.hibernate.SQL : insert into Email (Contact_idContact, Email, idEmail) values (?, ?, ?)
  7. 2020-10-15 16:40:16.511 TRACE 13608 --- [nio-9000-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [INTEGER] - [null]
  8. 2020-10-15 16:40:16.512 TRACE 13608 --- [nio-9000-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [VARCHAR] - [mail1@test.com]
  9. 2020-10-15 16:40:16.512 TRACE 13608 --- [nio-9000-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [INTEGER] - [47]
  10. 2020-10-15 16:40:16.542 WARN 13608 --- [nio-9000-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1048, SQLState: 23000
  11. 2020-10-15 16:40:16.542 ERROR 13608 --- [nio-9000-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : Column 'Contact_idContact' cannot be null
  12. 2020-10-15 16:40:16.636 ERROR 13608 --- [nio-9000-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

在这种情况下,Contact_idContact 应该被插入为 46。

出了什么问题?

英文:

I'm here to get help because I've been searching the internet for a few hours without finding my answers. Maybe someone here will have a solution to my problem.

I'm new with spring boot, so don't be too mean to me, I probably missed some information in the documentation.
I'm trying to add data to my database with a json file that looks like this one.
Everything works correctly up to the OneToMany relationship and vice versa ManyToOne (I followed the doc here).
I put Cascade.ALL to propagate to entities.
As you can see, Hibernate makes a binding with a null value, as if it had never been inserted whereas it seems to be at the previous step.
A lot of topics deal with this problem, and for many of them they have been solved with the null = false option, but it doesn't work for me.

Information:
A contact can have multiple email addresses, and an email address belongs to only one contact.

My code :

Emails

  1. @NoArgsConstructor
  2. @AllArgsConstructor
  3. @Getter
  4. @Setter
  5. @Entity
  6. @Table(name = &quot;Email&quot;)
  7. public class Email implements Serializable {
  8. @Id
  9. @GeneratedValue
  10. @Column(name = &quot;idEmail&quot;)
  11. private int id;
  12. @Column(name = &quot;Email&quot;)
  13. @JsonProperty(&quot;mail&quot;)
  14. private String email;
  15. @ManyToOne(fetch = FetchType.LAZY)
  16. @JoinColumn(name = &quot;Contact_idContact&quot;)
  17. private Contact contact;
  18. }

Contact

  1. @Entity
  2. @Table(name = &quot;Contact&quot;)
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. @Getter
  6. @Setter
  7. public class Contact implements Serializable {
  8. @GeneratedValue
  9. @Id
  10. @Column(name = &quot;idContact&quot;, nullable = false, unique = true)
  11. private int id;
  12. @Column(name = &quot;Nom&quot;)
  13. private String nom;
  14. @Column(name = &quot;Prenom&quot;)
  15. private String prenom;
  16. @Column(name = &quot;Mobile&quot;)
  17. private String mobile;
  18. @OneToMany(cascade = CascadeType.ALL, mappedBy = &quot;contact&quot;)
  19. private Set&lt;Email&gt; emails = new HashSet&lt;&gt;();
  20. }

My json post :

  1. {
  2. &quot;contacts&quot;: [
  3. {
  4. &quot;nom&quot;: &quot;myName&quot;,
  5. &quot;prenom&quot;: &quot;MyFirstName&quot;,
  6. &quot;mobile&quot;: &quot;06666666666&quot;,
  7. &quot;emails&quot;: [
  8. {
  9. &quot;mail&quot;: &quot;mail1@test.com&quot;
  10. },
  11. {
  12. &quot;mail&quot;: &quot;mail2@test.com&quot;
  13. }
  14. ],
  15. }
  16. ]
  17. }

POST Method

  1. @PostMapping(&quot;/add&quot;)
  2. public ResponseEntity&lt;Void&gt; addContact(@RequestBody Contact contact) {
  3. Contact contact1 = contactService.save(contact);
  4. URI location = ServletUriComponentsBuilder
  5. .fromCurrentRequest()
  6. .path(&quot;/{id}&quot;)
  7. .buildAndExpand(contact1.getId())
  8. .toUri();
  9. return ResponseEntity.created(location).build();
  10. }

Error:

  1. 2020-10-15 16:40:16.501 DEBUG 13608 --- [nio-9000-exec-2] org.hibernate.SQL : insert into Contact (Mobile, Nom, Prenom, idContact) values (?, ?, ?, ?)
  2. 2020-10-15 16:40:16.502 TRACE 13608 --- [nio-9000-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [06666666666]
  3. 2020-10-15 16:40:16.503 TRACE 13608 --- [nio-9000-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [VARCHAR] - [MyName]
  4. 2020-10-15 16:40:16.504 TRACE 13608 --- [nio-9000-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [VARCHAR] - [MyFirstName]
  5. 2020-10-15 16:40:16.505 TRACE 13608 --- [nio-9000-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [4] as [INTEGER] - [46]
  6. 2020-10-15 16:40:16.510 DEBUG 13608 --- [nio-9000-exec-2] org.hibernate.SQL : insert into Email (Contact_idContact, Email, idEmail) values (?, ?, ?)
  7. 2020-10-15 16:40:16.511 TRACE 13608 --- [nio-9000-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [INTEGER] - [null]
  8. 2020-10-15 16:40:16.512 TRACE 13608 --- [nio-9000-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [VARCHAR] - [mail1@test.com]
  9. 2020-10-15 16:40:16.512 TRACE 13608 --- [nio-9000-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [INTEGER] - [47]
  10. 2020-10-15 16:40:16.542 WARN 13608 --- [nio-9000-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1048, SQLState: 23000
  11. 2020-10-15 16:40:16.542 ERROR 13608 --- [nio-9000-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : Column &#39;Contact_idContact&#39; cannot be null
  12. 2020-10-15 16:40:16.636 ERROR 13608 --- [nio-9000-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

In this case, Contact_idContact should be 46 as inserted.

What's wrong with it?

答案1

得分: 0

我解决了我的问题。我在Email.java中忘记了一个CascadeType.ALL。

英文:

I resolved my problem. I forgot a CascadeType.ALL in Email.java.

huangapple
  • 本文由 发表于 2020年10月15日 22:52:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64374289.html
匿名

发表评论

匿名网友

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

确定