Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Cassandra store a List with custom object's

I like to store a object like:

@Table(value = "my_table")
public class MyTableDto {
  @PrimaryKeyColumn(name = "uid", type = PrimaryKeyType.PARTITIONED)
  @CassandraType(type = DataType.Name.UUID)
  private UUID uid;

  @Column(value = "child_ids")
  private List<ChildIdDto> childIds;
}

Then I get the exception:

Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Only primitive types are allowed inside Collections for property [childIds] of type ['interface java.util.List'] in entity [de.myapplication.repository.dto.MyTableDto]

I do understand the exception, but is there another way to persist custom objects?

EDIT:

  • When I comment out this attribute, everything works
like image 794
Daniel Eisenreich Avatar asked Nov 02 '25 18:11

Daniel Eisenreich


1 Answers

! Never say never, I got the solution.

To give a good example, I will list all according classes.

ParentClass.java

@Table(value = "my_table") //OPT
public class MyTableDto {
  @PrimaryKeyColumn(name = "uid", type = PrimaryKeyType.PARTITIONED) 
  @CassandraType(type = DataType.Name.UUID)
  private UUID uid;

  @Column(value = "child_ids") //OPT
  private List<ChildDto> childIds;
}

ChildDto.java

@UserDefinedType // THE SOLUTION
public class ChildDto {
  @Column(value = "child") //OPT
  @CassandraType(type = DataType.Name.TEXT) //OPT
  private String groupId;

  @Column(value = "description") //OPT
  @CassandraType(type = Name.TEXT) //OPT
  private String description;
}

The @UserDefinedType is the solution.
For more information see here.

NOTE: Each annotation with "OPT" is NOT required

like image 148
Daniel Eisenreich Avatar answered Nov 04 '25 09:11

Daniel Eisenreich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!