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
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);