O
O
Orkhan Hasanli2020-04-25 18:31:02
Hibernate
Orkhan Hasanli, 2020-04-25 18:31:02

Why is null displayed instead of id in OneToMany & ManyToOne and how to fix it?

Good day!
I'm dealing with a pressing issue.
There are 2 entities:

@Data
@Entity
@Table(name = "hb_destinations")
public class HbDestination {

  @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "hb_destination_id", unique = true, nullable = false)
    private long hbDestinationId;

    private String name;

    @OneToMany(mappedBy = "destination", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Zone> zones = new ArrayList<>();
}


@Data
@Entity
@Table(name = "hb_destination_zones")
public class Zone {

  @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long destinationZoneId;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    /*@JoinColumn(name = "hb_destination_id",referencedColumnName="hb_destination_id")*/
    private HbDestination destination;
}


And here is the code for adding data
HbDestination hbDestination = new HbDestination();
hbDestination.setName(name);
hbDestination.setZones(zones);
/*hbDestination.getZones.addAll(zones);*/
hbDestinationRepository.save(hbDestination);


As a result:

+---------------------+-------+-------------------------------+
| destination_zone_id | name  | destination_hb_destination_id |
+---------------------+-------+-------------------------------+
|                   1 | text1 | null                          |
|                   2 | text2 | null                          |
|                   3 | text3 | null                          |
+---------------------+-------+-------------------------------+


I found this solution - https://stackoverflow.com/questions/9650453/hibern...

HbDestination hbDestination = new HbDestination();

Zone zone = new Zone
zone.setDestination(hbDestination);
zones.add(zone);
hbDestination.setZones(zones);

hbDestinationRepository.save(hbDestination);


As a result:
+---------------------+-------+-------------------------------+--+
| destination_zone_id | name  | destination_hb_destination_id |  |
+---------------------+-------+-------------------------------+--+
|                   1 | null  | 5                             |  |
|                   2 | text2 | null                          |  |
|                   3 | null  | 6                             |  |
|                   4 | text3 | null                          |  |
+---------------------+-------+-------------------------------+--+


How to correctly save a List from the HbDestination side? I can only take the list of zones and iterate over it and save for each element inside.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2020-04-25
@azerphoenix

So far, I decided in the following way:
Added a setter to HbDestination

public void setZones(List<Zone> zones) {
        zones.forEach(zone -> zone.setDestination(this));
        this.zones = zones;
    }

03/03/2021
I will supplement my answer. You need to set cascading styles.
Useful links:
https://www.baeldung.com/jpa-many-to-many
https://vladmihalcea.com/the-best-way-to-map-a-man...
https://www. baeldung.com/hibernate-many-to-many

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question