title: hibernate: many-to-one mapping via xml
body: <p>I'm facing some difficulties, trying to set up many-to-one relationship for two tables:
  USERS and SUBSCRIBERS</p>
  
  <p>(the main idea here is that users can save references on other users)</p>
  
  <p>USERS table contain theese fields: |USER_ID|USER_PASSWORD|
  While SUBSCRIBERS conains: |SUBSCRIBER_ID|USER_ID|, where:
  1. Both fields are included in Primary Key
  2. Both fields are foreign keys referencing to USER_ID from USERS.
  There are no exception while saving User to a database.
  But when I call code: 
  (after creating 2 users inside DB - with first_u USER_ID, and second_u USER_ID)</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>hibernate_session.beginTransaction();
  subscr = new Subscription("first_u", "second_u");
  hibernate_session.save(subscr);
  hibernate_session.getTransaction().commit();
  </code></pre>
  
  <p><strong><em>I get this exception:</em></strong></p>
  
  <pre class="lang-java prettyprint-override"><code>org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of player.database.hibernate.entities.User.userID
      org.hibernate.property.access.spi.GetterMethodImpl.get(GetterMethodImpl.java:64)
      org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:223)
      org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4601)
      org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4313)
      org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:226)
      org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:276)
      org.hibernate.type.EntityType.getIdentifier(EntityType.java:455)
      org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:153)
      org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:343)
      org.hibernate.persister.entity.AbstractEntityPersister.dehydrateId(AbstractEntityPersister.java:2636)
      org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2604)
      org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2883)
      org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3386)
      org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
      org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:560)
      org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434)
      org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
      org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
      org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1282)
      org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:465)
      org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2963)
      org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2339)
      org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
      org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)
      org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
      org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
      org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
      player.debugg.FriendRegistration.doGet(FriendRegistration.java:39)
      player.debugg.FriendRegistration.doPost(FriendRegistration.java:20)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  java.lang.IllegalArgumentException: object is not an instance of declaring class
  </code></pre>
  
  <p><strong><em>Here are code snipets for entity classes and hbm.xml:</em></strong></p>
  
  <pre class="lang-java prettyprint-override"><code>package player.database.hibernate.entities; 
  public class User
  {
  
      private String userID;
      private String userPassword;
  
  
      public User()
      {
  
      }
  
      public User(String login, String password)
      {
          this.setUserID(login);
          this.setUserPassword(password);
      }
  
      public void setUserID(String id)
      {
          if((id!=null)&amp;&amp;(!id.equals("")))
              this.userID = id;
          else throw new IllegalArgumentException("User ID setter exception: empty or null");
      }
  
      public void setUserPassword(String password)
      {
          if((password!=null)&amp;&amp;(!password.equals("")))
              this.userPassword = password;
          else throw new IllegalArgumentException("User password setter exception: empty or null");
      }
  
      public String getUserID()
      {
          return this.userID;
      }
  
      public String getUserPassword()
      {
          return this.userPassword;
      }
  }
  </code></pre>
  
  <hr>
  
  <pre class="lang-java prettyprint-override"><code>    &lt;?xml version='1.0' encoding='utf-8'?&gt;
      &lt;!DOCTYPE hibernate-mapping PUBLIC
              "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
              "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;
      &lt;hibernate-mapping package="player.database.hibernate.entities"&gt;
        &lt;class name="User" table="USERS"&gt;
          &lt;id name="userID" column="USER_ID"&gt;
            &lt;generator class="assigned"&gt;&lt;/generator&gt;
          &lt;/id&gt;
          &lt;property name="userPassword" column="USER_PASS"/&gt;
        &lt;/class&gt;
  </code></pre>
  
  <h2>        </h2>
  
  <pre class="lang-java prettyprint-override"><code>package player.database.hibernate.entities;
  
  import java.io.Serializable;
  
  public class Subscription implements Serializable
  {
      private String subscriberID;
      private String userID;
  
      public Subscription()
      {
      }
  
      public Subscription(String subscriberID, String userID)
      {
          this.setSubscriberID(subscriberID);
          this.setUserID(userID);
      }
  
      public void setSubscriberID(String subscriberID) {
          if((subscriberID!=null)&amp;&amp;(!subscriberID.equals("")))
              this.subscriberID = subscriberID;
          else throw new IllegalArgumentException("Subscription subscriberID setter error: null or empty string");
      }
  
      public void setUserID(String userID) {
          if((userID!=null)&amp;&amp;(!userID.equals("")))
              this.userID = userID;
          else throw new IllegalArgumentException("Subscription subscriberID setter error: null or empty string");
      }
  
      public String getSubscriberID() {
          return subscriberID;
      }
  
      public String getUserID() {
          return userID;
      }
  
  
      @Override
      public boolean equals(Object obj) {
          if(obj instanceof Subscription){
              Subscription subscription = (Subscription) obj;
  
              if(!subscription.getSubscriberID().equals(subscriberID)){
                  return false;
              }
  
              if(!subscription.getUserID().equals(userID)){
                  return false;
              }
  
              return true;
          }
  
          return false;
      }
  
      @Override
      public int hashCode() {
          return subscriberID.hashCode() + userID.hashCode();
      }
  
  }
  </code></pre>
  
  <hr>
  
  <pre class="lang-java prettyprint-override"><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;
  &lt;hibernate-mapping package="player.database.hibernate.entities"&gt;
    &lt;class name="Subscription" table="SUBSCRIBERS"&gt;
  
        &lt;composite-id&gt;
            &lt;key-many-to-one name="subscriberID" column="SUBSCRIBER_ID" class="User"/&gt;
            &lt;key-many-to-one name="userID" column="USER_ID" class="User"/&gt;
        &lt;/composite-id&gt;
    &lt;/class&gt;
    &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>P.S. I am using a way described here <a href="http://www.java2s.com/Tutorial/Java/0350__Hibernate/ComposedID.htm" rel="nofollow">http://www.java2s.com/Tutorial/Java/0350__Hibernate/ComposedID.htm</a>
  to create a composite key. I assume it works fine, because it outputs theese in console:</p>
  
  <pre class="lang-java prettyprint-override"><code>Hibernate: create table SUBSCRIBERS (SUBSCRIBER_ID varchar(255) not null, USER_ID varchar(255) not null, primary key (SUBSCRIBER_ID, USER_ID))
  Hibernate: create table USERS (USER_ID varchar(255) not null, USER_PASS varchar(255), primary key (USER_ID))
  Hibernate: alter table SUBSCRIBERS add constraint FKnh93vk6v0ivt3a42hbaa7ffk5 foreign key (SUBSCRIBER_ID) references USERS (USER_ID)
  Hibernate: alter table SUBSCRIBERS add constraint FKodh9g832cfmbqtsgtixky9j8j foreign key (USER_ID) references USERS (USER_ID)
  </code></pre>
  
  <p>I would really appreciate any help on this question.</p>