title: Getting Hibernate: select nextval ('hibernate_sequence') error while inserting data using hibernate into Postgres
body: <p>I am new to Hibernate. I am using Postgres as my database. I have created a simple book class and trying to store data into database table named <code>book</code></p>
  
  <p>This is my Book class:</p>
  
  <pre><code>    package com.techno.domain;
  
  public class Book {
      private int id;
      private String name;
      private String authur;
      private String year_published;
      private String genere;
      private int no_of_piece;
  
      /**
       * Default Constructor
       */
      public Book() {
          super();
      }
  
      /**
       * @param name
       * @param authur
       * @param year_published
       * @param genere
       * @param no_of_piece
       */
      public Book(String name, String authur, String year_published, String genere, int no_of_piece) {
          super();
          this.name = name;
          this.authur = authur;
          this.year_published = year_published;
          this.genere = genere;
          this.no_of_piece = no_of_piece;
      }
  
      /**
       * @param id
       * @param name
       * @param authur
       * @param year_published
       * @param genere
       * @param no_of_piece
       */
      public Book(int id, String name, String authur, String year_published, String genere, int no_of_piece) {
          super();
          this.id = id;
          this.name = name;
          this.authur = authur;
          this.year_published = year_published;
          this.genere = genere;
          this.no_of_piece = no_of_piece;
      }
  
      /**
       * @return the id
       */
      public int getId() {
          return id;
      }
  
      /**
       * @param id
       *            the id to set
       */
      public void setId(int id) {
          this.id = id;
      }
  
      /**
       * @return the name
       */
      public String getName() {
          return name;
      }
  
      /**
       * @param name
       *            the name to set
       */
      public void setName(String name) {
          this.name = name;
      }
  
      /**
       * @return the authur
       */
      public String getAuthur() {
          return authur;
      }
  
      /**
       * @param authur
       *            the authur to set
       */
      public void setAuthur(String authur) {
          this.authur = authur;
      }
  
      /**
       * @return the year_published
       */
      public String getYear_published() {
          return year_published;
      }
  
      /**
       * @param year_published
       *            the year_published to set
       */
      public void setYear_published(String year_published) {
          this.year_published = year_published;
      }
  
      /**
       * @return the genere
       */
      public String getGenere() {
          return genere;
      }
  
      /**
       * @param genere
       *            the genere to set
       */
      public void setGenere(String genere) {
          this.genere = genere;
      }
  
      /**
       * @return the no_of_piece
       */
      public int getNo_of_piece() {
          return no_of_piece;
      }
  
      /**
       * @param no_of_piece
       *            the no_of_piece to set
       */
      public void setNo_of_piece(int no_of_piece) {
          this.no_of_piece = no_of_piece;
      }
  }
  </code></pre>
  
  <p>My hibernate.cfg.xml file:</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://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
  
          &lt;!-- Database connection settings --&gt;
          &lt;property name="connection.driver_class"&gt;org.postgresql.Driver&lt;/property&gt;
          &lt;property name="connection.url"&gt;jdbc:postgresql://localhost:5432/library&lt;/property&gt;
          &lt;property name="connection.username"&gt;test&lt;/property&gt;
          &lt;property name="connection.password"&gt;password&lt;/property&gt;
  
          &lt;!-- SQL dialect --&gt;
          &lt;property name="dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/property&gt;
  
          &lt;!-- Echo all executed SQL to stdout --&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
          &lt;!-- &lt;property name="hibernate.hbm2ddl.auto"&gt;validate&lt;/property&gt; --&gt;
          &lt;!-- Use XML-based mapping metadata --&gt;
          &lt;mapping resource="com/techno/domain/Book.hbm.xml"/&gt;
  
          &lt;!-- Use Annotation-based mapping metadata --&gt;
          &lt;!-- &lt;mapping class="com.techno.entity.Book" /&gt; --&gt;
  
  
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>My Book.hbm.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;
  
  &lt;hibernate-mapping&gt;
      &lt;class name="com.techno.domain.Book" table="book"&gt;
          &lt;id name="id" type="int" column="id"&gt;
              &lt;generator class="sequence"&gt;
                  &lt;param name="sequence"&gt;book_id_seq&lt;/param&gt;
              &lt;/generator&gt;
  
          &lt;/id&gt;
          &lt;property name="authur" type="string" column="authur" /&gt;
          &lt;property name="genere" type="string" column="genre" /&gt;
          &lt;property name="name" type="string" column="name" /&gt;
          &lt;property name="year_published" type="string" column="year_published" /&gt;
          &lt;property name="no_of_piece" type="int" column="no_of_piece" /&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>I am not able to figure it out where I am doing this wrong. I am constantly getting this error.</p>
  
  <pre><code>log4j:WARN No appenders could be found for logger (org.jboss.logging).
  log4j:WARN Please initialize the log4j system properly.
  log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
  Hibernate: select nextval ('hibernate_sequence')
  Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
      at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
      at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
      at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
      at org.hibernate.id.enhanced.SequenceStructure$1.getNextValue(SequenceStructure.java:96)
      at org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:40)
      at org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:417)
      at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
      at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
      at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
      at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
      at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
      at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
      at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:678)
      at org.hibernate.internal.SessionImpl.save(SessionImpl.java:670)
      at org.hibernate.internal.SessionImpl.save(SessionImpl.java:665)
      at com.techno.libraryOwner.LibraryOwner.main(LibraryOwner.java:20)
  Caused by: org.postgresql.util.PSQLException: ERROR: relation "hibernate_sequence" does not exist
    Position: 17
      at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2284)
      at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2003)
      at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:200)
      at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:424)
      at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:161)
      at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:114)
      at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
      ... 13 more
  </code></pre>