1

I have my primary key for a table as Composite key comprised of 3 fields: id, username and email. I want to be able to query a user by either just the username or by just the email without knowing about other keys.

I tried declaring a method in repository but it gives the error No property userKeyUsername found for type User!

Here is my repository interface:

@Repository public interface UserRepo extends CassandraRepository<User, UserKey>{ User findByUserKeyEmail(String email); User findByUserKeyUsername(String username); } 

I have the following class as a key for my User table.

public class UserKey { @PrimaryKeyColumn(name = "id", ordinal = 0, type = PrimaryKeyType.PARTITIONED) private UUID id; @PrimaryKeyColumn(name = "username", ordinal = 0, type = PrimaryKeyType.PARTITIONED) private String username; @PrimaryKeyColumn(name = "email", ordinal = 0, type = PrimaryKeyType.PARTITIONED) private String email; //equals and hashcode } 

And this class as my User model:

public class User { @PrimaryKey private UserKey key; @Column("first_name") @CassandraType(type = Name.TEXT) private String firstName; @Column("last_name") @CassandraType(type = Name.TEXT) private String lastName; @Column("password") @CassandraType(type = Name.TEXT) private String password; @Column("roles") @CassandraType(type = Name.LIST, typeArguments = Name.TEXT) private List<Role> roles = new ArrayList<>(); 

}

1 Answer 1

1

To retrieve data, all three columns defined in the partition key have to be supplied. You can use secondary indexes as a workaround but it's not recommended. I'd recommend identifying first all queries you would like to perform and then redesigning your data model based on them.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.