I have a relationship one-to one:
User:
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
private String firstName;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "id", referencedColumnName = "userId")
Employee employee;
Employee :
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
int id;
@NotNull
int userId;
String lastName;
userRepository:
@Repository
public interface UserRepository extends JpaRepository<User, Integer> { }
So I am creating an User and Employee by that way:
User user = new User();
user.setFirstName("George");
Employee employee = new Employee();
employee.SetLastName("Iliev");
user.setEmployee(employee);
userRepository.save(user);
The Id of user is auto incrementing and working. But when I select all from employees, the userId is always 0. Even if I insert 10 users + 10 employees, each userId is = 0.
Why ? And how to fix it ?
EDIT: Exception thrown: