How do i save jpa @OneToOne/@OneToMany with auto-generated id?

Viewed 73

I want a save entity with related entities.

This is my entities.

UserEntity.java

@entity
public class UserEntity{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "uid")
    private int uid;

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

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "user_uid", insertable = false, updatable = false)
    private ProductEntity productEntity;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "user_uid", insertable = false, updatable = false)
    private List<SalesEntity> list;
}

ProductEntity.java

@entity
public class ProductEntity{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "uid")
    private int uid;

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

    @Column(name = "user_uid")
    private int userUid;
}

SalesEntity.java

@entity
public class SalesEntity{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "uid")
    private int uid;

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

    @Column(name = "user_uid")
    private int userUid;
}

I want save UserEntity and ProductEntity, SalesEntity list once.

And I can save all data using userRepository.save(userEntity).

But, UserEntity.uid(Generated Value) is not inserted in ProductEntity and SalesEntity. Inserted with 0.

How can i insert auto-generated-id to cascaded entities?

0 Answers
Related