title: select nextval for every row inserted with hibernate
body: <p>It's my first time using Hibernate and when I insert multiple rows in a loop hibernate prints out the following for each row being inserted</p>
  
  <pre><code>Hibernate: select nextval ('hibernate_sequence')
  </code></pre>
  
  <p>after it does that for all the data in the loop it starts to insert the data</p>
  
  <pre><code>Hibernate: insert into user_data (age, location, sent_count, user_id, username, id) values (?, ?, ?, ?, ?, ?)
  </code></pre>
  
  <p>Is this how hibernate always operates? would it not be possible for the database to take care of the sequence? I feel like this really slows down the process of inserting rows. I am using PostgreSQL for my database.</p>
  
  <p>Here is my relevant code
  user_data </p>
  
  <pre><code>@Entity
  @Table(name = "user_data")
  public class UserData
  {
    @Id @GeneratedValue
    @Column(name = "id")
    private int id;
  
    @Column(name = "user_id")
    private String userid;
  
    @Column(name = "username")
    private String username;
  
    @Column(name = "age")
    private int age;
  
    @Column(name = "location")
    private int location;
  
    @Column(name = "sent_count")
    private int sentCount;
  
      public int getId()
      {
          return id;
      }
  
      public void setId(int id)
      {
          this.id = id;
      }
  
      public String getUserid()
      {
          return userid;
      }
  
      public void setUserid(String userid)
      {
          this.userid = userid;
      }
  
      public String getUsername()
      {
          return username;
      }
  
      public void setUsername(String username)
      {
          this.username = username;
      }
  
      public int getAge()
      {
          return age;
      }
  
      public void setAge(int age)
      {
          this.age = age;
      }
  
      public int getLocation()
      {
          return location;
      }
  
      public void setLocation(int location)
      {
          this.location = location;
      }
  
      public int getSentCount()
      {
          return sentCount;
      }
  
      public void setSentCount(int sentCount)
      {
          this.sentCount = sentCount;
      }
  }
  </code></pre>
  
  <p>My Hibernate helper class</p>
  
  <pre><code>public class HibernateUtil
  {
      static SessionFactory       sessionFactory;
      static ServiceRegistry  serviceRegistry;
  
      static
      {
          try
          {
              Configuration configuration = new Configuration();
              configuration.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
              configuration.configure();
              serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
              sessionFactory = configuration.buildSessionFactory(serviceRegistry);
          }
          catch (Throwable ex)
          {
              // Make sure you log the exception, as it might be swallowed
              System.err.println("Initial SessionFactory creation failed." + ex);
              closeSessionFactory();
              throw new ExceptionInInitializerError(ex);
          }
      }
  
      public static SessionFactory getSessionFactory()
      {
          return sessionFactory;
      }
  
      public static void closeSessionFactory()
      {
          sessionFactory.close();
      }
  
  }
  </code></pre>
  
  <p>and the relevant section which inserts the rows</p>
  
  <pre><code>    Session session = HibernateUtil.getSessionFactory().openSession();
      session.beginTransaction();
  
      for (String memberId : memberIds)
      {
          System.out.println(memberId);
          UserData user = new UserData();
          user.setUserid(memberId);
          session.save(user);
      }
  
      session.getTransaction().commit();
      session.close();
      HibernateUtil.closeSessionFactory();
  </code></pre>