Java Amazon DynamoDB和类层次结构
我正在使用库处理SpringBoot和AmazonDynamodb。该类层次结构存在问题:Java Amazon DynamoDB和类层次结构,java,spring-boot,spring-data,amazon-dynamodb,Java,Spring Boot,Spring Data,Amazon Dynamodb,我正在使用库处理SpringBoot和AmazonDynamodb。该类层次结构存在问题: @DynamoDBTable(tableName = "EventLogs") abstract class AbstractEventLogEntry implements Serializable { private static final long serialVersionUID = 7713867887326010287L; @DynamoDBHashKey(attributeNam
@DynamoDBTable(tableName = "EventLogs")
abstract class AbstractEventLogEntry implements Serializable {
private static final long serialVersionUID = 7713867887326010287L;
@DynamoDBHashKey(attributeName = "EventId")
private String eventId;
@DynamoDBAttribute(attributeName = "GeneratedAt")
@DynamoDBMarshalling(marshallerClass = ZonedDateTimeMarshaller.class)
private ZonedDateTime generatedAt;
AbstractEventLogEntry() {
eventId = new UUID().value();
generatedAt = ZonedDateTime.now();
}
/* Getters / Setter */
}
…另一类:
public abstract class EventLogEntry extends AbstractEventLogEntry {
private static final long serialVersionUID = 1638093418868197192L;
@DynamoDBAttribute(attributeName = "UserId")
private String userId;
@DynamoDBAttribute(attributeName = "EventName")
private String eventName;
protected EventLogEntry(AdminEvent event) {
userId = event.getUserName();
eventName = event.getClass().getSimpleName();
}
protected EventLogEntry(UserEvent event) {
userId = event.getUserId();
eventName = event.getClass().getSimpleName();
}
/* Getters / Setter */
}
…另一个:
public class AdminEventLogEntry extends EventLogEntry {
private static final long serialVersionUID = 1953428576998278984L;
public AdminEventLogEntry(AdminEvent event) {
super(event);
}
}
…最后一个:
public class UserEventLogEntry extends EventLogEntry {
private static final long serialVersionUID = 6845335344191463717L;
public UserEventLogEntry(UserEvent event) {
super(event);
}
}
典型的类层次结构。现在,我正在尝试使用公共存储库存储AdminEventLogEntry
和UserEventLogEntry
:
@EnableScan
public interface EventLogEntryRepository extends DynamoDBCrudRepository<EventLogEntry, String> {
// ...
}
一旦我(再次)宣布钥匙有效:
@DynamoDBHashKey(attributeName = "EventId")
private String eventId;
所以我的问题是:我是否需要重新声明层次结构之间可能通用的所有字段?它似乎无法识别来自父项的HashKey
有什么线索吗?我(不久前)找到了解决方案,所以我正在更新帖子,以防将来有人需要它。注意,抽象
类已经不存在了,也许你可以出于自己的目的对它进行调整,那时我没有时间测试它(不是现在…所以它更简单,从面向对象的角度来看可能不完全正确)
问题在于类层次结构和(基于Spring的)AmazonDB客户端的配置。接下来的类是实际的解决方案
(a)Amazon DynamoDB客户端的Spring配置文件。
@EnableScan
public interface EventLogEntryRepository extends DynamoDBCrudRepository<EventLogEntry, String> { }
请注意,您可能不需要dynamodboOperationsRef
,因为它是
仅在“每个环境”需要不同表的情况下使用。具有
DynamoDB(如果您只有一个帐户)您不能有不同的帐户
“环境”,所以你必须找到一种解决方法。这是
解决方案:为表添加前缀(并根据需要应用安全设置)
必需)
(b)DynamoDB注释的“实体”类
package io.shido.events;
// imports
@DynamoDBTable(tableName = "EventLogs")
final class EventLogEntry implements Serializable {
// Define your own long serialVersionUID
@DynamoDBHashKey(attributeName = "EventId")
private String eventId;
@DynamoDBTypeConvertedEnum
@DynamoDBAttribute(attributeName = "EventType")
private EventType type;
@DynamoDBAttribute(attributeName = "EntityId")
private String entityId;
@Scrambled
@DynamoDBAttribute(attributeName = "Event")
private Event event;
@DynamoDBAttribute(attributeName = "GeneratedAt")
@DynamoDBTypeConverted(converter = ZonedDateTimeConverter.class)
private ZonedDateTime generatedAt;
public EventLogEntry() {
generatedAt = ZonedDateTime.now();
}
public EventLogEntry(AdminEvent event) {
this();
eventId = event.getId();
type = EventType.ADMIN;
entityId = event.getEntityId();
this.event = event;
}
public EventLogEntry(UserEvent event) {
this();
eventId = event.getId();
type = EventType.USER;
entityId = event.getEntityId();
this.event = event;
}
// getters and setters (a MUST, at least till the version I'm using)
// hashCode(), equals and toString()
}
(c)Spring存储库定义。
@EnableScan
public interface EventLogEntryRepository extends DynamoDBCrudRepository<EventLogEntry, String> { }
你找到解决办法了吗?找到了!到目前为止,它的工作非常棒。我会把它寄出去的。
@EnableScan
public interface EventLogEntryRepository extends DynamoDBCrudRepository<EventLogEntry, String> { }
{
"TableName" : "local-EventLogs",
"AttributeDefinitions" : [
{ "AttributeName" : "EventId", "AttributeType" : "S" },
{ "AttributeName" : "EventType", "AttributeType" : "S" },
{ "AttributeName" : "EntityId", "AttributeType" : "S" },
{ "AttributeName" : "Event", "AttributeType" : "S" },
{ "AttributeName" : "GeneratedAt", "AttributeType" : "S" }
],
"KeySchema" : [ { "AttributeName" : "EventId", "KeyType" : "HASH" } ],
"ProvisionedThroughput" : { "ReadCapacityUnits" : 10, "WriteCapacityUnits" : 10 }
}