Hibernate overwrites persisted values to NULL

I have two entities with a one to many relationship. They are Continent and Country. When I want to map a new Country object with the same Continent name, the previously persisted data are overwritten with NULL values. Basically, hibernate updates the new entry with the relevant Continent, but it sets the previously persisted rows with NULL values.

Please can you have a look at my code and suggest solutions

Thanks.

Blockquote

@Entity
@Table(name=“CONTINENT”)
public class Continent {

   @Id @GeneratedValue
private int continentId;
private String name;
  @OneToMany
  @JoinColumn(name="continentId")
    private Set countries;
   //getters and setters

}

@Entity()
@Table(name=“COUNTRY”)
public class Country {

public class HibernateTest {
public static void main(String[] args) {
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();

Query query = session.createQuery(“From Continent c Where c.name=‘Africa’”)
List cont = query.getResultList();
Continent continent = cont.get(0);

Country egypt = new Country();
egypt.setName(“Egypt”);
egypt.setContinent(continent);

Set setContinent = new HashSet<>();
setContinent.add(egypt);

continent.setCountries(setContinent);

session.save(egypt);
session.save(continent);

session.getTransaction().commit();
session.close();

}

}

//Table in the Database (PostgreSQL)
countryid countryname continentid
100 Kenya Null //previously persisted data
101 Egypt 102