I have 2 classes: User and Company.
User
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "company_id")
public Company getCompany() {
return company;
}
Company
@OneToMany(mappedBy = "company", fetch = FetchType.LAZY)
public List<User> getEmployees() {
return employees;
}
I need when I delete Company to set foreign key company_id as null in User table. How could I do this?
Typically the relationship you describe would be created with a constraint
on the Foreign Key that references the User
entity. This is so you can't set it to null
. If you set it to null, it becomes an orphaned
row, which makes for a bad database.
If, on the other hand, you need the two entities to persist separately from their joins, then your relationship is not as you are describing it.
It seems that in this case Companies and Users (Persons?) both need to exist without necessarily being joined. I would suggest a new entity, something like Employment
. It would effectively act as a join table
in the sense that it has both Company
and User
id's, but it could also have more information such as startdate
, enddate
, and current employer
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments