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

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

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

问题

Service:

@Service
@Transactional
@Override
public ContactDTO save(ContactDTO contactDTO) {
    Contact contact = new Contact();
    modelMapper.map(contactDTO.getIdentification(), contact);
    contact.setAddresses(contactDTO
            .getAddressList().stream().map(s -> {
                Address address = new Address();
                modelMapper.map(s, address);
                return address;
            }).collect(Collectors.toList()));

    repository.save(contact);
    return contactDTO;
}

Entities:

@Entity
@Table(name = "contact")
public class Contact {
    ...

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "contact", fetch = FetchType.LAZY)
    private List<Address> addresses = new ArrayList<>();
}

@Entity
@Table(name = "address")
public class Address implements Serializable {

    ...

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

Error Logs:

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:
insert into address (city, contact_id, number, state, street, type, unit, zipcode, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?) [23502-197]
英文:

Service:

@Transactional
@Override
public ContactDTO save(ContactDTO contactDTO) {
    Contact contact = new Contact();
    modelMapper.map(contactDTO.getIdentification(), contact);
    contact.setAddresses(contactDTO
            .getAddressList().stream().map(s -&gt;
            {
                Address address = new Address();
                modelMapper.map(s, address);
                return address;
            }).collect(Collectors.toList()));

    repository.save(contact);
    return contactDTO;
}

Entities:

@Entity
@Table(name = &quot;contract&quot;)
public class Contact {
    ... 
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = &quot;contact&quot;, fetch = FetchType.LAZY)
    private List&lt;Address&gt; addresses = new ArrayList&lt;&gt;();
}

@Entity
@Table(name = &quot;address&quot;)
public class Address implements Serializable {

    ....

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

Error Logs:

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:
insert into address (city, contact_id, number, state, street, type, unit, zipcode, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?) [23502-197]

答案1

得分: 1

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

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

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

答案2

得分: 0

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

从:

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

到:

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

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

英文:

Solved this by changing my code.

From:

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

To:

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
@JoinColumn(name=&quot;contact_id&quot;)
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:

确定