How to make UUID auto generated in springbok Cassandra app

481 views Asked by At

I am using

Java: 19

Springboot: 3.0

Dependency

<dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-cassandra</artifactId>
 </dependency>

Entity class

@Table
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Player {
  @PrimaryKey
  private UUID id;

Would like to know if there is anything like

@Id
@GeneratedValue(strategy = GenerationType.AUTO)

that we can use with it.

2

There are 2 answers

0
Prafulla Kumar Sahu On BEST ANSWER
@CassandraType(type = DataType.Name.TIMEUUID)

is not auto generating the id.

One way is assigning default during declaration value like

 private UUID id = UUID.randomUUID();

but still, I consider this as a workaround.

6
Raushan Kumar On

It can be achieved either using @CassandraType annotation in combination with the com.datastax.driver.core.DataType.Name.UUID

@Table
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Player {
  @PrimaryKey
  @CassandraType(type = DataType.Name.UUID)
  private UUID id;

or @CassandraType annotation with the com.datastax.driver.core.DataType.Name.TIMEUUID

@Table
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Player {
  @PrimaryKey
  @CassandraType(type = DataType.Name.TIMEUUID)
  private UUID id;