简体   繁体   中英

foreign key not getting inserted when hibernate one to one mapping is used

I have two entites as follows

user entity

@Entity
@Table(name = "user")
@Getter
@Setter
public class UserEntity{

    @Id
    @GeneratedValue(strategy = GeneratedType.IDENTITY)
    @Column(name = "user_id")
    private Long userId;

    @Column(name = "user_name")
    private String name;

    @OneToOne(Cascade=CascadeType.ALL, mappedBy="user")
    private UserAddressEntity addressEntity;
}

user address entity

@Entity
@Table(name = "user_address")
@Getter
@Setter
public class UserAddressEntity{
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "user_id")
    private Long userId;

    @Column(name = "address")
    private String address;

    @OneToOne
    @JoinColumn(name = "user_id", nullable = false, referencedColumnName = "user_id")
    private UserEntity user;
}

I am saving user entity as follows in service class

@Service
public class UserService{

    @Autowired
    private UserRepository userRepository;

    public void saveUser(){
        UserEntity userEntity=new UserEntity();
        UserAddressEntity userAddressEntity=new UserAddressEntity();

        //logic here

        userEntity.setAddressEntity(userAddressEntity);
        userRepository.save(userEntity);
    }
}

After saving entity user_id column is not being saved in user_address table. It's value is saved as null.I can't seem to find where the issue is.

EDIT: I have added following to entities after answer referred in comments but I am getting this error

Infinite recursion (StackOverflowError)

@Entity
@Table(name = "user")
@Getter
@Setter
public class UserEntity{

    @Id
    @GeneratedValue(strategy = GeneratedType.IDENTITY)
    @Column(name = "user_id")
    private Long userId;

    @Column(name = "user_name")
    private String name;

    @OneToOne(Cascade=CascadeType.ALL, mappedBy="user")
    private UserAddressEntity addressEntity; 

    //added this
    public void setAddressEntity(UserAddressEntity addressEntity){
        if (!addressEntity.equals(this.addressEntity)){
            this.addressEntity=addressEntity;
            addressEntity.setUser(this);
        }

    }
@Entity
@Table(name = "user_address")
@Getter
@Setter
public class UserAddressEntity{
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "user_id")
    private Long userId;

    @Column(name = "address")
    private String address;

    @OneToOne
    @JoinColumn(name = "user_id", nullable = false, referencedColumnName = "user_id")
    private UserEntity user;

    //added this
    public void setUser(UserEntity user){
        if (!user.equals(this.user){
            this.user=user;
        }
    }
}

You need to set the relationship correctly in UserService.saveUser() :

 userEntity.setAddressEntity(userAddressEntity);
 userAddressEntity.setUser(userEntity);

Also, use regular getters/setters, no need to add logic there.

The recommended/best way to map a @OneToOne is to use @MapsId. With that approach, you don't need at all a bidirectional association.

I would advise you to take look at link below, there you have answer to your question with example how to save and map between objects.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM