Using ObjectId as String in Java (Manual reference) with spring-data mongodb - java

In MongoDB documentation they suggest to use ObjecId for manual references.
please see https://docs.mongodb.com/manual/reference/database-references/#document-references
original_id = ObjectId()
db.places.insert({
"_id": original_id,
"name": "Broadway Center",
"url": "bc.example.net"
})
db.people.insert({
"name": "Erin",
"places_id": original_id,
"url": "bc.example.net/Erin"
})
I'm using spring-data-mongodb and what I'm looking for is to have a People class defined like this:
#Document
public class People {
private String name;
#Reference // or any Annotation to convert an ObjectId to a String
private String placesId;
private String url;
}
How to have a "places_id" as ObjectId in mongoDB but mapped to a String in our POJO ?
I was expecting to have an annotation like #Reference but it seems to not be implemented.
I don't understand why we don't have this kind of annotation in spring-data-mongodb. I don't want to implement an explicit converter like suggested in spring documentation for all documents that use manual references.
Maybe it's not the right approach.
Did I miss something ?
UPDATE :
I like the idea to have a POJO using only String instead of ObjectId. Let's say I've got a class Place like this :
#Document
public class Place {
#Id
private String id;
private String name;
}
place.getId() will be a String but people.getPlaceId() will be an ObjectId. I want to avoid this unnecessary mapping.

The solution would be:
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.FieldType;
public class People {
#Field(targetType = FieldType.OBJECT_ID)
private String placesId;
}
This will map POJO string to ObjectId in MongoDB.

Why don't you leave the field as ObjectId?
#Document
public class People {
private String name;
private ObjectId placesId;
private String url;
}
If you want to query by this field you can do this:
For lists
List<String> ids // the ids as strings
List<ObjectId> objIds = ids .stream()
.map(i -> new ObjectId(i))
.collect(Collectors.toList());
For single String
String id // single id
ObjectId objId = new ObjectId(id);

If you want to make a real reference to an other object in your database, use the #DBRef annotation which is provided by Spring Data.
Your updated code could look like the following:
#Document
public class People {
private String name;
#DBRef
private Place place;
private String url;
}
Spring Data will then automatically map a Place object to your People object. Internally this is done with a reference to the unique ObjectId. Try this code and have a look at your mongo database.
For more information have a look at: MongoDb with java foreign key

I have a solution very simple:
#JsonSerialize(using= ToStringSerializer.class)
private ObjectId brandId;
...
put that on the attribute that is Object Id, and the ObjectId gets and inserts like string

Related

Spring Data MongoDB Unique Embedded Fields

So, I have one #Document class which has a embedded pojo field which I want it to be unique for the document based on a key in the pojo class. I tried using #CompoundIndex & #Indexed to mark it as unique but it doesn't seem to work.
#Document
public class Project {
private String id;
private String name;
private List<Details> details = new ArrayList<>();
}
public class Details{
private String key;
private String description;
}
What I want to achieve is that a project document should have unique details field in it with it's key being unique. But when I have the
#CompoundIndexes({ #CompoundIndex(name = "details_key", def = "{'details.key':1}", unique = true) }) on the Project class it doesn't work. Which I thought it should. Or am I wrong somewhere with my understanding. As I am new to this.

Is it possible to use a MongoRepository with not-fixed document structure? [duplicate]

