title: Error creating bean with name 'sessionFactory' defined in ServletContext resource
body: <p>So I'm trying to implement Spring Framework and Hibernate to my web project.</p>
  
  <p>I got hibernate to work at one point (register user and add it to database), but after some time nothing is working.</p>
  
  <pre><code>INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
  INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Sat Jul 20 22:45:19 EET 2013]; root of context hierarchy
  INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/root-context.xml]
  INFO : org.springframework.beans.factory.config.PropertyPlaceholderConfigurer - Loading properties file from ServletContext resource [/WEB-INF/jdbc.properties]
  INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7b4dab69: defining beans [messageSource,propertyConfigurer,dataSource,sessionFactory,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,transactionManager]; root of factory hierarchy
  WARN : org.hibernate.internal.util.xml.DTDEntityResolver - HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
  INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7b4dab69: defining beans [messageSource,propertyConfigurer,dataSource,sessionFactory,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,transactionManager]; root of factory hierarchy
  ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1482)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:608)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4797)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5291)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
      at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
      at java.util.concurrent.FutureTask.run(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  Caused by: java.lang.NullPointerException
      at org.hibernate.mapping.Constraint$ColumnComparator.compare(Constraint.java:134)
      at org.hibernate.mapping.Constraint$ColumnComparator.compare(Constraint.java:130)
      at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
      at java.util.TimSort.sort(Unknown Source)
      at java.util.TimSort.sort(Unknown Source)
      at java.util.Arrays.sort(Unknown Source)
      at org.hibernate.mapping.Constraint.generateName(Constraint.java:82)
      at org.hibernate.cfg.Configuration.buildUniqueKeyFromColumnNames(Configuration.java:1572)
      at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1395)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1756)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
      at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:247)
      at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:373)
      at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:358)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
      ... 22 more
  </code></pre>
  
  <p>and <code>Caused by: java.lang.NullPointerException</code></p>
  
  <p>root-context.xml</p>
  
  <pre><code>&lt;!-- Root Context: defines shared resources visible to all other web components --&gt;
  &lt;bean id="messageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
      &lt;property name="basename" value="classpath:messages" /&gt;
      &lt;property name="defaultEncoding" value="UTF-8" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
      &lt;property name="location" value="/WEB-INF/jdbc.properties" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dataSource"
      class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
      &lt;property name="driverClassName" value="${jdbc.driverClassName}" /&gt;
      &lt;property name="url"             value="${jdbc.databaseurl}" /&gt;
      &lt;property name="username"        value="${jdbc.username}" /&gt;
      &lt;property name="password"        value="${jdbc.password}" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource"         ref="dataSource" /&gt;
      &lt;property name="packagesToScan"     value="com.something.*" /&gt; 
      &lt;property name="configLocation"     value="classpath:hibernate.cfg.xml" /&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${jdbc.dialect}&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven /&gt;
  
  &lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>hibernate.cfg.xml</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt; 
  </code></pre>
  
  <p>
  </p>
  
  <pre><code>&lt;session-factory&gt;
  
      &lt;!-- Database connection settings --&gt;
      &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
      &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost/imdb&lt;/property&gt;
      &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
      &lt;property name="hibernate.connection.password"&gt;&lt;/property&gt;
  
      &lt;!-- JDBC connection pool (use the built-in) --&gt;
      &lt;property name="hibernate.connection.pool_size"&gt;1&lt;/property&gt;
  
      &lt;!-- SQL dialect --&gt;
      &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/property&gt;
  
      &lt;!-- Enable Hibernate's automatic session context management --&gt;
      &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
  
      &lt;!-- Echo all executed SQL to stdout for debugging --&gt;
      &lt;property name="show_sql"&gt;true&lt;/property&gt;
      &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;
  
      &lt;mapping class="springers.Model.Bean.UserAccount" /&gt;
      &lt;mapping class="springers.Model.Bean.Movie" /&gt;
      &lt;mapping class="springers.Model.Bean.TvShow" /&gt;
      &lt;mapping class="springers.Model.Bean.Connector_User_TvShow" /&gt;
  &lt;/session-factory&gt;
  </code></pre>
  
  <p></p>
  
  <p>data object example:</p>
  
  <pre><code>@Entity
  @Table(name = "USERS")
  
  public class UserAccount {
  
      private int    user_pk;
      private String username;
      private String password;
      private String e_mail;
      private String country;
  
      @Id
      @GeneratedValue
      @Column(name = "USER_PK")
      public int    getUser_pk()  { return user_pk;  }
  
      @Column(name = "USERNAME", nullable = false, length = 12, unique = true)
      public String getUsername() { return username; }
  
      @Column(name = "PASSWORD", nullable = false, length = 12)
      public String getPassword() { return password; }
  
      @Column(name = "E_MAIL", nullable = false, length = 50, unique = true)
      public String getE_mail()   { return e_mail;   }
  
      @Column(name = "COUNTRY", nullable = false, length = 25)
      public String getCountry()  { return country;  }
  
      public void setUser_pk (int    user_pk)  { this.user_pk = user_pk; }
      public void setUsername(String username) { this.username = username; }
      public void setPassword(String password) { this.password = password; }
      public void setCountry (String country)  { this.country = country;   }
      public void setE_mail  (String e_mail)   { this.e_mail = e_mail;     }
  </code></pre>
  
  <p>user tvshow connector bean:</p>
  
  <pre><code>@Entity
  @Table(name = "USER_TVSHOW_CONNECTOR",
  uniqueConstraints = @UniqueConstraint(columnNames = {"user_fk", "tvShow_fk"}))
  
  public class Connector_User_TvShow {
  
  private int         utc_pk;
  private boolean     toWatch  = false;
  private boolean     favorite = false;
  private boolean     reminder = false;
  private UserAccount user_fk;
  private TvShow      tvShow_fk;
  
  @Id
  @GeneratedValue
  @Column(name = "UTC_PK")
  public int         getUtc_pk()    { return utc_pk;    }
  
  @Column(name = "TO_WATCH", nullable = false)
  public boolean     isToWatch()    { return toWatch;   }
  
  @Column(name = "FAVOURITE", nullable = false)
  public boolean     isFavorite()   { return favorite;  }
  
  @Column(name = "REMINDER", nullable = false)
  public boolean     isReminder()   { return reminder;  }
  
  @OneToOne(cascade = CascadeType.ALL)
  public UserAccount getUser_fk()   { return user_fk;   }
  
  @OneToOne(cascade = CascadeType.ALL)
  public TvShow      getTvShow_fk() { return tvShow_fk; }
  
  public void setUtc_pk    (int         utc_pk)    { this.utc_pk    = utc_pk;    }
  public void setToWatch   (boolean     toWatch)   { this.toWatch   = toWatch;   }
  public void setFavorite  (boolean     favorite)  { this.favorite  = favorite;  }
  public void setReminder  (boolean     reminder)  { this.reminder  = reminder;  }
  public void setUser_fk   (UserAccount user_fk)   { this.user_fk   = user_fk;   }
  public void setTvShow_fk (TvShow      tvShow_fk) { this.tvShow_fk = tvShow_fk; }
  </code></pre>