保存具有一对多关系的实体在 JPA 中会得到以下错误:

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

Saving entity in jpa with one to many relationship get this errors bellow

问题

Service:

  1. @Service
  2. @Transactional
  3. @Override
  4. public ContactDTO save(ContactDTO contactDTO) {
  5. Contact contact = new Contact();
  6. modelMapper.map(contactDTO.getIdentification(), contact);
  7. contact.setAddresses(contactDTO
  8. .getAddressList().stream().map(s -> {
  9. Address address = new Address();
  10. modelMapper.map(s, address);
  11. return address;
  12. }).collect(Collectors.toList()));
  13. repository.save(contact);
  14. return contactDTO;
  15. }

Entities:

  1. @Entity
  2. @Table(name = "contact")
  3. public class Contact {
  4. ...
  5. @OneToMany(cascade = CascadeType.ALL, mappedBy = "contact", fetch = FetchType.LAZY)
  6. private List<Address> addresses = new ArrayList<>();
  7. }
  8. @Entity
  9. @Table(name = "address")
  10. public class Address implements Serializable {
  11. ...
  12. @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
  13. @JoinColumn(name="contact_id", nullable=false)
  14. private Contact contact;
  15. }

Error Logs:

  1. 2020-09-02 14:09:44.939 ERROR 20148 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : NULL not allowed for column "CONTACT_ID"; SQL statement:
  2. insert into address (city, contact_id, number, state, street, type, unit, zipcode, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?) [23502-197]
英文:

Service:

  1. @Transactional
  2. @Override
  3. public ContactDTO save(ContactDTO contactDTO) {
  4. Contact contact = new Contact();
  5. modelMapper.map(contactDTO.getIdentification(), contact);
  6. contact.setAddresses(contactDTO
  7. .getAddressList().stream().map(s -&gt;
  8. {
  9. Address address = new Address();
  10. modelMapper.map(s, address);
  11. return address;
  12. }).collect(Collectors.toList()));
  13. repository.save(contact);
  14. return contactDTO;
  15. }

Entities:

  1. @Entity
  2. @Table(name = &quot;contract&quot;)
  3. public class Contact {
  4. ...
  5. @OneToMany(cascade = CascadeType.ALL, mappedBy = &quot;contact&quot;, fetch = FetchType.LAZY)
  6. private List&lt;Address&gt; addresses = new ArrayList&lt;&gt;();
  7. }
  8. @Entity
  9. @Table(name = &quot;address&quot;)
  10. public class Address implements Serializable {
  11. ....
  12. @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
  13. @JoinColumn(name=&quot;contact_id&quot;, nullable=false)
  14. private Contact contact;
  15. }

Error Logs:

  1. 2020-09-02 14:09:44.939 ERROR 20148 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : NULL not allowed for column &quot;CONTACT_ID&quot;; SQL statement:
  2. insert into address (city, contact_id, number, state, street, type, unit, zipcode, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?) [23502-197]

答案1

得分: 1

你需要在双向关系中同步双方,这意味着你需要在每个“Address”中设置“Contract”以更新外键。

  1. contact.getAddresses().forEach(a -> a.setContact(contact));
英文:

You need to sync both sides in bi-directional relationship, means you need to set Contract in every Address to update the foreign key

  1. contact.getAddresses().forEach(a -&gt; a.setContact(contact));

答案2

得分: 0

通过修改我的代码解决了这个问题。

从:

  1. @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
  2. @JoinColumn(name="contact_id", nullable=false)
  3. private Contact contact;

到:

  1. @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
  2. @JoinColumn(name="contact_id")
  3. private Contact contact;

移除了nullable=false并将CascadeType更改为REFRESH

英文:

Solved this by changing my code.

From:

  1. @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
  2. @JoinColumn(name=&quot;contact_id&quot;, nullable=false)
  3. private Contact contact;

To:

  1. @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
  2. @JoinColumn(name=&quot;contact_id&quot;)
  3. private Contact contact;

removed the nullable=false and change CascadeType.REFRESH

huangapple
  • 本文由 发表于 2020年9月2日 14:19:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63699739.html
匿名

发表评论

匿名网友

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

确定