jpa hibernate one-to-many bidirectional

Nicolas Sagala Beaucage

I have an issue with one of my One-To-Many relationships. My problem is when I add a Tenant into an Apartment it doesn't set the column value in the Tenant to the ApartmentID. The thing is I have the exact same relationship with other classes and they are working fine... Does anyone have an idea why it's not wotking? Thanks

Apartment :

@Entity
public class Apartment {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="ApartmentID",unique = true, nullable = false)
    public int apartmentID;

    @OneToMany(mappedBy = "apartment", cascade = CascadeType.ALL)
    private List<Tenant> tenants;
}

Tenant :

@Entity
public class Tenant {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="TenantID",unique = true, nullable = false)
    public int tenantID;

    @ManyToOne
    @JoinColumn(name = "ApartmentID")
    private Apartment apartment;
}
nickmesi

In your bi-directional relationship Tenant is the owner of the relationship. Since you are persisting Apartment you have to manually set the apartment of tenant. If you want to get rid of manual setting change your @OneToMany as follows:

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="ApartmentID",referencedColumnName="ApartmentID")
private List<Tenant> tenants;

By the way, with your current sample if you are persisting tenant with apartment; apartment will be automatically persisted.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Hibernate / JPA optional one-to-one bidirectional mapping

From Dev

Bidirectional Many to One Mapping

From Dev

many to one relationship in spring hibernate jpa

From Dev

How to describe one to many relationship (JPA/HIbernate)

From Dev

Dropwizard/Hibernate: persisting objects with bidirectional one-many relationships gives error "NULL not allowed for column"

From Dev

JPA/Hibernate Bidirectional Set<Object> on both entities

From Dev

Unidirectional one to many association in Hibernate / spring-data-jpa

From Dev

JPA Hibernate Lazy many-to-one fetch proxy

From Dev

Duplicate Entry Exception: Spring Hibernate/JPA cascade save Many To One

From Dev

how to avoid updating parent in JPA Hibernate One-to-Many

From Dev

JPA 2.1 hibernate one-to-many contraint violation duplicate entry

From Dev

Java: Many-to-one bidirectional lookup

From Dev

Bidirectional many-to-one with subclasses and generics

From Dev

How to save multiple entity in one action related in Many to Many Relationship [Spring Boot 2, JPA, Hibernate, PostgreSQL]

From Dev

JPA Repository many to one

From Dev

Hibernate One To Many

From Dev

doubts many to many relationships in hibernate JPA

From Dev

JPA Hibernate Many to Many NestedServletException issue

From Dev

doubts many to many relationships in hibernate JPA

From Dev

When to use one to many or many to one in JPA

From Dev

Hibernate JPA many-to-one self-join association cascade, how to select the top hierarchy and map children?

From Dev

Spring MVC REST JPA Hibernate Jackson infinite recursion one-to-many JSON error

From Dev

Spring MVC REST JPA Hibernate Jackson infinite recursion one-to-many JSON error

From Dev

Hibernate/JPA auto creates foreign key instead of using exisiting foreign key in many to one association

From Dev

An extra property makes Grails interpret relationship as bidirectional many-to-one

From Dev

Persist nested entities (one-to-many) with a bidirectional relationship

From Dev

How to create a bidirectional one-to-many relationship with a composite id in the parent?

From Dev

JPA one to many relationship query

From Dev

Spring JPA REST One to Many

Related Related

  1. 1

    Hibernate / JPA optional one-to-one bidirectional mapping

  2. 2

    Bidirectional Many to One Mapping

  3. 3

    many to one relationship in spring hibernate jpa

  4. 4

    How to describe one to many relationship (JPA/HIbernate)

  5. 5

    Dropwizard/Hibernate: persisting objects with bidirectional one-many relationships gives error "NULL not allowed for column"

  6. 6

    JPA/Hibernate Bidirectional Set<Object> on both entities

  7. 7

    Unidirectional one to many association in Hibernate / spring-data-jpa

  8. 8

    JPA Hibernate Lazy many-to-one fetch proxy

  9. 9

    Duplicate Entry Exception: Spring Hibernate/JPA cascade save Many To One

  10. 10

    how to avoid updating parent in JPA Hibernate One-to-Many

  11. 11

    JPA 2.1 hibernate one-to-many contraint violation duplicate entry

  12. 12

    Java: Many-to-one bidirectional lookup

  13. 13

    Bidirectional many-to-one with subclasses and generics

  14. 14

    How to save multiple entity in one action related in Many to Many Relationship [Spring Boot 2, JPA, Hibernate, PostgreSQL]

  15. 15

    JPA Repository many to one

  16. 16

    Hibernate One To Many

  17. 17

    doubts many to many relationships in hibernate JPA

  18. 18

    JPA Hibernate Many to Many NestedServletException issue

  19. 19

    doubts many to many relationships in hibernate JPA

  20. 20

    When to use one to many or many to one in JPA

  21. 21

    Hibernate JPA many-to-one self-join association cascade, how to select the top hierarchy and map children?

  22. 22

    Spring MVC REST JPA Hibernate Jackson infinite recursion one-to-many JSON error

  23. 23

    Spring MVC REST JPA Hibernate Jackson infinite recursion one-to-many JSON error

  24. 24

    Hibernate/JPA auto creates foreign key instead of using exisiting foreign key in many to one association

  25. 25

    An extra property makes Grails interpret relationship as bidirectional many-to-one

  26. 26

    Persist nested entities (one-to-many) with a bidirectional relationship

  27. 27

    How to create a bidirectional one-to-many relationship with a composite id in the parent?

  28. 28

    JPA one to many relationship query

  29. 29

    Spring JPA REST One to Many

HotTag

Archive