Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

mysub99 avatar image
mysub99 asked smadhavan edited

How do I specify the table name similar to @Table in Spring?

I wan to upgrade from spring-data-cassandra to java datastax driver 4.12.0

Below is my spring-data-cassandra version for domain class

@Table('students_details')
class Students{

    @PrimaryKeyColumn(name='s_id',type = PrimaryKeyType.PARTITIONED)
    Integer id

    @PrimaryKeyColumn(name='stident_name',type= PrimaryKeyType.CLUSTERED)
    String studentName
}

now i want to change above class according to java datastax annotation, below is my datastax mapper version

@Entity
class Students{

    @PartitionKey
    @CqlName('s_id')
    Integer id

    @ClusteringColumn
    @CqlName('stident_name')
    String studentName

}

Now the problem is how to specify table name similar to @Table annotation in spring casandra.

Thanks

java driver
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Erick Ramirez avatar image
Erick Ramirez answered

The @Entity is a Java class that gets mapped to the Cassandra table. The class name has to match the name of the CQL table.

In the class definition you provided, it will be mapped to a CQL table called students:

@Entity
public class Product {
    ...
}

Cheers!

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

smadhavan avatar image
smadhavan answered smadhavan edited

@mysub99, with DataStax Java Driver conversion, what you need is a Naming Strategy here.

In your example, let's assume your actual CQL table name is students_details. In that case, your class will look like this:

@Entity
@CqlName("students_details")
class Students {
...
}

By doing so, Students class name will be mapped to the CQL table name of students_details. There are other varieties of naming strategies that can be applied as well. Refer to the documentation here for additional details.

Additional tip: The same technique of providing a CqlName to provide an user-provided name to the fields or methods as well will work.

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.