Search code examples
cassandrakundera

Kundera persistence entity definition with partition key and cluster key


I am trying to use Kundera for connecting to Cassandra. In my case I have an Entity X with Partition Key {A,B} and cluster key {C}.

We have multiple values of C for each A and B combination(primary key).

So in such case how should be define the entity?

@Embeddable
public class PrimayKey implements Serializable{

 @Column(name = "A")
     private String a;

     @Column(name = "B")
     private String b;
}




@Entity
@Table(name = "X")
public class X{


    @EmbeddedId
    private PrimayKey key;

    @Column(name = "C")
    private String c;

   @Column(name = "D")
    private String d;
}

Here if I go for a find by Primary Key its not working as we have multiple values of C and D for each A and B combination. In such case how should we define entity?


Solution

  • You can create an embeddable for partition key and then use it as a field in primary key. Kundera will automatically read the entities and consider the fields other than partition keys as clustering keys.

    Cassandra equivalent Primary key : ((A, B), C)

    Entity :

    @Embeddable
    public class PartitionKey {
    
     @Column(name = "A")
     private String a;
    
     @Column(name = "B")
     private String b;
    
    }
    
    @Embeddable
    public class PrimayKey {
    
     private PartitionKey key;
    
     @Column(name = "C")
     private String c;
    
    }
    

    Please refer this sample entity for more info.