title: Migrating from Mysql to Cassandra Spring boot
body: <p>I am totally new to Cassandra, this is the very first time, never used it before, so far we have been using Spring Boot and MySql as our DB, but now we are planning to migrate our database to Cassandra with the minimum code or no change.
  here is a sample demo of code that we have been using.</p>
  <p>config class</p>
  <pre><code>package com.example.demo.config;
  import com.zaxxer.hikari.HikariDataSource;
  import lombok.Generated;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Primary;
  import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  import org.springframework.orm.jpa.JpaTransactionManager;
  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  import org.springframework.orm.jpa.vendor.Database;
  import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  import javax.sql.DataSource;
  import java.util.HashMap;
  import static org.hibernate.cfg.AvailableSettings.*;
  @Configuration
  @EnableTransactionManagement
  @EnableJpaAuditing
  @EnableJpaRepositories(entityManagerFactoryRef = &quot;orclEntityManagerFactory&quot;, transactionManagerRef = &quot;orclTransactionManager&quot;, basePackages = {&quot;com.example.demo.repository&quot;})
  @Generated
  public class DataSourceConfig {
      @Value(&quot;${spring.datasource.driver-class-name}&quot;)
      private String orclDbDriver;
      @Value(&quot;${spring.datasource.url}&quot;)
      private String orclDbConnUrl;
      @Value(&quot;${spring.datasource.username}&quot;)
      private String orclDbUsername;
      @Value(&quot;${spring.datasource.password}&quot;)
      private String orclDbPassword;
      @Value(&quot;${spring.datasource.poolName}&quot;)
      private String dataSourcePoolName;
      @Value(&quot;${spring.jpa.properties.hibernate.dialect}&quot;)
      private String orclHibernateDialect;
      @Value(&quot;${spring.jpa.hibernate.ddl-auto}&quot;)
      private String hibernateDDL;
      @Value(&quot;${spring.jpa.show-sql}&quot;)
      private boolean showSql;
      @Value(&quot;${spring.datasource.packagesToScan}&quot;)
      private String[] packagesToScan;
      public DataSourceConfig() {
      }
      @Bean
      @Primary
      public DataSource orclDataSource() {
          HikariDataSource dataSource = new HikariDataSource();
          dataSource.setDriverClassName(this.orclDbDriver);
          dataSource.setJdbcUrl(this.orclDbConnUrl);
          dataSource.setUsername(this.orclDbUsername);
          dataSource.setPassword(this.orclDbPassword);
          dataSource.setPoolName(this.dataSourcePoolName);
          return dataSource;
      }
      @Bean
      @Primary
      public LocalContainerEntityManagerFactoryBean orclEntityManagerFactory() {
          LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
          factory.setDataSource(this.orclDataSource());
          factory.setPackagesToScan(this.packagesToScan);
          HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
          jpaVendorAdapter.setDatabase(Database.MYSQL);
          jpaVendorAdapter.setGenerateDdl(Boolean.TRUE);
          jpaVendorAdapter.setShowSql(showSql);
          jpaVendorAdapter.setDatabasePlatform(orclHibernateDialect);
          factory.setJpaVendorAdapter(jpaVendorAdapter);
          HashMap&lt;String, Object&gt; properties = new HashMap();
          properties.put(HBM2DDL_AUTO, this.hibernateDDL);
          properties.put(DIALECT, this.orclHibernateDialect);
          properties.put(STATEMENT_BATCH_SIZE, &quot;500&quot;);
          properties.put(ORDER_UPDATES, &quot;true&quot;);
          properties.put(ORDER_INSERTS, &quot;true&quot;);
          properties.put(GENERATE_STATISTICS, &quot;true&quot;);
          factory.setJpaPropertyMap(properties);
          return factory;
      }
      @Bean
      @Primary
      public JpaTransactionManager orclTransactionManager() {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(this.orclEntityManagerFactory().getObject());
          return transactionManager;
      }
  }
  </code></pre>
  <p>controller class</p>
  <pre><code>import com.example.demo.entity.Child;
  import com.example.demo.repository.ChildRepository;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.RestController;
  
  import java.sql.Timestamp;
  import java.util.List;
  
  @RestController
  public class HelloController {
      @Autowired
      private ChildRepository childRepository;
  
      @GetMapping(&quot;put&quot;)
      public Child getHello() {
          Child child = new Child();
          child.setName(&quot;shrikant&quot;);
          child.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
          return childRepository.save(child);
      }
  
      @GetMapping(&quot;get&quot;)
      public List&lt;Child&gt; hello() {
          return childRepository.findAll();
      }
  }
  </code></pre>
  <p>repository</p>
  <pre><code>package com.example.demo.repository;
  import com.example.demo.entity.Child;
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.stereotype.Repository;
  @Repository
  public interface ChildRepository extends JpaRepository&lt;Child, Long&gt; {
  }
  
   
  </code></pre>
  <p>application.dev</p>
  <pre><code>spring.datasource.url=jdbc:mysql://localhost:3306/student_db?jdbcCompliantTruncation=false&amp;sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'&amp;useSSL=false&amp;useServerPrepStmts=false&amp;rewriteBatchedStatements=true&amp;useUnicode=true&amp;characterEncoding=utf8
  spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
  spring.datasource.username=root
  spring.datasource.password=root
  spring.datasource.poolName=springHikariCp
  spring.jpa.generate-ddl=true
  spring.jpa.database.schema=student_db
  spring.jpa.hibernate.ddl-auto=update
  #spring.jpa.hibernate.ddl-auto=none
  spring.jpa.show-sql=true
  spring.datasource.packagesToScan=com.example.demo.entity
  spring.profiles.active=dev
  </code></pre>
  <p>build.gradle</p>
  <pre><code>plugins {
      id 'org.springframework.boot' version '2.2.6.RELEASE'
      id 'io.spring.dependency-management' version '1.0.9.RELEASE'
      id 'java'
  }
  group = 'com.example'
  version = '0.0.1-SNAPSHOT'
  sourceCompatibility = '8'
  configurations {
      compileOnly {
          extendsFrom annotationProcessor
      }
  }
  repositories {
      mavenCentral()
  }
  dependencies {
      implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
      implementation 'org.springframework.boot:spring-boot-starter-web'
      compileOnly 'org.projectlombok:lombok'
      compile 'mysql:mysql-connector-java'  
      annotationProcessor 'org.projectlombok:lombok' 
      testImplementation('org.springframework.boot:spring-boot-starter-test') {
          exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
      }
  }
  test {
      useJUnitPlatform()
  }
  </code></pre>
  <p>I have installed Cassandra with the help of google and able to login using <code>cqlsh</code>.</p>
  <p>created keyspace</p>
  <pre><code>CREATE KEYSPACE IF NOT EXISTS mykeyspace WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};
  </code></pre>
  <p>added Cassandra dependency in Gradle</p>
  <pre><code>compile 'org.springframework.boot:spring-boot-starter-data-cassandra'
  </code></pre>
  <p>but what should be the configuration properties in the application.properties, I couldn't find any of the examples on Google creating a custom data source, all are using Spring Boot auto-config, but I can't do that, because that will be a major code change.</p>