spring-projects / spring-data-cassandra

Provides support to increase developer productivity in Java when using Apache Cassandra. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-cassandra/
Apache License 2.0
373 stars 307 forks source link

Revise `@PrimaryKey` to be an alias for `@Column` #1476

Closed spartanhooah closed 5 months ago

spartanhooah commented 5 months ago

I have a table that has names like resource_key, and the entity POJO is defined like this

@Table("resource_ref")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Uuid implements Serializable {
    private static final long serialVersionUID = 1L;

    @PrimaryKey
    @Column("resource_key")
    private String resourceKey;

    @Column("resource_type")
    private String resourceType;

    @Column("resource_uuid")
    private UUID uuid;

    private boolean uuidAlreadyExists;
}

This causes Cassandra to complain that it can't find the column resourcekey. I have gotten around this by defining a post-processor:

public class CassandraPostProcessor implements BeanPostProcessor {
    public Object postProcessAfterInitialization(@NotNull Object bean, @NotNull String beanName) throws BeansException {
        if (bean instanceof CassandraMappingContext context) {
            context.setNamingStrategy(NamingStrategy.SNAKE_CASE);

            return context;
        }

        return bean;
    }
}

Is there something else I should be doing to make Spring respect the @Column value attribute?

mp911de commented 5 months ago

@PrimaryKey(…) accepts the column name and this annotation is considered only while the @Column annotation is ignored for identifiers. However, it would be a neat idea to accept @Column(…) if both are defined.