title: Creating Table Using Hibernate and Oracle
body: <p>I would like to create a simple table "account" using hibernate in an oracle database.</p>
  
  <p>Here's my bean class <code>Account</code>:</p>
  
  <pre><code>@Entity
  @Table(name = "account")
  public class Account implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column(nullable = false)
      private String id;
  
      @Column(nullable = false)
      private String iban;
  
      @Column(nullable = false)
      private String name;
  
      @Column(name = "`usage`", nullable = false)
      private String usage;
  
      @Column(nullable = false)
      private String type;
  
      @Column(nullable = false)
      private String ccy;
  
      @Column(nullable = false)
      private String psuStatus;
  
      @Column(nullable = false)
      private String balances;
  
      public Account() {
      }
  
      public Account(String id, String iban, String name, String usage,
              String type, String ccy, String psuStatus, String balances) {
          this.id = id;
          this.iban = iban;
          this.name = name;
          this.usage = usage;
          this.type = type;
          this.ccy = ccy;
          this.psuStatus = psuStatus;
          this.balances = balances;
      }
  
      public String getId() {
          return id;
      }
  
      public void setId(String id) {
          this.id = id;
      }
  
      public String getIban() {
          return iban;
      }
  
      public void setIban(String iban) {
          this.iban = iban;
      }
  
      public String getUsage() {
          return usage;
      }
  
      public void setUsage(String usage) {
          this.usage = usage;
      }
  
      public String getType() {
          return type;
      }
  
      public void setType(String type) {
          this.type = type;
      }
  
      public String getCcy() {
          return ccy;
      }
  
      public void setCcy(String ccy) {
          this.ccy = ccy;
      }
  
      public String getPsuStatus() {
          return psuStatus;
      }
  
      public void setPsuStatus(String psuStatus) {
          this.psuStatus = psuStatus;
      }
  
      public String getBalances() {
          return balances;
      }
  
      public void setBalances(String balances) {
          this.balances = balances;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + ((id == null) ? 0 : id.hashCode());
          return result;
      }
  
      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          Account other = (Account) obj;
          if (id == null) {
              if (other.id != null)
                  return false;
          } else if (!id.equals(other.id))
              return false;
          return true;
      }
  
  }
  </code></pre>
  
  <p>And here's the config file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
  
      &lt;!-- Root Context: defines shared resources visible to all other web components --&gt;
  
      &lt;!-- dataSource (connexion vers la base de données) --&gt;
      &lt;!-- dataSource côté serveur &lt;jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/myDataSource" 
          /&gt; --&gt;
      &lt;!-- datasource locale --&gt;
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&gt;
          &lt;property name="url" value="removed for privacy purposes" /&gt;
          &lt;property name="username" value="removed for privacy purposes" /&gt;
          &lt;property name="password" value="removed for privacy purposes" /&gt;
      &lt;/bean&gt;
  
  
      &lt;!-- support d'annotations JPA --&gt;
      &lt;bean
          class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
      &lt;!-- config de la sessionFactory --&gt;
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"&gt;
  
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="packagesToScan"&gt;
              &lt;list&gt;
                  &lt;value&gt;fr.bnpparibas.dsp2aisp.beans&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;
                      org.hibernate.dialect.MySQL5InnoDBDialect
                  &lt;/prop&gt;
  
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
                  &lt;!-- &lt;prop key="hibernate.generate_statistics"&gt;true&lt;/prop&gt; --&gt;
  
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/prop&gt; &lt;!-- update --&gt;
  
                  &lt;!-- Cache de niv2 --&gt;
                  &lt;prop key="hibernate.cache.use_second_level_cache"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.cache.region.factory_class"&gt;
                      org.hibernate.cache.ehcache.EhCacheRegionFactory
                  &lt;/prop&gt;
                  &lt;prop key="hibernate.cache.use_query_cache"&gt;true&lt;/prop&gt;
  
                  &lt;!-- Configuring Connection Pool --&gt;
                  &lt;prop key="hibernate.c3p0.min_size"&gt;5&lt;/prop&gt;
                  &lt;prop key="hibernate.c3p0.max_size"&gt;20&lt;/prop&gt;
                  &lt;prop key="hibernate.c3p0.timeout"&gt;50&lt;/prop&gt;
                  &lt;prop key="hibernate.c3p0.max_statements"&gt;50&lt;/prop&gt;
                  &lt;prop key="hibernate.c3p0.idle_test_period"&gt;2000&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  
      &lt;!-- support des transactions par annotations @Transactional --&gt;
      &lt;bean id="transactionManager"
          class="org.springframework.orm.hibernate5.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
          &lt;property name="autodetectDataSource" value="true" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  
      &lt;!-- MailService --&gt;
      &lt;bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"&gt;
  
      &lt;!-- SMTP settings --&gt;
          &lt;property name="host" value="smtp.dsl.ovh.net" /&gt;
  &lt;!--        &lt;property name="port" value="21" /&gt; --&gt;
          &lt;!-- &lt;property name="username" value="USER_NAME" /&gt;
          &lt;property name="password" value="PASSWORD" /&gt; --&gt;
  
          &lt;property name="javaMailProperties"&gt;
              &lt;!-- additional properties specific to JavaMail --&gt;
              &lt;props&gt;
                  &lt;prop key="mail.transport.protocol"&gt;smtp&lt;/prop&gt;
                  &lt;prop key="mail.smtp.auth"&gt;false&lt;/prop&gt;
                  &lt;prop key="mail.smtp.starttls.enable"&gt;false&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- surcouche de l'entityManager =&gt; Hibernate --&gt;
      &lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="accountDao" class="fr.bnpparibas.dsp2aisp.daos.AccountDao"&gt;
          &lt;property name="hibernateTemplate" ref="hibernateTemplate" /&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>The problem is, when I run the app, it doesn't create the table <code>account</code> in the database. I used the same example with an MySQL JDBC driver and a wampserver and it worked perfectly. But with an Oracle driver, for some reason, it doesn't work. Anybody knows why? </p>
  
  <p><strong>UPDATE</strong>: I was able to display the error by setting the hbm2ddl logger to true, and here's what I have now:</p>
  
  <pre><code>java.sql.SQLException: Listener refused the connection with the following error:
  ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
  
      at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:673)
      at oracle.jdbc.driver.PhysicalConnection.&lt;init&gt;(PhysicalConnection.java:711)
      at oracle.jdbc.driver.T4CConnection.&lt;init&gt;(T4CConnection.java:385)
      at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:30)
      at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:558)
      at java.sql.DriverManager.getConnection(DriverManager.java:675)
      at java.sql.DriverManager.getConnection(DriverManager.java:219)
      at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153)
      at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144)
      at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:196)
      at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:159)
      at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)
      at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:33)
      at org.hibernate.tool.hbm2ddl.DatabaseExporter.&lt;init&gt;(DatabaseExporter.java:35)
      at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:425)
      at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:361)
      at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:350)
      at org.hibernate.internal.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:469)
      at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:711)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:727)
      at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:511)
      at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:495)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1688)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1626)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:742)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
      at com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextCreated(WebApp.java:2384)
      at com.ibm.ws.webcontainer31.osgi.webapp.WebApp31.notifyServletContextCreated(WebApp31.java:514)
      at com.ibm.ws.webcontainer.webapp.WebApp.initialize(WebApp.java:1012)
      at com.ibm.ws.webcontainer.webapp.WebApp.initialize(WebApp.java:6574)
      at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.startWebApp(DynamicVirtualHost.java:467)
      at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.startWebApplication(DynamicVirtualHost.java:462)
      at com.ibm.ws.webcontainer.osgi.WebContainer.startWebApplication(WebContainer.java:1132)
      at com.ibm.ws.webcontainer.osgi.WebContainer.access$000(WebContainer.java:108)
      at com.ibm.ws.webcontainer.osgi.WebContainer$3.run(WebContainer.java:944)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:522)
      at java.util.concurrent.FutureTask.run(FutureTask.java:277)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1153)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
      at java.lang.Thread.run(Thread.java:785)
  </code></pre>
  
  <p>Does this mean that I need to define the oracle schema? </p>
  
  <p><strong>UPDATE 2</strong>: Now I have this error:</p>
  
  <pre><code>14:29:58.026 [Default Executor-thread-12] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: create table account (id varchar(255) not null, balances varchar(255) not null, ccy varchar(255) not null, iban varchar(255) not null, name varchar(255) not null, psuStatus varchar(255) not null, type varchar(255) not null, `usage` varchar(255) not null, primary key (id)) ENGINE=InnoDB
  14:29:58.027 [Default Executor-thread-12] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - ORA-00911: invalid character
  </code></pre>
  
  <p>But what's strange is that I'm generating the query using Hibernate. I didn't write the query myself. </p>