title: Can a Hibernate entity have multiple one-to-many relationships with other separate entities?
body: <p>I have three entities, say A, B and C. A contains multiple instances of B and C. So logically, there will be two one-to-many relations from A to B and A to C. The rows are getting populated in the tables, but B and C are not having the ids of A in the foreign key columns. The codes are as follows:</p>
  
  <p>Class A:</p>
  
  <pre><code>@Entity
  public class A
  {
   private int id;
   private B[] bSet = null;
   private C[] cSet = null;
  
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   public int getId()
   {return id;}
  
   @OneToMany(cascade = CascadeType.ALL, mappedBy = "a")
   @OrderColumn(name = "bIndex")
   public B[] getBset()
   {return bSet;}
  
   @OneToMany(cascade = CascadeType.ALL, mappedBy = "a")
   @OrderColumn(name = "cIndex")
   public C[] getCset()
   {return cSet;}
  }
  </code></pre>
  
  <p>Class B:</p>
  
  <pre><code>@Entity
  public class B
  {
   private int bId;
   private A a;
  
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   public int getBId()
   {return bId;}
  
   @ManyToOne(cascade = CascadeType.ALL)
   public A getA(A a)
   {return a;}
  }
  </code></pre>
  
  <p>Class C:</p>
  
  <pre><code>@Entity
  public class C
  {
   private int cId;
   private A a;
  
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   public int getCId()
   {return cId;}
  
   @ManyToOne(cascade = CascadeType.ALL)
   public A getA(A a)
   {return a;}
  }
  </code></pre>
  
  <p>You may ask why am I setting the cascade property for the OneToMany side also in A. But if I don't do that, it raises a transient object exception like this.</p>
  
  <pre><code>org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: B
  </code></pre>
  
  <p>If I do it like this, the exception vanishes, and the data is persisted in the H2 database, but the A column in both B and C get null values. Please help me out with this.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I'm adding here the methods which I use to persist the entities. For my purpose, I had made all the entities extend from a common empty class for ease of use.</p>
  
  <pre><code>public void populateObjects()
  {
    HashMap&lt;String,ArrayList&lt;HibernateEntity&gt;&gt; persistentEntities = new HashMap&lt;String, ArrayList&lt;HibernateEntity&gt;&gt;();
  
    persistentEntities.put("A", new ArrayList&lt;HibernateEntity&gt;());
    persistentEntities.put("B", new ArrayList&lt;HibernateEntity&gt;());
    persistentEntities.put("C", new ArrayList&lt;HibernateEntity&gt;());
  
   A a = new A();
   a.bSet = new B[2];
   a.bSet[0] = new B();
   a.bSet[1] = new B();
   a.cSet = new C[1];
   a.cSet[0] = new C();
  
   persistentEntities.get("A").add(a);
   start();
   persistEntities(persistentEntities);
  }
  
  
  public synchronized boolean start() throws Exception
  {
      if(isStarted)
          return isStarted;
  
      server = Server.createTcpServer();
      server.start();
      Properties prop = new Properties();
      String propFileName = "database.properties";
      InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
      prop.load(inputStream);
  
      if(inputStream == null)
      {
          throw new FileNotFoundException("Property file "+propFileName+" not found in classpath");
      }
  
      String dbPath = prop.getProperty("db_path");
      String dbName = prop.getProperty("db_name");
      System.out.println("Full database path: "+"jdbc:h2:file:"+dbPath+dbName);
      DriverManager.getConnection("jdbc:h2:file:"+dbPath+dbName);
      Configuration configuration = new Configuration();
      configuration.configure();
      serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
      sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  
      isStarted = true;
      return isStarted;
  }
  
  
  public void persistEntities(HashMap&lt;String, ArrayList&lt;HibernateEntity&gt;&gt; persistentEntities)
  {
   Session session = openSession();
      Transaction transaction = session.beginTransaction();
  
      Set&lt;String&gt; keySet = persistentEntities.keySet();
  
      try
      {
          for(String key: keySet)
          {
  
              ArrayList&lt;HibernateEntity&gt; entities = persistentEntities.get(key);
  
              if(key.equalsIgnoreCase("A"))
              {
                  if(entities.size() &gt; 0)
                  {
                      for(HibernateEntity h: entities)
                      {
                          A a = (A)h;
                          session.save(a);                       
                      }
                  }
  
              }
              else if(key.equalsIgnoreCase("B"))
              {
                  if(entities.size() &gt; 0)
                  {
                      for(HibernateEntity h: entities)
                      {
                          session.save((B)h);
                          System.out.println("Persisted "+key);
                      }
                  }
  
              }
              else if(key.equalsIgnoreCase("C"))
              {
                  if(entities.size() &gt; 0)
                  {
                      for(HibernateEntity h: entities)
                      {
                          session.save((C)h);
                          System.out.println("Persisted "+key);
                      }
                  }
  
              }            
          }
          transaction.commit();
          System.out.println("Successfully committed");
  
      }
      finally
      {
          closeSession(session);
      }
  }
  </code></pre>