title: Hibernate Map throws - ORA-01722: invalid number error
body: <p>I am trying to create a simple program using Map with Hibernate. I have a Country entity with a Map of State's. Here are my classes:</p>
  
  <pre><code>@Entity
  public class Country implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @Column(name = "id")
      @GeneratedValue
      private int id;
  
      @Column(name = "name")
      private String name;
  
      @OneToMany(cascade = CascadeType.ALL)
      @JoinColumn(name = "country_id")
      @MapKeyColumn(name = "id")
      private Map&lt;String, State&gt; states;
  // setters &amp; getters
  }
  
  
  
  @Entity
  @Table(name = "state")
  public class State {
      @Id
      @Column(name = "id")
      @GeneratedValue
      private int id;
  
      @Column(name = "name")
      private String name;
  // setters &amp; getters
  }
  </code></pre>
  
  <p>Here is my program that creates some countries and states:</p>
  
  <pre><code>public class AppTest {
      private static final SessionFactory concreteSessionFactory;
          static {
           try {
                  concreteSessionFactory = HibernateUtil.getSessionFactory();
            } catch (Throwable ex) {
              throw new ExceptionInInitializerError(ex);
            }
          }
          public static Session getSession()
                  throws HibernateException {
              return concreteSessionFactory.openSession();
          }
  
          public static void main(String... args){
              saveCountries();
              showCountries();
         }
  
          private static void saveCountries() {
              saveCountry("US", "CA", "Texas");
              saveCountry("UK", "London", "Cambridge");           
          }
  
          private static void saveCountry(String countryName, String... states) {
              Session session=getSession();
              session.beginTransaction();
  
              Country country = new Country();
              country.setName(countryName);
  
              Map&lt;String,State&gt; stateMap = new HashMap&lt;String, State&gt;();
              int count = 0;
              for (String stateName : states) {
                  State state = new State();
                  state.setName(stateName);
                  stateMap.put(stateName+ count++, state);
              }
              country.setStates(stateMap);
              session.save(country);
              session.close();
          }
  
          private static void showCountries() {
              Session session=getSession();
              session.beginTransaction();
              Country c=(Country)session.get(Country.class, new Integer(1));
              Map&lt;String,State&gt; states = c.getStates();
              Iterator entries = states.entrySet().iterator();
              while (entries.hasNext()) {
                  Map.Entry&lt;String,State&gt; entry = (Map.Entry) entries.next();
                  String key = entry.getKey();
                  State value = (State)entry.getValue();
                  System.out.println("Key = " + key + ", Value = " + value.getName());
              }
              session.close();
          }
      }
  </code></pre>
  
  <p>When I try to run this program then I am getting exception as :</p>
  
  <pre><code>Exception in thread "main" org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [org.hibernate.examples.State#0]
  </code></pre>
  
  <p>I am getting this exception at line <code>session.save(country);</code> Please let me know why I am getting this error?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Based on answer given by JB, now I added <code>@GeneratedValue</code> to my Id's in Country and State. This time I started getting exception as:</p>
  
  <pre><code>Aug 22, 2014 1:44:49 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  WARN: SQL Error: 1722, SQLState: 42000
  Aug 22, 2014 1:44:49 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  ERROR: ORA-01722: invalid number
  </code></pre>
  
  <p>Also below are DDL and DML operations that Hibernate has generated:</p>
  
  <pre><code>Hibernate: create table Country (id number(10,0) not null, name varchar2(255 char), primary key (id))
  Hibernate: create table state (id number(10,0) not null, name varchar2(255 char), country_id number(10,0), primary key (id))
  Hibernate: alter table state add constraint FK_lxoqjm8644epv72af3k3jpalx foreign key (country_id) references Country
  Hibernate: create sequence hibernate_sequence
  Aug 22, 2014 1:44:49 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
  INFO: HHH000230: Schema export complete
  Hibernate: select hibernate_sequence.nextval from dual
  Hibernate: select hibernate_sequence.nextval from dual
  Hibernate: select hibernate_sequence.nextval from dual
  Hibernate: insert into Country (name, id) values (?, ?)
  Hibernate: insert into state (name, id) values (?, ?)
  Hibernate: insert into state (name, id) values (?, ?)
  Hibernate: update state set country_id=?, id=? where id=?
  </code></pre>
  
  <p>I am not clear on where I am still missing.</p>