Mongodb is a no-schema document database, but in spring data, it's necessary to define entity class and repository class, like following:
Entity class:
#Document(collection = "users")
public class User implements UserDetails {
#Id private String userId;
#NotNull #Indexed(unique = true) private String username;
#NotNull private String password;
#NotNull private String name;
#NotNull private String email;
}
Repository class:
public interface UserRepository extends MongoRepository<User, String> {
User findByUsername(String username);
}
Is there anyway to use map not class in spring data mongodb so that the server can accept any dynamic JSON data then store it in BSON without any pre-class define?
First, a few insightful links about schemaless data:
what does “schemaless” even mean anyway?
“schemaless” doesn't mean “schemafree”
Second... one may wonder if Spring, or Java, is the right solution for your problem - why not a more dynamic tool, such a Ruby, Python or the Mongoshell?
That being said, let's focus on the technical issue.
If your goal is only to store random data, you could basically just define your own controller and use the MongoDB Java Driver directly.
If you really insist on having no predefined schema for your domain object class, use this:
#Document(collection = "users")
public class User implements UserDetails {
#Id
private String id;
private Map<String, Object> schemalessData;
// getters/setters omitted
}
Basically it gives you a container in which you can put whatever you want, but watch out for serialization/deserialization issues (this may become tricky if you had ObjectIds and DBRefs in your nested document). Also, updating data may become nasty if your data hierarchy becomes too complex.
Still, at some point, you'll realize your data indeed has a schema that can be pinpointed and put into well-defined POJOs.
Update
A late update since people still happen to read this post in 2020: the Jackson annotations JsonAnyGetter and JsonAnySetter let you hide the root of the schemaless-data container so your unknown fields can be sent as top-level fields in your payload. They will still be stored nested in your MongoDB document, but will appear as top-level fields when the ressource is requested through Spring.
#Document(collection = "users")
public class User implements UserDetails {
#Id
private String id;
// add all other expected fields (getters/setters omitted)
private String foo;
private String bar;
// a container for all unexpected fields
private Map<String, Object> schemalessData;
#JsonAnySetter
public void add(String key, Object value) {
if (null == schemalessData) {
schemalessData = new HashMap<>();
}
schemalessData.put(key, value);
}
#JsonAnyGetter
public Map<String, Object> get() {
return schemalessData;
}
// getters/setters omitted
}

Morphia custom converter: Can I insert a nested Java object's fields directly into the parent document?

I have the following classes:
#Entity(value="students", noClassnameStored=true)
public class Student {
#Id
private String studentId;
private String firstName;
private String lastName;
private Address address;
}
public class Address {
private String street;
private String city;
private Integer zip;
private String state;
}
When I save the instances of the Student class, I want them to be saved in the following format inside the Mongo database:
{
_id: "12345",
firstName: "Cler",
lastName: "Fit",
street: "123 xyz"
city: "unnt",
zip: 76443
state: "IM"
}
In other words, even though the Java object being saved has a nested member, I want it to be saved as a flat structure in the resulting document. Can I do that in Morphia? I am aware I can do it by defining a custom converter on the "Student" class. But I have way too many fields in that class than I have shown above, and I don't want to individually handle every one of those. Ideally I want a custom converter defined on the "Address" class which can hopefully accomplish the same result.
Tried searching through the Morphia API documentation. Unfortunately nothing much is mentioned in the API documentation.
Morphia does not currently support flattened documents like that, no. You might be able to coerce that with the #Pre- and #Post- annotations to massage data in and out of that shape, but that's a lot of manual mapping.

manipulate JSON array in Spring Hibernate

I'm trying to store an JSON Array in MySQL with Spring and Hibernate.
The application gets the likes from the users and stores the id of a user in a JSON Array, for example
[1,2,3,4,5,6]
in a column name likeswith type text (MySQL)
¿How can I put a new id in the array? I mean, get the JSON form database and insert the id and count the number of elements.
I'd use transient variables as a solution for this problem. I don't think there is a better solution for this. You may also try hibernate interceptors for this as a solution.
#Table
#Entity(name='Likes')
public class LikesEntity implements Serializable {
#Transient
private List<String> userIds;
#Column
private String likes;
#Column
private String count;
//getter and setters for the like and count
...
public void addUserId(String id){
this.userIds.add(userIds);
this.likes = convertToJsonArray(userIds); // use GSON or Jackson or any other library that can help you convert array to JSON string
this.count = this.userIds.size();
}
}
within your service layer do the following
#Transactional(lockmode = LockMode.READ){
LikesEntity le = sess.get(LikesEntity.class,234);
le.addUserId("userPk");
session.saveOrUpdate(le);
}

Many to One in hibernate, get column type

In Hibernate when I got simple Pojo, for example User:
private class User{
private String name;
}
When I want to get property of the "name", a simply use:
sessionFactory.getClassMetadata(User.class).getPropertyType("name")
But when I add a class Grou to my User:
private class User{
private String name;
private Group group;
}
My previous code :
sessionFactory.getClassMetadata(User.class).getPropertyType("group.name")
doesn't work.
Any way to solve it?
Split your query group.name into the corresponding parts group and name.
Class<?> currentClass = User.class;
for(String field: "group.name".split("\\Q.\\E")) {
currentClass = sessionFactory.getClassMetadata(currentClass).getPropertyType(field);
}
for group.name to work , you have to specify the mapping for Group class also.
If you have already specified a mapping and still its not working then please share the mapping file here.
Issue might be in the mapping only.

Categories