title: Prevent @Entity from re creating a database table - Spring Boot
body: <p>I'm kinda new to spring boot data jpa, as far as I know @Entity is used to represent a database table in the application, for this project im using <code>spring-boot 2.2.5.RELEASE</code> and <code>H2</code> as in memory database.</p>
  <p>So far I've got this.</p>
  <p>inside of resources/data.sql</p>
  <pre><code>CREATE TABLE CURRENCY (
    id INT AUTO_INCREMENT  PRIMARY KEY,
    name VARCHAR(250) NOT NULL,
    code VARCHAR(250) NOT NULL
  );
  
  CREATE TABLE EXCHANGE_CURRENCY (
    id INT AUTO_INCREMENT  PRIMARY KEY,
    IdFx1 INT NOT NULL,
    IdFx2 INT NOT NULL,
    equivalent DECIMAL NOT NULL,
    FOREIGN KEY (IdFx1) REFERENCES CURRENCY(id),
    FOREIGN KEY (IdFx2) REFERENCES CURRENCY(id)
  );
  </code></pre>
  <p>My entity class</p>
  <pre><code>import javax.persistence.*;
  
  @Entity
  @Table(name = &quot;CURRENCY&quot;)
  public class Currency {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      private String name;
      private String code;
  }
  </code></pre>
  <p>Repository</p>
  <pre><code>import org.springframework.data.jpa.repository.Query;
  import org.springframework.data.repository.CrudRepository;
  import org.springframework.data.repository.query.Param;
  import org.springframework.stereotype.Repository;
  
  import java.util.List;
  
  @Repository
  public interface CurrencyRepository extends CrudRepository&lt;Currency, Long&gt; {
  
      @Query(&quot;SELECT c FROM CURRENCY WHERE c.code LIKE %:code%&quot;)
      List&lt;Currency&gt; findCurrencyByCode(@Param(&quot;code&quot;) String code);
  
  }
  </code></pre>
  <p>And service</p>
  <pre><code>import com.currency.canonical.models.Currency;
  import com.currency.canonical.request.ExchangeValueRequest;
  import com.currency.canonical.response.ExchangeValueResponse;
  import com.currency.dao.CurrencyService;
  import com.currency.dao.repository.CurrencyRepository;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  
  import java.util.List;
  
  @Component
  public class CurrencyConversionServiceImpl implements CurrencyConversionService {
  
      Logger logger = LoggerFactory.getLogger(CurrencyConversionServiceImpl.class);
  
      @Autowired
      private CurrencyRepository currencyRepository;
  
      @Override
      public ExchangeValueResponse performCurrencyConversion(ExchangeValueRequest request) {
          final long initialTime = System.currentTimeMillis();
  
          ExchangeValueResponse objExchangeValueResponse = new ExchangeValueResponse();
  
          try {
              List&lt;Currency&gt; currencyList = currencyRepository.findCurrencyByCode(request.getMonedaOrigen());
              currencyList.forEach(System.out::println);
  
          } catch (Exception e) {
  
          }
  
          return objExchangeValueResponse;
      }
  }
  </code></pre>
  <p>When executing the app I got this error</p>
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #2 of URL [file:/C:/Users/Usuario/Documents/IdeaProjects/currency-converter/currency-converter-resource/target/classes/data.sql]: CREATE TABLE CURRENCY ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(250) NOT NULL, code VARCHAR(250) NOT NULL ); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Tabla &quot;CURRENCY&quot; ya existe
  Table &quot;CURRENCY&quot; already exists; SQL statement:
  CREATE TABLE CURRENCY ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(250) NOT NULL, code VARCHAR(250) NOT NULL ) [42101-200]
  </code></pre>
  <p>Why is the @Entity trying to re create a table that is supposed to only represent, and is there a way to disable this?</p>