-1

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:

Controller code for inserting

User.java

Employer.java

2 Answers2

1

Why is userId always 0?

Your userId property does not have any annotations that tell JPA that it is a foreign key. Therefore, userId is treated like a normal field. When Employee gets instantiated, userId will have no value. When a primitive int has no value, it defaults to 0.

Proposed solution

Change the property from int userId to User user and annotate it with the following @OneToOne annotation:

@NotNull
@OneToOne(mappedBy = "employee")
User user;

Then to get the userId, simply call user.getId().

How to use @JoinColumn

This specifies the column in your table. To keep things simple, use it to annotate your foreign key property and give it a suitable name. Make sure it doesn't conflict with existing column names.

@JoinColumn(name = "employee_id")
Employee employee;

You can leave out referencedColumnName, as this will automatically be set to the primary key column name of the referenced entity.

For a more detailed answer on the annotations, please have a look at this answer for a nice example.

Sync
  • 3,571
  • 23
  • 30
  • I added it and got exception: Caused by: org.hibernate.AnnotationException: Unknown mappedBy in: com.foo.dto.Employee.userId, referenced property unknown: int.employee – Stiliyan Koev Nov 20 '17 at 12:30
  • Okay, but if I change userId to User user, how should I map this: @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "id", referencedColumnName = "userId") Employee employee; – Stiliyan Koev Nov 20 '17 at 12:42
  • ConstraintViolationImpl{interpolatedMessage='may not be null' – Stiliyan Koev Nov 20 '17 at 14:04
  • Did you remove the `USER_ID` column in your database? If not, please delete the column. Because it is now named `employee_id`, the column is unused. – Sync Nov 20 '17 at 14:05
  • Yes, I dropped all the tables and still: ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=user, – Stiliyan Koev Nov 20 '17 at 14:19
  • Could you post the entire stacktrace (perhaps edit it in your original question). That little snippet error does not really help. – Sync Nov 20 '17 at 14:22
  • I added some pictures. If we solve this out, I will be really thankfull !!! – Stiliyan Koev Nov 20 '17 at 14:30
  • The error is saying the property `user` in `Employer` is `null`. This makes sense, because in your method `test()` you've never set `user`. To do so, use: `employer.setUser(userTwo);` – Sync Nov 20 '17 at 14:54
  • Now it is inserting only User, and no Employer. Also user field EmployerId is null ... – Stiliyan Koev Nov 20 '17 at 15:31
0

First of all change

@NotNull
int userId;

To

@NotNull
@OneToOne(mappedBy = "employee")
User user;

Second, change

@JoinColumn(name = "id", referencedColumnName = "userId")
Employee employee;

To

@JoinColumn(name = "employee_id")
    Employee employee;

Because you already have column named "id" above (your primary key). Also you have to delete useId column from employee table.

Anton Kolosok
  • 482
  • 9
  • 24