列 'foreign_key_id' 不能为空
Column 'foreign_key_id' cannot be null
我有休息 api 通过 @RepositoryRestResource
从 spring-data-rest-api
公开。当我尝试从 Postman 提供 json 有效负载以创建链接到组织的用户时,它抱怨 Column 'organizationId' cannot be null
,而我在 json.
中明确提供了它
{
"firstName": "Test",
"lastName": "User",
"email": "user@example.com",
"phoneNumber": "+12019582790",
"organizationId": "22bf93a5-e620-4aaf-8333-ad67391fb235",
"password": "example123",
"role": "admin",
}
每个用户都属于一个组织,因此是多对一的关系。我希望 Java 将用户所属的组织作为组织对象映射到用户中。
User.java:
@Entity
@Table(name="user")
public class User {
@ManyToOne(targetEntity = Organization.class, fetch = FetchType.EAGER)
@JoinColumn(name = "organizationId", nullable = false)
private Organization organization;
}
Organization.java:
@Entity
@Table(name = "organization")
public class Organization {
@Id
@GeneratedValue(generator = "id-generator")
@Type(type = "uuid-char")
@Column(name="organizationId")
private UUID organizationId;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "organization")
private Set<User> users;
}
非常感谢任何帮助。
这是我最终采用的方法。仍然想知道为什么 @ManyToOne
注释本身没有将 organizationId 保存为用户 table 中的外键。
User.java:
@Type(type = "uuid-char")
@Column
private UUID organizationId;
@ManyToOne
@JoinColumn(name = "organizationId", insertable = false, updatable = false)
private Organization organization;
我有休息 api 通过 @RepositoryRestResource
从 spring-data-rest-api
公开。当我尝试从 Postman 提供 json 有效负载以创建链接到组织的用户时,它抱怨 Column 'organizationId' cannot be null
,而我在 json.
{
"firstName": "Test",
"lastName": "User",
"email": "user@example.com",
"phoneNumber": "+12019582790",
"organizationId": "22bf93a5-e620-4aaf-8333-ad67391fb235",
"password": "example123",
"role": "admin",
}
每个用户都属于一个组织,因此是多对一的关系。我希望 Java 将用户所属的组织作为组织对象映射到用户中。
User.java:
@Entity
@Table(name="user")
public class User {
@ManyToOne(targetEntity = Organization.class, fetch = FetchType.EAGER)
@JoinColumn(name = "organizationId", nullable = false)
private Organization organization;
}
Organization.java:
@Entity
@Table(name = "organization")
public class Organization {
@Id
@GeneratedValue(generator = "id-generator")
@Type(type = "uuid-char")
@Column(name="organizationId")
private UUID organizationId;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "organization")
private Set<User> users;
}
非常感谢任何帮助。
这是我最终采用的方法。仍然想知道为什么 @ManyToOne
注释本身没有将 organizationId 保存为用户 table 中的外键。
User.java:
@Type(type = "uuid-char")
@Column
private UUID organizationId;
@ManyToOne
@JoinColumn(name = "organizationId", insertable = false, updatable = false)
private Organization organization;