org.hibernate.id.IdentifierGenerationException:“试图从空一对一属性分配id"; [英] org.hibernate.id.IdentifierGenerationException: "attempted to assign id from null one-to-one property"

查看:29
本文介绍了org.hibernate.id.IdentifierGenerationException:“试图从空一对一属性分配id";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 Spring Boot 2.1.6.RELEASE 和 Spring Data JPA 将用户对象保存到数据库时遇到问题.

I have a problem when i try to save the user object to database using Spring Boot 2.1.6.RELEASE and Spring Data JPA.

  1. 用户对象和详细信息对象具有双向的一对一关系.
  2. detail 的 id 有一个指向用户 id 的外键(用户的 id 是自动递增的).
  3. 用户信息从json格式映射到@RequestBody对象.

json:

{"name" : "Jhon",
    "detail":
    {
        "city" : "NY"
    }
}

userController.java:

userController.java:

...
@PostMapping(value="/user")
public Boolean agregarSolicitiud(@RequestBody User user) throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
    userRepository.save(user);
...

User.java:

...
@Entity
public class User {

    @Id
    @Column(updatable=false, nullable=false, unique=true)
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @Column
    private String name;

    @OneToOne(mappedBy =     "solicitud",optional=false,cascade=CascadeType.ALL)
    private UserDetail userDetail;
}

UserDetail.java:

UserDetail.java:

...
@Entity
public class UserDetail {

    @Id
    @Column
    private Long id;

    @Column
    private String city;

    @MapsId
    @OneToOne(optional = false,cascade = CascadeType.ALL)
    @JoinColumn(name = "id", nullable = false)
    private User user;
}

userRepository.java

userRepository.java

...
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
...

错误:

 org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [proyect.model.Detail.user]

我能做什么?

谢谢

推荐答案

通过关注这篇文章,我可以保存这两个实体.

By following this post I am able to save both the entities.

https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/

把这看作是在不使用持久化提供程序的情况下设置两个 java 类之间的关系.这些属性需要由开发人员手动设置,以便它可以从他想要的方向访问关系.

Think of this as setting a relationship between the two java classes without using the persistence provider.These properties need to be set manually by the developer so that it can access the relation from the direction he wants.

此代码需要添加到用户实体中,以适当绑定userdetail

This code needs to be added to the user entity which binds the userdetail appropriately

    public void setUserDetail(UserDetail userDetail) {
      if (userDetail == null) {
            if (this.userDetail != null) {
                this.userDetail.setUser(null);
            }
        } else {
            userDetail.setUser(this);
        }
        this.userDetail = userDetail;
    }
````
This code sets the user to the userDetails which is causing the issue.

As mentioned in the comments the deserializer is not able to bind the objects properly.The above code will binds the userDetails.user.


这篇关于org.hibernate.id.IdentifierGenerationException:“试图从空一对一属性分配id";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