title: Hibernate - persist automatically each relation
body: <p>Maybe it is a simple question, but I can't find out this situation of relations in Hibernate.</p>
  
  <p>I have these Entities:</p>
  
  <pre><code>@Entity
  public class User {
    ...
    @OneToMany(mappedBy = "user")
    private Set&lt;Conversation&gt; posts = new HashSet&lt;Conversation&gt;();
    ...
  }
  
  @Entity
  public class Conversation { 
    ...
    @OneToMany(mappedBy = "conversation")
    private Set&lt;Message&gt; messages = new HashSet&lt;Message&gt;();
    ...
  }
  
  @Entity
  public class Message { ... }
  </code></pre>
  
  <p>and then I want create User with Conversation and Message at once. Idea should be like this:</p>
  
  <pre><code>User user = new User();
       user.getPosts().add(new Conversation(){
          {
              getMessages().add(new Message());
          }
       });
  
  session.persist(user);
  </code></pre>
  
  <p>But just User is saved in database - why isn't it all? Because of default LAZY fetching? Could my idea be implemented somehow?</p>
  
  <p>PS: Of course I know about the solution of persisting each of the entities, but I am used to do like this in other frameworks like nette or Django, so I can't get out of my head.</p>
  
  <p>PPS: I found out that problem is in default CascadeType. Could it be set on globally, e.g. in Hibernate config XML? Is it a good idea (by performance point of view - it is persisted each time on "superpersist" or only in case of changes)? </p>
  
  <p>PPPS: I also found out (opposite to Django) that I have to set FK ex-post for each item added to collection. It is natural (because of selected pure Set type), but new for me. Which approach would you recommend me? Required FK as argument in constructor on item Entity e.g.:</p>
  
  <pre><code>  Class Message{
      Message(Conversation conversation){
         setConversation(conversation);
      }
      ...
    }
  </code></pre>
  
  <p>or make a method for adding where FK sets inside e.g.:</p>
  
  <pre><code>  Class Conversation{
      ...
      public void addMessage(Message msg){
         msg.setConversation(this);
         getMessages().add(msg);
      }
      ...
    }
  </code></pre>
  
  <p>?</p>
  
  <p>Making Session + configure XML.</p>
  
  <pre><code>private final static String CFG = "hibernate-cfg.xml";
      private final static String SCRIPT_FILE = "query.sql";
  
      private static SessionFactory sessionFactory;
  
      private static ServiceRegistry buildRegistry() {
          return new StandardServiceRegistryBuilder()
                  .configure(CFG)
                  .build();
      }
  
      private static Metadata getMetaData() {
          return new MetadataSources(buildRegistry()).getMetadataBuilder().build();
      }
  
      private static SessionFactory buildSessionFactory() {
          return getMetaData().getSessionFactoryBuilder().build();
      }
  
      public static SessionFactory getSessionFactory() {
          if (sessionFactory == null) {
              sessionFactory = buildSessionFactory();
          }
          return sessionFactory;
      }
  
      public static Session getSession(){
          try {
              return getSessionFactory().openSession();
          } catch (Exception e) {
              e.printStackTrace();
              return null;
          }
      }
  </code></pre>
  
  <p>and the hibernate-cfg.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
    &lt;session-factory&gt;
      &lt;property name="connection.driver_class"&gt;
              com.mysql.jdbc.Driver
          &lt;/property&gt;
      &lt;property name="connection.url"&gt;
              jdbc:mysql://localhost:3306/learnme
          &lt;/property&gt;
      &lt;property name="connection.username"&gt;root&lt;/property&gt;
      &lt;property name="connection.password"/&gt;
      &lt;property name="connection.pool_size"&gt;100&lt;/property&gt;
      &lt;!-- SQL dialect --&gt;
      &lt;property name="hibernate.dialect"&gt;
              org.hibernate.dialect.MySQL5Dialect
          &lt;/property&gt;
      &lt;!-- Enable Hibernate's automatic session context management --&gt;
      &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
      &lt;!-- Disable the second-level cache --&gt;
      &lt;property name="cache.provider_class"&gt;
              org.hibernate.cache.NoCacheProvider
          &lt;/property&gt;
      &lt;!-- Display all generated SQL to stdout --&gt;
      &lt;property name="show_sql"&gt;true&lt;/property&gt;
      &lt;!-- Drop and re-create the database schema on startup --&gt;
      &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;
      &lt;mapping class="learnme.hibernate.entities.User"/&gt;
      &lt;mapping class="learnme.hibernate.entities.Conversation"/&gt; 
      &lt;mapping class="learnme.hibernate.entities.Message"/&gt;
    &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>