英文:
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 ->
{
Address address = new Address();
modelMapper.map(s, address);
return address;
}).collect(Collectors.toList()));
repository.save(contact);
return contactDTO;
}
Entities:
@Entity
@Table(name = "contract")
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]
答案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 -> 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="contact_id", nullable=false)
private Contact contact;
To:
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
@JoinColumn(name="contact_id")
private Contact contact;
removed the nullable=false and change CascadeType.REFRESH
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论