【问题标题】:Spring Rest - Field with @ManyToOne mapping is nullSpring Rest - 具有@ManyToOne 映射的字段为空
【发布时间】:2018-02-17 13:15:22
【问题描述】:

我有以下情况:

  1. 2 个实体,用户和公司;
  2. 我正在使用 H2 数据库;
  3. 我坚持一个公司,一切正常,返回 id 1;
  4. 当我尝试将用户保存到公司时,我在数据库中的字段 company_id 为空。我不知道发生了什么!查看我的 json 以保留用户:

    { “身份证”:空, "name": "我的名字", "登录": "我的登录", "邮件":"my@mail.com", “密码”:“秘密”, “联邦ID”:“0000000”, “公司”:{“身份证”:1} }

我不会发布 Controller 和其他类,只会显示解决这个问题的重要性。在您的地图使用的类下方。

用户类:

@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;
}

公司类:

@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);
    }

}

【问题讨论】:

  • 看看一些控制器代码会很有用。 JSON 是如何转换成实际的 Java User 对象的?
  • @VitorSantos 我已经更新了我的帖子,让你看到我的用户控制器。
  • 我假设 JSON 正在由 Spring REST 转换。我的猜测是,如果您按原样调试控制器,您会看到用户对象中的公司属性为空。我会尝试将 JSON 传递为:{ "id": null, "name": "My Name", "login": "My Login", "mail":"my@mail.com", "password":"secret", "federalID":"0000000", "company": {id: 1} },并将公司作为 JSON 对象。
  • @VitorSantos,最糟糕的是我已经做到了!但它不起作用。

标签: java spring-data h2 spring-rest


【解决方案1】:

如您所见,Company 是您的 User 类的一个关系。由于这一事实,Hibernate 无法将其识别为已初始化的实体。 要解决此问题,您应该使用 company_id 手动加载公司,并将加载的公司设置为 User 类 insideUserController。

【讨论】:

  • 阿卜杜拉,谢谢你的提问,你解释的方式确实可以使用,但我用另一种方式解决了这个问题。
  • 有必要对我的代码进行一些调整,但解决问题的基础,基本上是关注关于 Jackson – 双向关系 的帖子。您可以在此处找到帖子:baeldung.com/… 我使用 User 类中的 @JsonManagedReference 注释和我的 @JsonBackReference 注释解决了我的问题公司 类。除了两个类中的 bteer 模式外,我还在 Lombok 注释中放置了一个 Serializable 实现和一些调整。
  • 在公司类中我这样做:@JsonBackReference(value = "users") private List users。在用户类中,我这样做:@JsonManagedReference 私人公司公司。
  • 用户类:@NotNull @ManyToOne @JoinColumn(name = "company_id", nullable = false) 私人公司公司;
  • 公司类:@OneToMany(mappedBy = "company") @JsonBackReference(value = "users") 私有列表 users;
猜你喜欢
  • 2020-09-14
  • 2016-03-21
  • 2022-07-12
  • 2014-11-02
  • 1970-01-01
  • 2017-08-13
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多