Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying Tables with Composite Primary Keys using Spring-data-cassandra

I'm getting the following exception when using the findOne() or findAll() method of CassandraReporitory:

Caused by: java.lang.IllegalStateException: property does not have a single column mapping
    at org.springframework.data.cassandra.mapping.BasicCassandraPersistentProperty.getColumnName(BasicCassandraPersistentProperty.java:134)
    at org.springframework.data.cassandra.convert.BasicCassandraRowValueProvider.getPropertyValue(BasicCassandraRowValueProvider.java:64)
    at org.springframework.data.cassandra.convert.BasicCassandraRowValueProvider.getPropertyValue(BasicCassandraRowValueProvider.java:35)
    at org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:78)
    at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:71)
    at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:83)
    at org.springframework.data.cassandra.convert.MappingCassandraConverter.readEntityFromRow(MappingCassandraConverter.java:133)
    at org.springframework.data.cassandra.convert.MappingCassandraConverter.readRow(MappingCassandraConverter.java:115)
    at org.springframework.data.cassandra.convert.MappingCassandraConverter.read(MappingCassandraConverter.java:200)
    at org.springframework.data.cassandra.repository.query.AbstractCassandraQuery.getSingleEntity(AbstractCassandraQuery.java:176)
    at org.springframework.data.cassandra.repository.query.AbstractCassandraQuery.execute(AbstractCassandraQuery.java:133)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:454)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:432)

My class definitions are as follows:

@PrimaryKeyClass
public class ItemAndLocationKey implements Serializable {

    private static final long serialVersionUID = 1L;

    @PrimaryKeyColumn(name = "itemId", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
    @CassandraType(type = Name.UUID)
    private UUID itemId;
    @PrimaryKeyColumn(name = "locationId", ordinal = 1, type = PrimaryKeyType.CLUSTERED)
    @CassandraType(type = Name.UUID)
    private UUID locationId;

    public ItemAndLocationKey(UUID itemId, UUID locationId) {
        this.itemId = itemId;
        this.locationId = locationId;
    }

    public UUID getItemId() {
        return itemId;
    }

    public void setItemId(UUID itemId) {
        this.itemId = itemId;
    }

    public UUID getLocationId() {
        return locationId;
    }

    public void setLocationId(UUID locationId) {
        this.locationId = locationId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ItemAndLocationKey that = (ItemAndLocationKey) o;

        if (!itemId.equals(that.itemId)) return false;
        return locationId.equals(that.locationId);
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + itemId.hashCode();
        result = prime * result + locationId.hashCode();
        return result;
    }
}

@Table(value = ItemAndLocation.tableName)
public class ItemAndLocation {

    @org.springframework.data.annotation.Transient
    public static final String tableName = "ItemAndLocation";

    @PrimaryKey
    private ItemAndLocationKey itemAndLocationKey;

    public ItemAndLocation(ItemAndLocationKey itemAndLocationKey) {
        this.itemAndLocationKey = itemAndLocationKey;
    }

    public ItemAndLocationKey getItemAndLocationKey() {
        return itemAndLocationKey;
    }

    public void setItemAndLocationKey(ItemAndLocationKey itemAndLocationKey) {
        this.itemAndLocationKey = itemAndLocationKey;
    }
}

public interface ItemAndLocationRepository extends TypedIdCassandraRepository<ItemAndLocation, ItemAndLocationKey> {

}

Essentially what is happening here is that after reading a row from Cassandra during the invocation of findOne() or findAll(), spring data cassandra attempts to use ClassGeneratingEntityInstantiator to instantiate the ItemAndLocation object that does not know how to populate the composite primary key object, ItemAndLocationKey. I couldn't figure out how to supply a custom object instantiator. There are no examples or documentation for the current release.

Any help will be much appreciated.

like image 927
Mukil Kesavan Avatar asked Dec 18 '15 20:12

Mukil Kesavan


2 Answers

I had the same problem, the problem was in the class that annotated with @Table.

In your scenario, you have to remove the overloaded constructor in the following class:

@Table(value = ItemAndLocation.tableName)
public class ItemAndLocation {

    @org.springframework.data.annotation.Transient
    public static final String tableName = "ItemAndLocation";

    @PrimaryKey
    private ItemAndLocationKey itemAndLocationKey;

    // \\\\\\\\\\\\\\\\\\\\ REMOVE THIS CONSTRUCTOR //////////////////
    // public ItemAndLocation(ItemAndLocationKey itemAndLocationKey) {
    //    this.itemAndLocationKey = itemAndLocationKey;
    // }
    // ////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


    public ItemAndLocationKey getItemAndLocationKey() {
        return itemAndLocationKey;
    }

    public void setItemAndLocationKey(ItemAndLocationKey itemAndLocationKey) {
        this.itemAndLocationKey = itemAndLocationKey;
   }
}

I hope this helps :)

like image 144
Mohammad Hassany Avatar answered Nov 06 '22 17:11

Mohammad Hassany


I had the same problem both in the Main entity class as well as the PrimaryKey-class. What fixed it for me was to supply default constructors that take no parameters and initialize all fields to some default value, meaning null for Objects, false for booleans and 0 for all other primitive types.

In your case that would mean to add:

private ItemAndLocation() {
    this.itemAndLocationKey = null;
}

and

private ItemAndLocationKey() {
    this.itemId = null;
    this.locationId = null;
}

The constructors do not need to be public, the fields can be final, the framework will set the values via reflection.

like image 43
luk2302 Avatar answered Nov 06 '22 17:11

luk2302