ManyToOne and OneToMany

By ru51an

I use Hibernate and I have entities:

@Data
@Entity
public class Country {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;
    @Column(name = "country_nm")
    private String countryName;
}
@Data
@Entity
public class City {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;
    @Column(name = "city_nm")
    private String cityName;
}

Country can have many cities, city has only one country.
What is the best way to associate these entities?

1) add City city field in City class and add @ManyToOne and @JoinColumn anotations above it? As a result we will have two tables: country and city, city table will have country_id column.

2) add Country country field in Country class and @OneToMany(mappedBy='country')above it and add City city field in City class and add @ManyToOne anotations above it? In this case there will be three tables: country, city and combining table country_city

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273