derjust/spring-data-dynamodb

How to have a separate java class as just HashKey

Opened this issue · 1 comments

Expected Behavior

Hi ,
As one of my application requirement which involves dynamodb I have to segregate my key columns and other non-key columns in separate java classes and then form the underlying dynamodb table using these classes.
I achieved this by writing a separate class containing only the key attribute and annotated the class with DynamoDBDocument and then in my table definition class I added this class as a field annotated with DynamoDBIgnore , then I wrote the getter setter for this field's inner variables (which are actually the keys). I marked the getter as the DynamoHash and DynamoRangeKeys accordingly . Life so far was easier
Now the problem is I have a table containing single partition key (this table only has hashkeys without any range keys) and the above approach is not working here
Please help me with identifying what needs to be done as literally now I am running out of options.

Actual Behavior

My Crud Repository is failing while performing operation with this error

Expected ID type to be the same as the return type of the hash key method ( class java.lang.String ) : class com.yantriks.yso.organization.services.core.dao.key.DynamoOrgKey",

*Key

package com.yantriks.yso.organization.services.core.dao.key;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDocument;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.yantriks.yso.external.db.support.DynamoKey;

import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@DaTa
@DynamoDBDocument
public class DynamoOrgKey implements DynamoKey{

private static final long serialVersionUID = 8085449185252111836L;

 @DynamoDBHashKey
  private String orgId;

}

**Entity

package com.yantriks.yso.organization.services.core.dao.entity;

import java.time.ZoneId;

import org.springframework.data.annotation.Id;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIgnore;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverted;
import com.yantriks.yso.external.db.converters.ZoneIdConverterClass;
import com.yantriks.yso.external.db.support.DynamoAuditGenericEntity;
import com.yantriks.yso.organization.services.core.dao.key.DynamoOrgKey;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@DaTa
@AllArgsConstructor
@NoArgsConstructor
@DynamoDBTable(tableName = DynamoOrgEntity.TABLE_NAME)
public class DynamoOrgEntity extends DynamoAuditGenericEntity{

private static final long serialVersionUID = 1872486358255339960L;

public static final String TABLE_NAME = "ORG";

 @Id
 @DynamoDBIgnore
 private DynamoOrgKey orgKey;

  @DynamoDBAttribute(attributeName = "system_hold_time")
  private Integer systemHoldTime;

  @DynamoDBAttribute(attributeName = "system_locale")
  @DynamoDBTypeConverted(converter = ZoneIdConverterClass.class)
  private ZoneId systemLocale;

  @DynamoDBAttribute(attributeName = "optimization_rule_id")
  private String optimizationRuleId;
  
 @DynamoDBHashKey(attributeName="org_id")
   public String getOrgIdKey() {
	  return null != this.orgKey?this.orgKey.getOrgId():null; 
}
  
  public void setOrgIdKey(String orgId) { 
	  if(null == this.orgKey) {
		  this.orgKey  = new DynamoOrgKey();
  } 
	  this.orgKey.setOrgId(orgId);
}

}


package com.yantriks.yso.external.db.support;

import java.time.LocalDateTime;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDocument;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverted;
import com.yantriks.yso.external.db.converters.LocalDateTimeConverterClass;

import lombok.Data;

@DaTa
@DynamoDBDocument
public abstract class DynamoAuditGenericEntity implements DynamoEntity {

private static final long serialVersionUID = -4217561694452434319L;

@DynamoDBAttribute(attributeName = "update_time")
@DynamoDBTypeConverted(converter = LocalDateTimeConverterClass.class)
protected LocalDateTime updateTime;

@DynamoDBAttribute(attributeName = "update_user")
protected String updateUser;
}

********Repository

package com.yantriks.yso.external.db.support;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;

@NoRepositoryBean
public interface DynamoRepository<K extends DynamoKey,T extends DynamoEntity>
extends CrudRepository<T,K> {

}


package com.yantriks.yso.external.db.repositories;

import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
import org.springframework.stereotype.Repository;
import com.yantriks.yso.external.db.support.DynamoRepository;
import com.yantriks.yso.organization.services.core.dao.entity.DynamoOrgEntity;
import com.yantriks.yso.organization.services.core.dao.key.DynamoOrgKey;

@EnableScan
@repository
public interface DynamoOrgRepository
extends DynamoRepository<DynamoOrgKey, DynamoOrgEntity> {

}

What was the solution to your problem?