Spring 休息 - 带有@ManyToOne 映射的字段为空
Spring Rest - Field with @ManyToOne mapping is null
我有以下情况:
- 2 个实体,用户和公司;
- 我正在使用 H2 数据库;
- 我持久化了一个Company,一切正常,返回了id 1;
当我尝试将用户保存到公司时,我在数据库中的字段 company_id 为空。我不知道发生了什么!查看我的 json 以保留用户:
{
"id":空,
"name": "My Name",
"login": "My Login",
"mail":"my@mail.com",
"password":"secret",
"federalID":"0000000",
"company":{"id": 1}
}
我不会 post 控制器和其他 class,只是会说明解决这个问题的重要性。在您的地图使用的 classes 下方。
用户class:
@Entity(name = "users")
@Table(name = "users")
@EqualsAndHashCode
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String login;
private String mail;
private String password;
private String federalID;
@ManyToOne(optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "company_id", referencedColumnName="company_id")
private Company company;
}
公司class:
@Entity(name="companies")
@Table(name="companies")
@EqualsAndHashCode
@Data
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String federalID;
@OneToMany(mappedBy="company", targetEntity=User.class, fetch=FetchType.EAGER)
private List<User> users;
}
其他用户实体(公司控制器遵循相同的模式):
@RestController
public class UsersController {
@Autowired
UserRepository userRepository;
@RequestMapping(value = "/user", method = RequestMethod.GET)
public List<User> getUsers(){
return userRepository.findAll();
}
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable("id") Long id){
return userRepository.findById(id);
}
@RequestMapping(value = "/user", method = RequestMethod.POST)
public ResponseEntity<User> createUser(@RequestBody User company){
return new ResponseEntity<>(userRepository.save(company), HttpStatus.CREATED);
}
@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
public ResponseEntity<Void> deleteUser(@PathVariable("id") Long id){
userRepository.delete(id);
return new ResponseEntity<Void>(HttpStatus.OK);
}
}
如您所见,Company
是您 User
class 上的关系。因此,Hibernate 无法将其识别为已初始化的实体。
要解决此问题,您应该使用 company_id 手动加载公司,并将加载的公司设置到 User
class 里面 UserController
.
我有以下情况:
- 2 个实体,用户和公司;
- 我正在使用 H2 数据库;
- 我持久化了一个Company,一切正常,返回了id 1;
当我尝试将用户保存到公司时,我在数据库中的字段 company_id 为空。我不知道发生了什么!查看我的 json 以保留用户:
{ "id":空, "name": "My Name", "login": "My Login", "mail":"my@mail.com", "password":"secret", "federalID":"0000000", "company":{"id": 1} }
我不会 post 控制器和其他 class,只是会说明解决这个问题的重要性。在您的地图使用的 classes 下方。
用户class:
@Entity(name = "users")
@Table(name = "users")
@EqualsAndHashCode
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String login;
private String mail;
private String password;
private String federalID;
@ManyToOne(optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "company_id", referencedColumnName="company_id")
private Company company;
}
公司class:
@Entity(name="companies")
@Table(name="companies")
@EqualsAndHashCode
@Data
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String federalID;
@OneToMany(mappedBy="company", targetEntity=User.class, fetch=FetchType.EAGER)
private List<User> users;
}
其他用户实体(公司控制器遵循相同的模式):
@RestController
public class UsersController {
@Autowired
UserRepository userRepository;
@RequestMapping(value = "/user", method = RequestMethod.GET)
public List<User> getUsers(){
return userRepository.findAll();
}
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable("id") Long id){
return userRepository.findById(id);
}
@RequestMapping(value = "/user", method = RequestMethod.POST)
public ResponseEntity<User> createUser(@RequestBody User company){
return new ResponseEntity<>(userRepository.save(company), HttpStatus.CREATED);
}
@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
public ResponseEntity<Void> deleteUser(@PathVariable("id") Long id){
userRepository.delete(id);
return new ResponseEntity<Void>(HttpStatus.OK);
}
}
如您所见,Company
是您 User
class 上的关系。因此,Hibernate 无法将其识别为已初始化的实体。
要解决此问题,您应该使用 company_id 手动加载公司,并将加载的公司设置到 User
class 里面 UserController
.