See we have a class(entity) A and class(entity) Category:
class A {
private String title;
private Category category;
}
class Category {
.....
}
I want to apply hibernate search on the title and under one category, Any idea on this?
Thanks in advance.
Add #Field on your title field, add #IndexedEmbedded on your category field, then also annotate the fields you want to search on in the Category type.
https://docs.jboss.org/hibernate/search/5.6/reference/en-US/html_single/#example-indexing-associations
Related
I have 3 classes:
` ClassA{
private String name;
private List<ClassB> classb;
}
ClassB{
private String name;
private List<ClassC> classc;
}
ClassC{
private String name;
private String email;
}`
Now, my situation is based on ClassC's name , I have to display the email.
Here, I write a mongo query in my repository layer(in spring boot) as:
#Query(value="{'classb.classc.name':?0}",fields="{'classb.classc.email':1}")
But this mongo Query does not work properly, it displays every email from classC without matching the selection.
I am not sure where I am wrong. Please guide me through how to work out in such nested situation.
Thanks
I have a collection:
public class Person {
private String name;
private Integer age;
}
I want to delete field age in all the documents. So the schema gonna look like that:
public class Person {
private String name;
}
I'm using MongoRepositoty and I've been trying to write this method:
#Repository
public interface PersonRepository extends MongoRepository<Person, String> {
#Query("{$updateMany: [ {}, { $unset: {'age': ''} }]}")
void deleteAgeField();
}
I tried different brackets and quotes, but it all ends up with errors. What's wrong with my syntax? I see it differs from how we write queries in mongo console. For instance, round brackets and double quotes are not allowed here.
You could use simply
#Query(value = "{}", delete = true)
void deleteAgeField();
A solution I've found is simply to set the field to null:
repository.findAll().forEach(
person -> {
person.setAge(null);
repository.save(person);
});
As Mongo is not relational DB, it contains documents not tables. It has json presentation of objects, and when a field=null, it disappears. Maybe my explanation is a bit twisted, please correct me if I'm wrong.
I was trying the new syntax for C# 9 but I failed to use the Data members.
Documents say the syntax is:
public data class Person { string FirstName; string LastName; }
However, I faced the following compile errors:
CS0116: A namespace cannot directly contain members such as fields or methods
IDE1007: The name 'data' does not exist in the current context.
I have tried other syntaxes and they all worked. So, I am sure that I am using C#-9
Update: This question is suggested as a possible answer.
But, it is not my answer and I believe the accepted answer in that link is wrong. Record types and data members are two different things. Data members are a new way to define immutable types, while the record types are Value Objects.
Documents suggest that in the data member classes you just need to define the properties like private fields, so the Person is equal to:
public data class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
}
data classes are now called record. They are the same, immutable objects which behave like value types (exhibiting structural or in other words value based equality).
Regarding your code in OP,
public data class Person { string FirstName; string LastName; }
could be rewritten as
public record Person(string FirstName, string LastName);
The above syntax uses Primary Constructor . If you would like to skip the primary constructor, you could also declare records as
public record Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
}
In either cases, the Properties, FirstName and LastName could be assigned only during initialization (either via Constructor or object initializers) and not after that.
I want to update a MongoDB document containing a dbrf lazy attribute using spring data.
First of all, I load the existing document, I change the attributes I want and after that, I call #Repository save method, but when I check the document in MongoDB the dbrf lazy attribute is null.
I tried to load the attribute before by calling getAttribute, but that doesn't fix the problem.
Someone could help me?
Thanks
I have the Collection below:
#Data
#Document(collection = "calendriers")
public class CalendrierEntity {
#Id
#AutoGenerate(SequanceKey.CALENDRIER)
private Long id;
#NotNul
private String label;
#NotNull
private HorairesEntity horairesEntity;
#DBRef(lazy = true)
#CascadeSave
#Getter(AccessLevel.NONE)
private List<AbsenceEntity> absenceEntities;
}
and the repository bellow :
#Repository
public interface AbsenceRepository extends MongoRepository<CalendrierEntity, Long> {
AbsenceEntity findById(Long enfantId, LocalDate localDate);
}
I have calendrier document with Id 1L and want to update his label.
The calendrier document have allready a list of Absences.
this my code to update the label.
#Transactional
public void updateLabelCalendrier(Long id, String label){
CalendrierEntity calendrier = calenderRepository.findById(1L);
calendrier.setLabel(label);
calenderRepository.save(calendrier);
}
but when i check data in mongodb, i have the new label but my list of absences became null.
I am working with Spring MongoDb.
I create various entities using insert method:
http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/MongoOperations.html#insert-java.lang.Object-
However, all methods return void. I need to return the ObjectId of the inserted document.
What is the best way to get it?
This is pretty interesting and thought I would share. I just figured out the solution for this with the help of BatScream comment above:
You would create an object and insert it into your MongoDB:
Animal animal = new Animal();
animal.setName(name);
animal.setCat(cat);
mongoTemplate.insert(animal);
Your animal class looks like this with getters and settings for all fields:
public class Animal {
#Id
#JsonProperty
private String id;
#JsonProperty
private String name;
#JsonProperty
private String cat;
public String getId() {
return id;
}
}
AFTER you have done the insert under mongoTemplate.insert(animal);, you can actually call the method animal.getId() and it will return back the ObjectId that was created.
I had the same problem with #AlanH that animal.getId() is null. And then I just realized my id field had been set as a final field with a wither method. So of course getId() is null since the id field is immutable and the wither method returns a new object with id.
if this is the case: use animal = template.insert(animal).