0

I am using below drivers.

implementation 'com.datastax.astra:astra-spring-boot-starter:0.3.0'
implementation 'com.datastax.oss:java-driver-core:4.14.1'
implementation 'com.datastax.oss:java-driver-query-builder:4.14.1'
implementation 'com.datastax.oss:java-driver-mapper-runtime:4.14.1'
implementation 'org.springframework.boot:spring-boot-starter-data-cassandra'

Here are my entities:

@NamingStrategy(convention = NamingConvention.SNAKE_CASE_INSENSITIVE)
@CqlName("engine_torque_by_last_miles")
@Entity
public class EngineTorqueByLastMiles {

    private UUID id;

    @PartitionKey(1)
    private String vinNumber;
}

Here is my repository:

public interface EngineTorqueByLastMilesRepository extends CassandraRepository<EngineTorqueByLastMiles, String> {
    List<EngineTorqueByLastMiles> findAllByVinNumberAndOrganizationId(String vinNumber, Integer organizationId);
}

The problem I am facing is the soring.data.jpa.cassandra does not map the Entity name or the attributes to snake_case even after using NamingStrategy or CqlName annotations from datastax drivers.

Does datastax provide any driver that supports jpa so that I can write my Entities and their attributes in typical java naming convention and cassandra tables or attributes with snake_case ?

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23
Tuhin Subhra Mandal
  • 473
  • 1
  • 5
  • 15

2 Answers2

1

Datastax provides indeed a way to map objects to your Cassandra Tables and it is called the Cassandra object mapper. The documentation is here https://docs.datastax.com/en/developer/java-driver/4.13/manual/mapper/ BUT YOU DO NOT NEED IT HERE.

Looking at your code it seems you want to use Spring Data Cassandra. This is totally fine. You are simply not using the proper set of annotations. You should the Spring data annotations.

Your bean becomes:

@Table("engine_torque_by_last_miles")
public class EngineTorqueByLastMiles {

    @PrimaryKeyColumn(name = "vin_number", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
    private String vinNumber;

    @Column("id")
    @CassandraType(type = Name.UUID)
    private UUID id;

    // default constructor
    // getters
    // setters
}
  • Given the table name, it seems your partition key should be last_miles but it was not provided in your question.

  • You provided an id but it was not annotated also I assumed it was not part of the primary key. If you have a composite primary key with Partition key and cluster columns you need to create an ANOTHER internal bean for the PK and annotate it with @PrimaryKey (sample)

You can find a full-fledge working application here with multiple entities https://github.com/datastaxdevs/workshop-betterreads/tree/master/better-reads-webapp

If you edit or complete your question we could propose the exact beans needed.

clunven
  • 1,360
  • 6
  • 13
0

Try setting the property:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

since Hibernate 5 it's the default and you would get your snake-cased naming.

For more reference see the documentation here

Valerij Dobler
  • 1,848
  • 15
  • 25
  • I am not using any orm. However I have found solution for this. ---------- . we can add @Table(value = "engine_torque_by_last_miles") and @Column("organization_id") annotations from org.springframework.data.cassandra.core.mapping – Tuhin Subhra Mandal May 27 '22 at 09:58
  • Yes, this is an obviously better answer. Why not add it as a separate here and accept it? – Valerij Dobler May 27 '22 at 10:13