I have a simple one-to-many
relationship between Employee
& Phone
entities.
I have 2 issues here.
Scenario 1 :
@Entity
public class Employee {
@Id
@GeneratedValue
private long id;
private String firstName;
private String lastName;
private double salary;
@OneToMany
@JoinColumn(name = "parent_id", referencedColumnName = "id", insertable=false, updatable=false)
private List<Phone> phones = new ArrayList<Phone>(0);
// Setters & Getter methods
}
Phone.java
@Entity
public class Phone {
@Id
private long id;
private String type;
private int areaCode;
private String phoneNumber;
// Setter & Getters
}
Now I created employee instance and added 2 phones to it.
Employee e1 = new Employee(10,"Bob", "Way",50000);
Phone p1 = new Phone(1,"home",613,"792-0000");
Phone p2 = new Phone(2,"work",613,"896-1234");
e1.addPhone(p1);
e1.addPhone(p2);
Then in hibernate, if I use below code:
session.save(e1);
session.save(p1);
session.save(p2);
or if I save phone
then employee
session.save(p1);
session.save(p2);
session.save(e1);
In both cases, Hibernate just runs insert
queries with out any update
queries but the foreign key
is set to NULL.
mysql> select * from Phone;
+----+----------+-------------+------+-----------+
| id | areaCode | phoneNumber | type | parent_id |
+----+----------+-------------+------+-----------+
| 1 | 613 | 792-0000 | home | NULL |
| 2 | 613 | 896-1234 | work | NULL |
| 3 | 416 | 123-4444 | work | NULL |
+----+----------+-------------+------+-----------+
How can I fix this? Please let me know where I made mistake.
Scenario 2 :
Now if I made change to my @JoinColumn
in Employee
table like below:
@JoinColumn(name = "parent_id")
Then Hibernate is generating **insert**
then update
queries to map the foreign key properly.
Hibernate: insert into Employee (firstName, lastName, salary) values (?, ?, ?)
Hibernate: insert into Phone (areaCode, phoneNumber, type, id) values (?, ?, ?, ?)
Hibernate: insert into Phone (areaCode, phoneNumber, type, id) values (?, ?, ?, ?)
Hibernate: insert into Phone (areaCode, phoneNumber, type, id) values (?, ?, ?, ?)
Hibernate: update Phone set parent_id=? where id=?
Hibernate: update Phone set parent_id=? where id=?
When Hibernate runs the insert
on Employee
table then it gets the employee id
there itself, then why does Hibernate runs additional update
query when it can just run a single insert query on Phone table by including the foreign key value?
I understand that in update
query it is adding the foreign key, but I would like to know why this is not managed in insert
query itself on Phone table. Can you please explain this?
Scenario 3:
If I say like this:
@JoinColumn(name = "OWNER_ID", insertable=false, updatable=false, nullable=false)
Then I get below exception:
Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value : onetomany.Phone._phones_OWNER_IDBackref