我正在使用与hibernate和spring boot之间的一对多关系,我收到了这个错误 添加新公司或更新公司时
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'activity' cannot be null
添加新联系人或更新联系人时出现同样的错误
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'fk_company' cannot be null
我的实体课程:
@Entity
public class Company {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Column(name = "name", unique = true, nullable = false)
private String name;
@Column(name = "activity", nullable = false)
private String activity;
@Column(name = "address", nullable = false)
private String address;
@Column(name = "city", nullable = false)
private String city;
@Column(name = "logo", nullable = true)
private String logo;
@OneToMany(cascade = {CascadeType.ALL}, mappedBy = "company", fetch = FetchType.LAZY)
@JsonManagedReference
private Set<Contact> contacts = new HashSet<Contact>();
}
@Entity
public class Contact implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Column(name = "email", unique = true, nullable = false)
private String email;
@Column(name = "phone", unique = true, nullable = false)
private long phone;
@Column(name = "firstName", nullable = false)
private String firstName;
@Column(name = "lastName", nullable = false)
private String lastName;
@Column(name = "job", nullable = false)
private String job;
@Column(name = "birthday", nullable = true)
private Date birthday;
@ManyToOne
@JoinColumn(name = "fk_company", nullable = false)
@JsonBackReference
private Company company;
}
以下是保存/更新公司或联系人的代码
@RestController
public class CompanyRestService {
@Autowired
private CompanyRepository companyRepository;
@RequestMapping(value="/companies", method=RequestMethod.POST)
@ResponseBody
public Company save(Company c){
return companyRepository.save(c);
}
@RequestMapping(value="/companies/{id}", method=RequestMethod.PUT)
@ResponseBody
public Company update(@PathVariable Long id, Company c){
c.setId(id);
return companyRepository.save(c);
}
}
ving @RestController
public class ContactRestService {
@Autowired
private ContactRepository contactRepository;
@RequestMapping(value="/contacts", method=RequestMethod.POST)
@ResponseBody
public Contact save(Contact c){
return contactRepository.save(c);
}
@RequestMapping(value="/contacts/{id}", method=RequestMethod.PUT)
@ResponseBody
public Contact update(@PathVariable Long id, Contact c){
c.setId(id);
return contactRepository.save(c);
}
}
我已经尝试保存并更新没有“OneToMany”映射的联系人,它确实对我有用,我想知道这是否是映射问题? 你知道可能导致这个问题的原因吗?