Skip to content

Querying document attributes #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
danielbathke opened this issue May 21, 2018 · 1 comment
Open

Querying document attributes #165

danielbathke opened this issue May 21, 2018 · 1 comment

Comments

@danielbathke
Copy link

danielbathke commented May 21, 2018

Looking to the demo source code, but changing it a little bit to meet my needs, I've added a document type attribute, to be able to store an object inside an attribute. That was ok.

But then, if I need to search by a field inside this object/document, it fails.

In Spring it's called Property expressions (https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions).

Just as a test, here follows the source:

@DynamoDBTable(tableName = "Device")
public class Device {
    @Id
    private DeviceKey key;

    @DynamoDBAttribute
    private String name;

    @DynamoDBAttribute
    private Date lastChange;

    @DynamoDBAttribute
    private DeviceConfiguration deviceConfiguration;

    private Device(DeviceKey key, String name, Date lastChange) {
        this.key = key;
        this.name = name;
        this.lastChange = lastChange;
    }

    public Device(Long vendorId, String productId, String name, Date lastChange) {
        this(new DeviceKey(vendorId, productId), name, lastChange);
    }

    public Device() {}


    @DynamoDBHashKey(attributeName = "VendorId")
    public Long getVendorId() {
        return (key != null) ? key.getVendorId() : null;
    }

    public void setVendorId(Long vendorId) {
        if (key == null) {
            key = new DeviceKey();
        }
        key.setVendorId(vendorId);
    }

    @DynamoDBRangeKey(attributeName = "ProductId")
    public String getProductId() {
        return (key != null) ? key.getProduct() : null;
    }

    public void setProductId(String product) {
        if (key == null) {
            key = new DeviceKey();
        }
        key.setProduct(product);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getLastChange() {
        return lastChange;
    }

    public void setLastChange(Date lastChange) {
        this.lastChange = lastChange;
    }

    public DeviceConfiguration getDeviceConfiguration() {
		return deviceConfiguration;
	}

	public void setDeviceConfiguration(DeviceConfiguration configuration) {
		this.deviceConfiguration = configuration;
	}

	@Override
	public String toString() {
		return "Device [key=" + key + ", name=" + name + ", lastChange=" + lastChange + ", deviceConfiguration="
				+ deviceConfiguration + "]";
	}

}

And the DeviceConfiguration class:

@DynamoDBDocument
public class DeviceConfiguration {
	
	@DynamoDBAttribute
	private String host;
	
	@DynamoDBAttribute
	private String port;

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

}

And now the repository:

@EnableScan
public interface DeviceRepository extends CrudRepository<Device, DeviceKey> {

    List<Device> findAll();
    
    List<Device> findByDeviceConfiguration_Host( String host);
}

When I try to nosqlRepository.findByDeviceConfiguration_Host("localhost"), it gives me an exception:

Caused by: com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: Device[host]; no mapping for attribute by name
	at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperTableModel.field(DynamoDBMapperTableModel.java:94) ~[aws-java-sdk-dynamodb-1.11.232.jar:na]
	at org.socialsignin.spring.data.dynamodb.repository.query.AbstractDynamoDBQueryCriteria.getPropertyAttributeValue(AbstractDynamoDBQueryCriteria.java:494) ~[spring-data-dynamodb-4.5.4.jar:na]
	at org.socialsignin.spring.data.dynamodb.repository.query.AbstractDynamoDBQueryCriteria.createSingleValueCondition(AbstractDynamoDBQueryCriteria.java:640) ~[spring-data-dynamodb-4.5.4.jar:na]
	at org.socialsignin.spring.data.dynamodb.repository.query.DynamoDBEntityWithHashAndRangeKeyCriteria.withPropertyEquals(DynamoDBEntityWithHashAndRangeKeyCriteria.java:389) ~[spring-data-dynamodb-4.5.4.jar:na]
	at org.socialsignin.spring.data.dynamodb.repository.query.AbstractDynamoDBQueryCreator.addCriteria(AbstractDynamoDBQueryCreator.java:131) ~[spring-data-dynamodb-4.5.4.jar:na]
	at org.socialsignin.spring.data.dynamodb.repository.query.AbstractDynamoDBQueryCreator.create(AbstractDynamoDBQueryCreator.java:68) ~[spring-data-dynamodb-4.5.4.jar:na]
	at org.socialsignin.spring.data.dynamodb.repository.query.AbstractDynamoDBQueryCreator.create(AbstractDynamoDBQueryCreator.java:42) ~[spring-data-dynamodb-4.5.4.jar:na]
	at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
	at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
	at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
	at org.socialsignin.spring.data.dynamodb.repository.query.PartTreeDynamoDBQuery.doCreateQuery(PartTreeDynamoDBQuery.java:65) ~[spring-data-dynamodb-4.5.4.jar:na]
	at org.socialsignin.spring.data.dynamodb.repository.query.AbstractDynamoDBQuery.doCreateQueryWithPermissions(AbstractDynamoDBQuery.java:76) ~[spring-data-dynamodb-4.5.4.jar:na]
	at org.socialsignin.spring.data.dynamodb.repository.query.AbstractDynamoDBQuery$CollectionExecution.execute(AbstractDynamoDBQuery.java:98) ~[spring-data-dynamodb-4.5.4.jar:na]
	at org.socialsignin.spring.data.dynamodb.repository.query.AbstractDynamoDBQuery.execute(AbstractDynamoDBQuery.java:288) ~[spring-data-dynamodb-4.5.4.jar:na]
	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:499) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:477) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at com.sun.proxy.$Proxy70.findByDeviceConfiguration_Host(Unknown Source) ~[na:na]
	at com.github.spring_data_dynamodb.examples.multirepo.Application.demoNoSQL(Application.java:127) [classes/:na]
	at com.github.spring_data_dynamodb.examples.multirepo.Application.lambda$0(Application.java:53) [classes/:na]
	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]

I'm trying to migrate a database from MongoDB, but it's kinda frustrating that I can't search inside a document attribute.

@derjust
Copy link
Owner

derjust commented May 22, 2018

Yes, sadly this is not yet supported. Please see #114

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants