title: HSQL - HIBERNATE. org.hibernate.HibernateException: identifier of an instance of "....Author.class" was altered from 1 to null
body: <p><strong>Can't insert data to HSQL DB (in-memory). Please, help me to understand mistake, text from console:</strong></p>
  
  <pre><code>    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
      log4j:WARN Please initialize the log4j system properly.
      Hibernate: 
  insert 
  into
      author
      (ID, NAME, COUNTRY) 
  values
      (default, ?, ?)
      Hibernate: 
  insert 
  into
      book
      (ID, NAME, GENRE, AUTHORID) 
  values
      (default, ?, ?, ?)
      Hibernate: 
  insert 
  into
      book
      (ID, NAME, GENRE, AUTHORID) 
  values
      (default, ?, ?, ?)
      org.hibernate.HibernateException: identifier of an instance of com.maven.vaadin.bookshelf.Author was altered from 1 to null
  at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:85)
  at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:190)
  at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:147)
  at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
  at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
  at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
  at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
  at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
  at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
  at com.maven.vaadin.bookshelf.DBManager.save(DBManager.java:50)
  at com.maven.vaadin.bookshelf.MyVaadinUI.init(MyVaadinUI.java:43)
  at com.vaadin.ui.UI.doInit(UI.java:614)
  at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:223)
  at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:73)
  at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37)
  at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1371)
  at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  at java.lang.Thread.run(Thread.java:744)
      Hibernate: 
  select
      author0_.ID as ID0_,
      author0_.NAME as NAME0_,
      author0_.COUNTRY as COUNTRY0_ 
  from
      author author0_
      List size: 0
  </code></pre>
  
  <p><strong>Hibernate config of connection to DB and links to mapping files</strong></p>
  
  <pre><code>&lt;hibernate-configuration&gt;
  &lt;session-factory&gt;
      &lt;!-- Database connection settings, Connect to HSQL, IN Memory  --&gt;
      &lt;property name="dialect"&gt;org.hibernate.dialect.HSQLDialect&lt;/property&gt;
      &lt;property name="connection.driver_class"&gt;org.hsqldb.jdbcDriver&lt;/property&gt;
      &lt;property name="connection.url"&gt;jdbc:hsqldb:mem:test&lt;/property&gt;
      &lt;property name="connection.username"&gt;sa&lt;/property&gt;
      &lt;property name="connection.password"&gt;&lt;/property&gt;
      &lt;!-- JDBC connection pool (use the built-in) --&gt;
      &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
      &lt;!-- Echo all executed SQL to stdout --&gt;
      &lt;property name="show_sql"&gt;true&lt;/property&gt;&lt;property name="format_sql"&gt;true&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;!--create the database schema on startup if required --&gt;
      &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;
      &lt;mapping resource="com/maven/vaadin/bookshelf/Author.hbm.xml"/&gt;
      &lt;mapping resource="com/maven/vaadin/bookshelf/Book.hbm.xml"/&gt;
  &lt;/session-factory&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Hibernate mapping files. First:</strong></p>
  
  <pre><code>&lt;hibernate-mapping package="com.maven.vaadin.bookshelf"&gt;
  &lt;class name="com.maven.vaadin.bookshelf.Author" table="author"&gt;
      &lt;id name="id" type="java.lang.Long"&gt;
          &lt;column name="ID" /&gt;
          &lt;generator class="native" /&gt;
      &lt;/id&gt;
      &lt;property name="name" type="java.lang.String"&gt;
          &lt;column name="NAME" length="64" not-null="true" unique="true" /&gt;
      &lt;/property&gt;    
      &lt;property name="country" type="java.lang.String"&gt;
          &lt;column name="COUNTRY" length="32" not-null="true" /&gt;
      &lt;/property&gt;    
      &lt;set name="authorbook" table="book" cascade="all" inverse="true"&gt;
          &lt;key column="AUTHORID" not-null="true" /&gt;
          &lt;one-to-many class="com.maven.vaadin.bookshelf.Book" /&gt;
      &lt;/set&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Second:</strong></p>
  
  <pre><code>&lt;hibernate-mapping package="com.maven.vaadin.bookshelf"&gt;
  &lt;class name="com.maven.vaadin.bookshelf.Book" table="book"&gt;
      &lt;id name="id" type="java.lang.Long"&gt;
          &lt;column name="ID" /&gt;
          &lt;generator class="native" /&gt;
      &lt;/id&gt;
      &lt;property name="name" type="java.lang.String"&gt;
          &lt;column name="NAME" length="32" not-null="true" /&gt;
      &lt;/property&gt;
      &lt;property name="genre" type="java.lang.String"&gt;
          &lt;column name="GENRE" length="32" not-null="true" /&gt;
      &lt;/property&gt;    
      &lt;many-to-one name="author" class="com.maven.vaadin.bookshelf.Author" not-null="true"&gt;
          &lt;column name="AUTHORID" /&gt;
      &lt;/many-to-one&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Javas:</strong></p>
  
  <pre><code>public class Author implements Serializable{
  private Long id;
  private String name;
  private String country;
  private Set&lt;Book&gt; authorbook = new HashSet&lt;Book&gt;();
  
  public Author(String name, String country, Set&lt;Book&gt; authorBook){
      super();
      this.name = name;
      this.country = country;
      this.authorbook = authorbook;
  }
  public Author(){}
  
  public void setId(Long Id){this.id = id;}
  public Long getId(){return id;}
  
  public void setName(String name){this.name = name;}
  public String getName(){return name;}   
  
  public void setCountry(String country){this.country = country;}
  public String getCountry(){return country;}
  
  public void setAuthorbook(Set&lt;Book&gt; authorBook){this.authorbook = authorBook;}
  public Set&lt;Book&gt; getAuthorbook(){return authorbook;}
  
  public void addBook(Book book){
      book.setAuthor(this);
      this.authorbook.add(book);
  }}
  </code></pre>
  
  <hr>
  
  <pre><code>public class Book implements Serializable{
  private Long id;
  private String name;
  private String genre;
  private Author author;
  
  public Book(String name, String genre){
      super();
      this.name = name;
      this.genre = genre;
  }
  public Book(){}
  
  public void setId(Long id){this.id = id;}
  public Long getId(){return id;}
  
  public void setName(String name){this.name = name;}
  public String getName(){return name;}
  
  public void setGenre(String genre){this.genre = genre;}
  public String getGenre(){return genre;}
  
  public void setAuthor(Author author){this.author = author;}
  public Author getAuthor(){return author;}}
  </code></pre>
  
  <hr>
  
  <pre><code>public class DBManager {
  
  public static void main(String[] args){
      Session session = HibernateUtil.getSessionFactory().openSession();
      session.beginTransaction();
      DBManager app = new DBManager();
      app.save("Author","Country");
      app.list();
  }
  
  public void save(String authorName, String authorCountry){
      Session session = HibernateUtil.getSessionFactory().openSession();
      session.beginTransaction();
      Long id = null;
      Transaction transaction = null;
  
      try {
          transaction = session.beginTransaction();
          Author author = new Author();
          author.setName(authorName);
          author.setCountry(authorCountry);
  
          Book bk1 = new Book();
          Book bk2 = new Book();
          bk1.setName("Book1");;
          bk1.setGenre("Genre1");
  
          bk2.setName("Book2");;
          bk2.setGenre("Genre2");
          author.addBook(bk1);
          author.addBook(bk2);
          session.save(author);
  
          transaction.commit();
      } catch (HibernateException e) {
          transaction.rollback();
          e.printStackTrace();
      } finally {
          session.close();
      }
  
  }
  
  public void list() {
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction transaction = null;
      try {
          transaction = session.beginTransaction();
          @SuppressWarnings("unchecked")
          List&lt;Author&gt; list = session.createQuery("FROM Author").list();
  
          System.out.println("List size: " + (list).size());
          for (Iterator iterator = list.iterator(); iterator.hasNext();) {
              Author author = (Author) iterator.next();
          }
          transaction.commit();
      } catch (HibernateException e) {
          transaction.rollback();
          e.printStackTrace();
      } finally {
          session.close();
      }
  }}
  </code></pre>