For example I have a model:
#Document(collection = "events")
public class Event {
#Id
private String id;
private String value1;
private Spring value2;
}
And some code in the service class:
Event event = eventRepository.findOne("1");
event.setValue1("newValue1");
eventRepository.save(event);
Does Spring Data update all fields for the document or only changed fields?
In this case what's about field2, will it be updated with the old value?
How can I turn on logs to check what exactly spring data do?
logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG
This one turn on logs for the query only, not for the update:
016-10-10 09:39:25.938 DEBUG 11417 --- [ restartedMain] o.s.data.mongodb.core.MongoTemplate : findOne using query: { "_id" : "1"} in db.collection: t87.events
2016-10-10 09:39:25.965 DEBUG 11417 --- [ restartedMain] o.s.data.mongodb.core.MongoTemplate : Saving DBObject containing fields: [_class, _id, value1, value2]
MongoRepository.save() calls MongoDB db.collection.save behind the scenes . Depending on the value of id it may insert / upsert / replace the entire document.
So, in answer to your question:
Does Spring Data update all fields for the document or only changed fields? In this case what's about field2, will it be updated with the old value?
All fields are updated (upserted) including field-2 with the old value.
In order to set only field-1 - which is more efficient - you will have to use MongoTemplate in conjunction with an Update and a $set operator.
In order to see what's going on, you better look at the MongoDB log to see how update operations being handled. See setProfilingLevel on how to do capture each write operation; you will probably have to record each operation by setting the profiling level to 2. Don't forget to turn it off afterwards.
Related
I have a Pojo with an attribute as
Class A{
#Id
#Field("item_id")
private String itemId;
}
When i try to update a document in MongoDB collection based on the itemId as below, it worked well and able to see from mongo ops logs that the query was transformed as "_id in itemIds "
Query query = new Query(Criteria.where("itemId").in(itemIds));
Update update = new Update();
update.set("field2", "abd");
mongoTemplate.updateMulti(query, update, A.class)
When i upgraded to spring-data-mongodb-2.1.5.RELEASE, the query i saw in the mongo logs was "item_id in itemIds". Since item_id is not a field and no index for that field in the collection, the query took forever to complete.
Any help to understand why the spring-data library is building the query as _id in older version and using the field as it is in newer version?
After a 2 minute search on the Spring documentation (https://docs.spring.io/spring-data/mongodb/docs/1.3.3.RELEASE/reference/html/mapping-chapter.html):
The following outlines what field will be mapped to the '_id' document field:
A field annotated with #Id (org.springframework.data.annotation.Id) will be mapped to the '_id' field.
A field without an annotation but named id will be mapped to the '_id' field.
Did you try that already?
How to retrieve data from elasticsearch containing special characters like . / - ... using spring data ?
I have document defined like this:
#Document(indexName = "audit-2018.135", type = "audit")
public class Trace {
#Id
private String id;
#Field(type = FieldType.Text)
private String uri;
// setters & getters
}
I need to retrieve data from elasticSearch by field uri.
Here's an example how data looks:
martin-int-vip.vs.cz:5080/kib/api/runtime/case/CASE0000000000324223
When using kibana I can use:
uri: "martin-int-vip.vs.cz:5080/kib"
and I get back the record above which contains desired substring.
Now I need to achieve the same from java however in spring data it's not working as expected.
I have elasticSearchRepository defined like this:
public interface TraceRepository extends ElasticsearchRepository<Trace, String> {
List<Trace> findByUriContaining(String uri);
}
when I call method findByUriContaining with parameter uri as:
martin-int-vip.vs.cz:5080\/kib
or even this
martin-int
I get back 0 results. When I send "kib" as parameter it returns correctly all records containing word "kib" however it's not working with special characters like . / - etc. How should I query my elasticSearch from java to get all records which contains my desired substring ? Thanks
I found out that by default elasticSearch analyzer tokenize your query. Instead of "martin-int-vip.vs.cz:5080/kib" it looks for a field which contains martin and int and vip...
In order to query these kind of fields you need to change behaviour of analyzer or you can use elasticSearchTemplate and Criteria to build more flexible queries.
I have the following entities:
Area
Listing
They are both many-to-many:
An area can have many listings
A listing can have many areas
Both Area and Listing have other fields like name, domain, etc.
I'm using Spring Web RestController as a way to update the entities.
For example:
#PutMapping("/{id}")
public Area update(#PathVariable Long id, #RequestBody Area update) {
return areaRepository.save(update);
}
However, as an Area can have many thousands of Listings, it's not practical to pass them all in the update request when I just want to update the Area name and other basic fields in my web application.
For example, the update json in the http request would be:
{
"id" : 69,
"name" : "San Francisco",
"domain" : "foo",
...
}
When serialised, the area instance above will have a listings field equal to null (obviously) and then when saved, all association are remove from this Area.
I'm thinking that I could do a select-then-update set of operations and only update the values necessary but that is cumbersome - especially when there are many dozens of non-association fields.
The question would be: how can I try to keep to the above code and http request and not remove all of the existing Listing associations when saving the input area? Is this possible? I want to update the name and other basic fields but not the association fields.
You can use the BeanUtilBean(org.apache.commons.beanutils.BeanUtilsBean).
Step 1: Create custom beanutilbean class.
#Component
public class CustomBeanUtilsBean extends BeanUtilsBean {
#Override
public void copyProperty(Object dest, String name, Object value)
throws IllegalAccessException, InvocationTargetException {
if(value==null)return;
super.copyProperty(dest, name, value);
}
}
Step 2: In your controller while updating. first get the Area from database & use copyProperties method as shown in below.
#PutMapping("/{id}")
public Area update(#PathVariable Long id, #RequestBody Area update) {
Area areaDB = areaRepository.findOne(update.getId());
customBeanUtilsBean.copyProperties(areaDB,update);//it will set not null field values of update to areaDB.
return areaRepository.save(areaDB);
}
Hope this will helps you..:)
Since you are using spring-data-jpa repository you can write a new method that takes the changed values of Area object with #Modifying and #Query annotations on it.
In the #Query you specify the HQL query with update statement as in here
Does Spring Data treats mongo nested "id" attributes differently? I explain my problem: I have collection matches with the following structure
"teams": [
{
"id" : "5601",
"name" : "FC Basel"
},
... // more
]
When I want to retrieve all the matches which has team id 5601 I execute the following query
db.matches.find({ "teams.id" : "5601"})
Which works perfectly and returns some objects.
When I make a method
public List<MatchMongo> findByTeams_id(String id);
on my MatchRepository interface I get 0 results back while there are.
Logs shows
Created query Query: { "teams.id" : "5601"}, Fields: null, Sort: null
find using query: { "teams.id" : "5601"} fields: null for class: class
MatchMongo in collection: matches
So the query he makes seems to be the right one... :S
Trying with other fields (referee.name for ex.) works.
I even tried with the #Query annotation, but can't get it to work
Is there another solution? Is this a bug or am I doing something wrong?
Oh found the solution:
MatchMongo had List<TeamMongo> teams; on whereI had
#Id
private String id;
#Field(value = "id")
private String teamIdAttr;
So the method should be called
public List<MatchMongo> findByTeams_teamIdAttr(String id);
Never thought the method name should reflect objects attributes instead of collection structure
Thanks #martin-baumgartner your comment helped to solve this :)
I'm retrieving a list of rows with selected fields from Oracle database using Hibernate. The retrieval is made by the following method in one of the DAOs in my application.
#SuppressWarnings("unchecked")
public List<Object[]> getOldFileName(String []ids)
{
int len=ids.length;
Long longType[]=new Long[len];
for(int i=0;i<len;i++)
{
longType[i]=Long.valueOf(ids[i]);
}
return sessionFactory.getCurrentSession().createQuery("select catId, catImage from Category where catId in(:id)").setParameterList("id", longType).list();
}
Here I'm fetching two fields categoryId and categoryImage as listed in the given HQL query based on the catId supplied as an array of String[] as a method parameter via Spring. It works fine and there is no question about it.
But regarding my requirements, retrieving catId again is completely unnecessary and I would like to remove catId from the list of fields in the query something simply like the following.
select catImage from Category where catId in(:id)
If I try to execute this query then the following call to the preceding method inside the Spring controller class,
String temp[]=request.getParameter("setDel").split(",");
List<Object[]> oldFileNames = categoryService.getOldFileName(temp);
//Invokes the preceding method in DAO.
causes the following exception to be thrown,
java.lang.ClassCastException: java.lang.String cannot be cast to
[Ljava.lang.Object;
The exception message indicates that java.lang.String cannot be cast to an array of Objects - Object[]. It appears that the HQL statement attempts to get only a single value of type String instead of retrieving List<Object[]>.
I just want to delete files which are stored in a directory after retrieving their names from the database and mentioning of catId in the list of fields in HQL is completely unnecessary.
Why do I need to add catId in the list of fields of the HQL statement in this scenario?
The return type for the list should be List. When only one column comes back hibernate does not put the result into and Object[]