How do I use DynamoDBAutoGeneratedKey to give me an auto generated key? - java

I need to use DynamoDBAutoGeneratedKey from the AWS SDK to give me a random key(of type String) that I can then use to do something. I can't find any examples online of doing this, and while it seems like it should be relatively straightforward I am really struggling to get this working. Can anyone link me to an example of this being used?

Found easy answer.
String uniqueID = UUID.randomUUID().toString();
Screw using DynamoDBAutoGeneratedKey, sounds like a headache.

#DynamoDBTable( tableName = "Details")
public class Details
{
#DynamoDBGeneratedUuid( DynamoDBAutoGenerateStrategy.CREATE )
private UUID id;
....
#DynamoDBHashKey(attributeName = "id")
#DynamoDBAutoGeneratedKey
public UUID getId()
{
return id;
}
// also you need to add the setter otherwise you will get an exception
public void setId(UUID id)
{
this.id = id;
}
...

Related

How can I set a custom name for #JsonProperty?

I have got a problem with setting a custom name for #JsonProperty, I use mustache to generate classes form openapi.yaml and make a library to use it in different projects.
Here is a sample of yaml:
PriceType:
type: object
properties:
ID:
type: string
And I get the next:
#JsonProperty("ID")
private String ID;
public PriceType ID(String ID) {
this.ID = ID;
return this;
}
But I need:
#JsonProperty("user_id")
private String ID;
public PriceType ID(String ID) {
this.ID = ID;
return this;
}
I suspect it can be because of my mustche file defenition for the property:
#JsonProperty("{{baseName}}")
but I dont know how to make it, at the same time I have to do it just for this particular scenario, which means basename in my mustache stays and other classes are generated normally
I have tried to change my mustache file, but crashed it. And tried to somehow set jsonproperty name in yaml but i don't know how.

Morphia: Saving document in a specific collection

I am using Morphia 2.x, and I have a usecase where i want to save 1 Entity class into 2 different Collections.
However Morphia 2.x save method doesn't have any option to pass a Collection Name, Where as Morphia 1.x had this option.
I am not sure why they have removed this option.
#Entity
public class Unit {
#Id
private String id;
protected Unit() {
}
public String getId() {
return this.id;
}
public void setId(final String id) {
this.id = id;
}
}
Morphia 2.3 will reintroduce this functionality as an option passed via the alternateCollection option on InsertOneOptions (among other options classes). I'm working to get this out soon.

Pass value from Mono/Flux to methods

I'm new to reactive programming concept. I'm following the "Learning Spring Boot 2.0" and the described simple concepts/examples are understandable. But I don't get how to use Mono/Flux in more complex use cases.
Some example with with spring boot, mongo and project reactor
My model
User
#Id
private String id;
private String login;
Comment
#Id
private ObjectId _id;
private String content;
private String ownerLogin;
so this simple example to get comments by owner works fine
#GetMapping(params = "login")
#ResponseBody
public Flux<Comment> getAllCommentsByLogin(#RequestParam("login") String login) {
return commentRepository.findByOwnerLogin(login);
};
But if I would slightly change the model to store owner by entity id, it would be not so ease to retrieve the comments by owner
Comment
#Id
private ObjectId _id;
private String content;
private String ownerId;
my intention is to make rest controller easy to use by end user and first find the user entity by login and if exists all user comments
#GetMapping(params = "login")
#ResponseBody
public Flux<Comment> getAllCommentsByLogin(#RequestParam("login") String login) {
return commentRepository.findByOwnerId(userRepository.findByLogin(login).map(user2 -> user2.getId())
};
this soulution is obviously wrong, but I don't know if the whole approach is wrong or only this method.
Let's hope your userRepository.findByLogin() returns Mono<User>, then code should be like this:
return userRepository.findByLogin(login)
.flatMapMany(user -> commentRepository.findByOwnerId(user.getId()));

How to save object nested inside another object with ormlite?

I try to save "BestandItem", which is made out of "ScanItem" and a Calendar with Ormlite. After I restart my app (for Android with Android Studio, which I write with Java) the content of ScanItem is gone, but only if it is inside of a Bestanditem.
This is my ScanItem:
#DatabaseTable(tableName = "scanItem")
public class ScanItem{
#DatabaseField (generatedId = true)
private int id;
#DatabaseField
private String barcode;
#DatabaseField
private String name;
public ScanItem(String barcode, String name) {
this.barcode = barcode;
this.name = name;
}
public ScanItem(){}
And this is my BestandItem:
#DatabaseTable (tableName = "bestandItem")
public class BestandItem {
#DatabaseField (generatedId = true)
private int id;
#DatabaseField (foreign = true, foreignAutoCreate = true)
private ScanItem scanItem;
#DatabaseField (dataType = DataType.SERIALIZABLE)
private Calendar ablaufDatum;
public BestandItem() { }
public BestandItem(ScanItem scanItem, Calendar ablaufDatum) {
this.scanItem = scanItem;
this.ablaufDatum = ablaufDatum;
}
Some of the things I have tried:
- Ormlite Documentation
- First Stackoverflow Answer
- Second Stackoverflow Answer
For more code see my github project: Github SmartFridge
My Ormlite Database has a UtilConfigClass and a always updated config.txt.
What did I do wrong here? Why dosn't save the ScanItem right?
After some checks, I can say that the other methods work just fine. (only after the lost of the ScanItem, I get NullPointerException). My conclusion is, that the problem is the constructor of the BestandItem.
I think I did something wrong with generatedID and/or foreignAutoCreate, but I dont really understand how do use it properly.
Also what exactly foreignAutoRefresh does.
I have tried to change the ID and generatedId around, because I think there lies the problem.
After some tests and countless trys, I now have a solution.
Because the barcode inside of ScanItem is unique, I could use it for a ID, instead of a generated id.
And with an existing ID, the programm now works.

Generating UUID based #ID for Playframework 2.3x

Good day Guys,
I want to create Models that don't use the default #Id auto generation for Playframework and Ebeans. I have seen online that there are options for Using GenericModel, however that class doesn't seem to be included in version 2.3x. I have done this in order to workaround it but i still fall short my aim
public class ProductVariants extends Model
{
#Id
String id;
public String getId() {
return (this.id == null) ? UUID.randomUUID().toString() : this.id;
}
public void setId(String id) {
this.id = id;
}
}
The issue with this is that i have to manually set the ID before i can save the object e.g.
productVariant.setId(productVariant.getId());
productVariant.save();
Both for a primary model and all it related models with a OneToMany relationship, and it is currently giving me issues when i bind from the view to the Model object with error ERROR executing DML bindLog[] error[Field 'id' doesn't have a default value]]].
Please any help will be appreciated.
Good day Guys,
I finally fixed this by using the UUID class that ships with the JDK. So when you are creating your Models you create them with the
#Id
public java.util.UUID id
Also in the routes file if you need to map to a record by the ID you can do that by doing something like this
GET /:pid/edit controllers.Application.edit(pid: java.util.UUID)

Categories