title: Insertion of rows with primary key/foreign key using Hibernate
body: <p>In my Spring project, I am trying insert some rows in the database, using Hibernate, but I am facing two problems:</p>
  
  <p>1) In one of the tables, represented by this class:</p>
  
  <pre><code>@Entity
  @Table(name="sessao")
  public class Sessao implements java.io.Serializable {
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column
      private int id;
  
      @Column
      private Usuario usuario;
  
      @Column
      private int action;
  
      public Sessao() {
      }
  
      public Sessao(Usuario usuario, int action) {
          this.usuario = usuario;
          this.action = action;
      }
  
      public Sessao(int id, Usuario usuario, int action) {
          this.id = id;
          this.usuario = usuario;
          this.action = action;
      }
  
      public int getId() {
          return this.id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public Usuario getUsuario() {
          return this.usuario;
      }
  
      public void setUsuario(Usuario usuario) {
          this.usuario = usuario;
      }
  
      public int getAction() {
          return this.action;
      }
  
      public void setAction(int action) {
          this.action = action;
      }
  }
  </code></pre>
  
  <p>The columns <strong>Usuario</strong> is a foreign key to table of same name in the database. When I try insert one item in the database, in this way:</p>
  
  <pre><code>Usuario novo = new Usuario(username, convertByteToHex(digest));
  List&lt;Usuario&gt; lista = (List&lt;Usuario&gt;) usuario.findByExample(novo);
  novo.setId(lista.get(0).getId());
          novo.setPnome(lista.get(0).getPnome());
          novo.setUnome(lista.get(0).getUnome());
          this.setId_usuario(novo.getId());
  
          if(lista.size() == 0) {
              ModelAndView mav = new ModelAndView();
              mav.setViewName("usuario_login");
              return mav;
          }
          else {
              Sessao nova = new Sessao(novo, 0);
              sessao.persist(nova);
              ModelAndView mav = new ModelAndView();
              mav.setViewName("usuario_start");
              mav.addObject("usuario", novo);
              return mav;
          }
  </code></pre>
  
  <p>I am receiving the error message:</p>
  
  <pre><code>ERROR: null value in column "fk_usuario" violates not-null constraint
  </code></pre>
  
  <p>Someone knows how to solve this?</p>
  
  <p>2) The other situation is related to primary key, whom in the database is represented by the type <strong>serial</strong>, so when I do an insert, I don't need inform this field, and can do this way:</p>
  
  <pre><code>INSERT INTO sessao(fk_usuario, action, usuario) VALUES (?, ?, ?);
  </code></pre>
  
  <p>my other question is: all my Model classes have construtors where I don't need pass the Id as parameter. If I have created an Object with this construtor, how I could pass this object for my DAO class to save it in the database.</p>
  
  <p>the method resposable for saving the data in the database in my DAO class is:</p>
  
  <pre><code>@Transactional
  public void persist(Sessao transientInstance) {
      log.debug("persisting Sessao instance");
      try {
          sessionFactory.getCurrentSession().persist(transientInstance);
          log.debug("persist successful");
      } catch (RuntimeException re) {
          log.error("persist failed", re);
          throw re;
      }
  }
  </code></pre>