This question already has an answer here:
Azure DocumentDB Read Document Resource Not Found
(1 answer)
Closed 2 years ago.
I'm able to delete the document in cosmosdb using the below code.
Optional<Place> place = repository.findById(id);
repository.delete(place.get());
But, if I use the method repository.deleteById(id) from the CrudRepository it's returning me the below error.
com.azure.data.cosmos.NotFoundException: ["Resource Not Found"]
You should pass the partition key to the deleteById method. Use the following method from CosmosRepository:
void deleteById(ID id, PartitionKey partitionKey);
yourRepository.deleteById(id,new PartitionKey(<parition key value>));
The following works because the the argument has the ID and Partition key values.
repository.delete(place.get());
Additionally, Use the following method from CosmosRepository for find a document by ID.
Optional<T> findById(ID id, PartitionKey partitionKey);
If your IDs are not unique across partitions then you might get inconsistent results.
Related
I've put a derived query on a CrudRepository<Customer, Long> that should delete all entities with a given businessId (which is not the primary key but just another, non-unique column), of which there can be many.
In my SpringDataJdbcTest test, I first save 2 customers with the same businessId.
Then, I want to call the following method on the CrudRepository:
fun deleteAllByBusinessId(businessId: Long)
But it gives me:
org.springframework.dao.IncorrectResultSizeDataAccessException: Incorrect result size: expected 1, actual 2
at org.springframework.dao.support.DataAccessUtils.nullableSingleResult(DataAccessUtils.java:100)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:244)
It seems Spring Data Jdbc first wants to determine if there's a unique entity with that business Id, but of course, there are 2.
Could it be that support for such a derived delete query doesn't work correctly? I use Spring Data JDBC 2.3.5.
You are right, derived delete queries don't work yet. See https://github.com/spring-projects/spring-data-relational/issues/771
I am working on a spring application.
We have a specific requirement where when we get a specific event, we want to look it up in the DB. If we find the record in the DB, then we delete it from DB, create another event using the details and trigger it.
Now my concern is:
I do not want to use two different calls, one to find the record and another to
delete the record.
I am looking for a way where we can delete the record using a custom
query and simultaneously fetch the deleted record.
This saves two differnet calls to DB, one for fetch and another for delete.
What I found on the internet so far:
We can use the custom query for deletion using the annotation called #Modifying. But this does not allow us to return the object as a whole. You can only return void or int from the methods that are annotated using #Modifying.
We have removeBy or deleteBy named queries provided by spring. but this also returns int only and not the complete record object that is being deleted.
I am specifically looking for something like:
#Transactional
FulfilmentAcknowledgement deleteByEntityIdAndItemIdAndFulfilmentIdAndType(#Param(value = "entityId") String entityId, #Param(value = "itemId") String itemId,
#Param(value = "fulfilmentId") Long fulfilmentId, #Param(value = "type") String type);
Is it possible to get the deleted record from DB and make the above call work?
I could not find a way to retrieve the actual object being deleted either by custom #Query or by named queries. The only method that returns the object being deleted is deleteById or removeById, but for that, we need the primary key of the record that is being deleted. It is not always possible to have that primary key with us.
So far, the best way that I found to do this was:
Fetch the record from DB using the custom query.
Delete the record from DB by calling deleteById. Although, you can now delete it using any method since we would not be requiring the object being returned by deleteById. I still chose deleteById because my DB is indexed on the primary key and it is faster to delete it using that.
We can use reactor or executor service to run the processes asynchronously and parallelly.
This question already has answers here:
Query relationship Eloquent
(3 answers)
Closed 1 year ago.
In laravel with Model we can get all Table details
Locations::all();
But I want to get with Foreign key data with the all data.
like there is
id , name , location_id
so it has to get
location_id name
So i get it like
id , name, location_id with location_id_Name
Is it possible to get that result by using laravel basic model. Or I have to change some.
There is answer How to retrieve full data of record by its foreign key in laravel? But then I have to create a new function to add more component of each array. Which I don't want as I suspect there should be better way.
Solution which i found
Location::with('locationid')->get();
where loctionid declared in the model Location
public function locationid(){
return $this->belongsTo(Location::class, 'location_id');
}
So now location load with parent. And also it is very much efficient. If anyone know what will better solution please just comment or answer it.
Best answer on stackoverflow look like https://stackoverflow.com/a/66187247/12657567
See the official documentation of Laravel framework. You may need Eager Loading.
https://laravel.com/docs/8.x/eloquent-relationships#eager-loading
I would like to make a second request with the id obtained in the previous request.
I have 3 tables:
organization
users
follow_organization (relationship table)
I would like to obtain the number of occurrences in the follow_organization table by passing the id of the organization previously obtained.
$organization = DB::table('organizations')->where('name', $name)->first();
$followers = DB::table('follow_organizations')->where('organization_id', $organization)->count();
Unfortunately, I get the following error message:
Object of class stdClass could not be converted to string.
I understood the problem, I pass a table while the query builder waits for a string.
But I can't find the solution to this problem despite the tons of answers I've read on the internet.
Currently you are passing the whole organization object into the where clause, but you only need the id.
To access the id property you can use $organization->id
DB::table('follow_organizations')->where('organization_id', $organization->id)->count();
Setting and Getting Property Values
i am using mongodb in my spring boot application and i am using mongo repository interface to get data from the database this is the way i am getting the data .
School sch=repository.findOne("id");
this will give me the school object then i can get all the data from there but my question is will it affect my application's performance if i get the whole object everytime i need some data from that object even if i need some fields . if so what will be the method to do that i searched and i see that using Query annotiation i can limit the fields but even then it give whole object it put all the other fields null and data is only at fields which i specify . any guidence will be helpfull .
You can use a projection interface to retrieve a subset of attributes.
Spring Data Mongo DB
interface NamesOnly {
String getName();
}
interface SchoolRepository extends Repository<School, UUID> {
NamesOnly findOneById(String id);
}
So after reading the documentation and reading other options this is the only solution i find where i can search the result using id and get only field i want in the result since i am only searching using the id so i have to overload the findbyId and since projection interface only change the return type i can not use that so here is what i did .
#Query(value="{_id:?0}",fields="{?1:1,?2:1}")
List<School> findById(String schoolId, String fieldOne,String fieldTwo);
here ?0 is a place holder for schoolId and ?1 and ?2 are the placeholder for the fields name so now i can create these overloaded method which i can use to any no of fields the output is list of school since i am using id which is primary key so its just one school object list so i can just do get(0) to get my school object and it will have all the other fields as null this is the best i could find please share your thought to improve it i would love to hear other solutions .