I want to fetch multiple data using Aerospike Repositories. I have defined a method:
List<Profile> findByIds(List<String> profilekeys, Class<Profile> class1);
And trying to use it as:
profileRepository.findByIds(profilekeys, Profile.class);
The Profile is:
#Id
private String id;
private String obj;
But, I am getting issues like:
No property ids found for type Profile! Did you mean 'id'?
How can I solve the issue?
Related
What is the best way to check if a record exists and if it doesn't, create it (avoiding duplicates)?
Keep in mind that this is a distributed application running across many application servers.
I'm trying to avoid these:
Race Conditions
TOCTOU
A simple example:
Person.java
#Entity
public class Person {
#Id
#GeneratedValue
private long id;
private String firstName;
private String lastName;
//Getters and Setters Omitted
}
PersonRepository.java
public interface PersonRepository extends CrudRepository<Person, Long>{
public Person findByFirstName(String firstName);
}
Some Method
public void someMethod() {
Person john = new Person();
john.setFirstName("John");
john.setLastName("Doe");
if(personRepo.findByFirstName(john.getFirstName()) == null){
personRepo.save(john);
}else{
//Don't Save Person
}
}
Clearly as the code currently stands, there is a chance that the Person could be inserted in the database in between the time I checked if it already exists and when I insert it myself. Thus a duplicate would be created.
How should I avoid this?
Based on my initial research, perhaps a combination of
#Transactional
#Lock
But the exact configuration is what I'm unsure of. Any guidance would be greatly appreciated. To reiterate, this application will be distributed across multiple servers so this must still work in a highly-available, distributed environment.
For Inserts: if you want to prevent same recordsto be persisted, than you may want to take some precoutions on DB side. In your example, if firstname should be unique, then define a unique index on that column, or a agroup of colunsd that should be unique, and let the DB handle the check, you just insert & get exception if you're inserting a record that's already inserted.
For updates: use #Version (javax.persistence.Version) annotation like this:
#Version
private long version;
Define a version column in tables, Hibernate or any other ORM will automatically populate the value & also verison to where clause when entity updated. So if someone try to update the old entity, it prevent this. Be careful, this doesn't throw exception, just return update count as 0, so you may want to check this.
Kindly help me to find out a solution to my problem.
I have a property of type String in my entity which is not primary key.
This would act as system generated unique profile id for users in my project.
I want to auto generate this value like a random string of particular size.
Could you please help me to arrive at a solution.
I have tried the #Generator and custom id generator class. but still null is getting inserted to the field in db.
I am using maria db.
Hi you can use the hibernate events.
#PostPersist Executed after the entity manager persist operation is actually executed or cascaded. This call is invoked after the database INSERT is executed.
Starting with Java 5, the UUID class provides a simple means for generating unique ids. The identifiers generated by UUID are actually universally unique identifiers.
Please follow the sample example.
public class TestModel {
#Column("column_name")
private String uid;
//setter and getter for uid
#PrePersist
protected void onCreate() {
// set the uid
setUid(java.util.UUID.randomUUID());
}
}
Please find the below link for more information on #PrePersist.
https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/listeners.html
I'm experimenting with play (v2.2.2), and I have it connected to MongoDB (v2.4.6) using jackson.
I have a models.Role class with the following attributes:
#Id
#ObjectId
public String id;
public String name;
public ArrayList<String> permissions;
On the template (roles.scala.html), I can easily get the list of permissions to be printed on the HTML, but when I try to add a new role passing a single permission as a string in the form as an #InputText field, it does not get recorded in MongoDB. I suppose that it is because play/scala is trying to assign a simple String to an ArrayList<String>.
Any ideas on the propper approach? Maybe I should do some logic on the create() method under Role class?
I have an object which has 3 fileds:
public class tags{
#Property("n")
private String name;
#Property("t")
private int type;
#Property("r")
private int rank;
.....
}
I am using morphia to communicate to my MongoDB.
I want to save al lthe fileds to the DB, but while retreiving I want to query only based on the 'name' and 'type' fields within my object. I have tried using the #Transient Annotation, but it completely ignores the field during load/save.
This is a very common use case.
The morphia wiki describes using filters or fluent interface: https://github.com/mongodb/morphia/wiki/Query#wiki-filter
Here's an example:
ds.createQuery(tags.class).field('name').equal('idbentley').field('type').equal(1);
If you are looking for limited query results https://github.com/mongodb/morphia/wiki/Query#wiki-ignoring-fields will cover that:
ds.createQuery(tags.class).retrievedFields(true, "name", "type").get();
Beware that you should only read these limited result sets or write back specific values. If you save them back, you will lose all the values you didn't retrieve.
I'm still trying to get my hands around mongodb and how best Entities can be mapped. if you take for example: the entity user and the entity addresses. there could be one-to-many when someone is coming from jpa background. Here in mongo i don't want to use dbref. So addresses are in a Set collection in user.
Supposing i was using spring-data-mongo:
Question 1 : should both User and Address have the #Document annotation?Or just User?
Question 2 : what is the best way to query for addresses of a user. It is possible at first place? Because right now I query to get the User by username or Id and then get the addresses of the user.Can I query directly for sub-document? if yes how is it done using spring-data-mongo Criteria Query:
#Document
public class User{
#Id
private Long ID;
private String username;
private Set<Address> addresses = new HashSet<Address>();
...
}
#Document
public class Address {
#Id
private Long ID;
private String city;
private String line1;
...
}
Question 1: No, #Document is not strictly necessary at all. We just leverage this on application startup if you activate classpath-scanning for document classes. If you don't the persistence metadata scanning will be done on the first persistence operation. We traverse properties of domain objects then, so Address will be discovered.
Question 2: You'll have to read the User objects entirely as MongoDB currently does not allow returning sub-documents. So you'll have to query for the entire Userdocument but can restrict the fields being returned to the addresses field using a fieldSpec on the Query object or the repository abstraction's #Query annotation (see ref docs).