How to set null to foreign key column in OneToMany relationship, Hibernate

hetsketch.

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?

K.Nicholas

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set null to foreign key column in OneToMany relationship, Hibernate

From Dev

Foreign key update anomaly: foreign keys set to null in Hibernate one-to-many relationship

From Dev

in OneTOMany mapping foreign key is inserted as null in Hibernate-MySQL

From Dev

why hibernate not set foreign key in the owner side of OneToMany

From Dev

why hibernate not set foreign key in the owner side of OneToMany

From Dev

Doctrine: How to unset (SET NULL) OneToMany relationship

From Dev

Hibernate save object (one to many relationship) foreign key is null

From Dev

Hibernate Criteria: How to retrieve table data with foreign key relationship

From Dev

HIbernate Can't delete Entity with foreign key. Foreign key gets set to null

From Dev

Composite key including foreign key in hibernate set

From Dev

Hibernate OneToMany integrity constraint violation: foreign key no parent

From Dev

Hibernate Spring - @OneToMany - Foreign key is not getting stored in database

From Dev

Hibernate Spring - @OneToMany - Foreign key is not getting stored in database

From Dev

Laravel relationship null foreign key fetching

From Dev

MSSQL Foreign Key Relationship and Null values

From Dev

Tastypie foreign key set null

From Dev

Hibernate Criterion in OneToMany relationship

From Dev

Hibernate Criterion in OneToMany relationship

From Dev

How to save a foreign key in hibernate?

From Dev

How to save a foreign key in hibernate?

From Dev

Foreign key value is inserting value as null in OneToMany mapping

From Dev

Hibernate is inserting null values in foreign key field

From Dev

Hibernate Many to one updating foreign key to null

From Dev

Null values are inserted in the foreign key fields with Hibernate

From Dev

Hibernate One to One mapping Foreign Key null

From Dev

Hibernate Many to one updating foreign key to null

From Dev

Why is Hibernate not filling this foreign key column

From Dev

Why is Hibernate not filling this foreign key column

From Dev

JPA hibernate foreign key not set in entity

Related Related

  1. 1

    How to set null to foreign key column in OneToMany relationship, Hibernate

  2. 2

    Foreign key update anomaly: foreign keys set to null in Hibernate one-to-many relationship

  3. 3

    in OneTOMany mapping foreign key is inserted as null in Hibernate-MySQL

  4. 4

    why hibernate not set foreign key in the owner side of OneToMany

  5. 5

    why hibernate not set foreign key in the owner side of OneToMany

  6. 6

    Doctrine: How to unset (SET NULL) OneToMany relationship

  7. 7

    Hibernate save object (one to many relationship) foreign key is null

  8. 8

    Hibernate Criteria: How to retrieve table data with foreign key relationship

  9. 9

    HIbernate Can't delete Entity with foreign key. Foreign key gets set to null

  10. 10

    Composite key including foreign key in hibernate set

  11. 11

    Hibernate OneToMany integrity constraint violation: foreign key no parent

  12. 12

    Hibernate Spring - @OneToMany - Foreign key is not getting stored in database

  13. 13

    Hibernate Spring - @OneToMany - Foreign key is not getting stored in database

  14. 14

    Laravel relationship null foreign key fetching

  15. 15

    MSSQL Foreign Key Relationship and Null values

  16. 16

    Tastypie foreign key set null

  17. 17

    Hibernate Criterion in OneToMany relationship

  18. 18

    Hibernate Criterion in OneToMany relationship

  19. 19

    How to save a foreign key in hibernate?

  20. 20

    How to save a foreign key in hibernate?

  21. 21

    Foreign key value is inserting value as null in OneToMany mapping

  22. 22

    Hibernate is inserting null values in foreign key field

  23. 23

    Hibernate Many to one updating foreign key to null

  24. 24

    Null values are inserted in the foreign key fields with Hibernate

  25. 25

    Hibernate One to One mapping Foreign Key null

  26. 26

    Hibernate Many to one updating foreign key to null

  27. 27

    Why is Hibernate not filling this foreign key column

  28. 28

    Why is Hibernate not filling this foreign key column

  29. 29

    JPA hibernate foreign key not set in entity

HotTag

Archive