Home > Blockchain >  Cassandra Java Enity to snake case table name mapping with jpa issue
Cassandra Java Enity to snake case table name mapping with jpa issue

Time:05-29

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 ?

CodePudding user response:

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

CodePudding user response:

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.

  • Related