michaellavelle / spring-data-dynamodb

Simplifies the development of creating an Amazon DynamoDB-based Java data access layer
https://derjust.github.io/spring-data-dynamodb/
Apache License 2.0
169 stars 284 forks source link

How to get the ID of an entity #30

Closed jAddict7 closed 10 years ago

jAddict7 commented 10 years ago

I have autogenerated my hash key using @Id and in JPA, there is an option like @GeneratedValue(strategy = GenerationType.IDENTITY), is there anything alternative to it where I can query ID too for the entity returned via GET. For example,

@DynamoDBTable(tableName = "test") public class Test implements Serializable{ @Id private String id;

@DynamoDBHashKey(attributeName = "id") @DynamoDBAutoGeneratedKey public String getId() { return id; }

public String setId() {
    this.id = id;
}

}

I need to get this ID when GET method called. But Query not fetching ID from finder method. Is it possible @michaellavelle .. Thank you.

michaellavelle commented 10 years ago

Hi

I'm trying to understand your issue correctly - perhaps you could clarify.

Is it that existing entities you have loaded are not coming back with their id's set, or is it that you aren't able to auto-generate an Id ?

The title of your issue suggests that you aren't getting ids back, however I've just done some tests locally and this is working for me - could you confirm?

The other point you raise is about auto-generating ids - as far as I know this isn't something that DynamoDB supports, but please do let me know if I'm wrong on this. I don't believe DynamoDB supports UUID generation in the same way that many databases do - as I result there is no concept of @GeneratedValue(strategy = GenerationType.IDENTITY), in the module.

Cheers,

Michael

For existing entities in DynamoDB, spring-data-dynamodb should correctly retrieve the id when it loads the entities - this is working for me in the tests I have just done - is this not the case for you?

It s

jAddict7 commented 10 years ago

Thanks Michael, For me id's are not returning from the entity. Keys are generating properly and there is no issue on it.

michaellavelle commented 10 years ago

That sounds odd - do you have example code that I can run to reproduce this as I am unable to reproduce here? Thanks,

Michael

jAddict7 commented 10 years ago

Domain Class:

@DynamoDBTable(tableName = "test") public class Test implements Serializable{

@Id private String id;
private String name;
private String email;
private String dob;
private Date registeredAt = new Date();

@DynamoDBHashKey(attributeName = "id")
@DynamoDBAutoGeneratedKey
public String getId() {
    return id;
}

@DynamoDBAttribute(attributeName = "name")
public String getName() {
    return name;
}

@DynamoDBAttribute(attributeName = "email")
public String getEmail() {
    return email;
}

@DynamoDBAttribute(attributeName = "dob")
public String getDob() {
    return dob;
}
@DynamoDBAttribute(attributeName = "registeredAt")
public Date getRegisteredAt() {
    return registeredAt;
}

public void setId(String id) {
    this.id = id;
}

public void setEmail(String email) {
    this.email = email;
}

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

public void setDob(String dob) {
    this.dob = dob;
}

public void setRegisteredAt(Date registeredAt) {
    this.registeredAt = registeredAt;
}

}

Repository Class:

@EnableScan @RestResource(path="test", rel="test") public interface TestRepository extends PagingAndSortingRepository<Test, String>{

}

michaellavelle commented 10 years ago

This works for me in fact...

I just took your domain class and added it to my local running version of spring-data-dynamodb-demo, and created a repository for that class. Next I created a hash-only dynamodb table for the class in AWS console and put a single item in the table ( again using the console).

Then I could successfully browse the contents of the table in the demo, and I could see the id without any problems.

What happens if you do this - do you see the id?

How are you seeing no id - are you explicitly calling the getId() method on a returned entity, or are you using spring-data-rest to display the entities in the browser?

(BTW, you shouldn't need the @Id attribute on your domain class - for hash-key only entities it's just the @DynamoDBHashKey method annotation which is needed. @Id is used to annotate composite keys for hash-and range key tables. )

jAddict7 commented 10 years ago

No I am unable to, When I put pageable then I am receiving like this,

{ _links: { self: { href: "http://localhost:8080/test{?page,size,sort}" templated: true }- }- _embedded: { tests: [1] 0: { name: "Gowtham" _links: { self: { href: "http://localhost:8080/test/***_id-here**_**" }- }-

}-

}- page: { size: 20 totalElements: 1 totalPages: 1 number: 0 }- }

michaellavelle commented 10 years ago

What are you seeing in place of *_id-here_**" ? are you seeing null ?

jAddict7 commented 10 years ago

No am getting ID there?? we cant access ID as like name or what?? If I require ID alone without the links.self.href means only way is to substring the href value only as far here. If I use List in place of page in repository also am not getting that link too. Is there any possibility to get ID as like name attribute in the resulting JSON? If my question is stupid, please forgive. Am new in case of this.

michaellavelle commented 10 years ago

I think the issue you are having relates to spring-data-rest, and not to spring-data-dynamodb.

If I understand correctly, you are using spring-data-dynamodb to retrieve data, and spring-data-rest to display the data.

Spring-Data-DynamoDB is correctly retreiving the ids ( otherwise you wouldn't be able to see them in the "self" links that spring-data-rest is displaying).

I think your issue is that those ids are not being displayed in the JSON representation - this is a spring-data-rest issue.

I can't see your application, but it may be that you need to configure spring-data-rest to display the ids for your specific domain class, using "exposeIdsFor" methods on the config - see the following config class for an example:

https://github.com/michaellavelle/spring-data-dynamodb-demo/blob/master/src/main/java/org/socialsignin/springframework/data/dynamodb/demo/config/DemoRestMvcConfiguration.java

Hope this helps.

Michael

jAddict7 commented 10 years ago

Thank you Michael. This works perfectly.

michaellavelle commented 10 years ago

You're welcome - glad you got it working.