Updated Datastore without using Key#id by using java - java

I have the sample code below which updates the records based on the key id but i need a sample script which updates the DS values based on other columns rather than using key i also tried using query but couldn't find a solution.
boolean markDone(long id) {
Transaction transaction = datastore.newTransaction();
try {
Entity task = transaction.get(keyFactory.newKey(id));
if (task != null) {
transaction.put(Entity.newBuilder(task).set("done", true).build());
}
transaction.commit();
return task != null;
} finally {
if (transaction.isActive()) {
transaction.rollback();
}
}

This page has a number of query examples.
If you want the query to be part of a transaction, it will need to be an ancestor query.

Related

nodejs mongodb and android studio slow fetch speed

I have a system which, pulls data from my server and stores it to a mobile SQL database via android studio. It works but it is painful slow like 30mins. I have around 86000 records in my database and want to pull all of them out of the server. What is the best way to do this?
Presently I get the count from the server and then query the server database until i find each ID and then send that result back to my mobile app.
app.post("/get_data", function(req, res)
{
var Id_request = req.body.Id_request;//The requested ID
var query = {Val_String : Id_request};//Query value
//console.log(query);
//Data.find({}, function(err, result) {//Finds all data
Data.findOne(query, function(err, result) {//Finds all data
if (err) {
//res.status(400).send(err);
console.log("Sending error");
res.status(200).send();
} else {
return res.json(result);
}
});
});
I use a recersive function in my pull request for each ID
private void call_method()
{
HashMap<String, String> map = new HashMap<>();
map.put("Id_request", Integer.toString(data_pointer));//The ID value
Call<Fetch_result> call = retrofitInterface.executeGet_data(map);//Run the post
call.enqueue(new Callback<Fetch_result>() {
//call.enqueue(new Callback<Fetch_result>() {
#Override
public void onResponse(Call<Fetch_result> call, Response<Fetch_result> response) {
if (response.code() == 200)//Successful login
{
D1= response.body().getT1_String();
D2= response.body().getT2_String();
data_pointer = data_pointer + 1;
boolean result = BLE_DB.addData_Downloaded(D1,D2);//Add data
if(data_pointer<= Total_entries) {//Call method again
call_method();//Recursive here
}else if (data_pointer > Total_entries){
Utils.toast(getApplicationContext(),"All data received");
}
} else if (response.code() == 404) {
Utils.toast(getApplicationContext(), "Get data fail");
}
}
#Override
public void onFailure(Call<Fetch_result> call, Throwable t) {
Utils.toast(getApplicationContext(), "Get data error");
}
});
}
How Can I speed this up or do it differently to speed it up?
Try to fetch as much data as possible at once ( limit the amount of queries you do ). It is hard to tell you how since I don't know your monogDB collection.
Try to do this with as little requests as possible. If you can return all the fetched data at once, this will save you some time.
JSON may be very slow when doing it on 86000 documents
Consider caching the data for future users
Right now i suspect what is limiting you is the fact that you are doing 86000 queries to the db... If you can get the entire mongoDB collection it may be a bit faster( look at notes )
Notes:
https://docs.mongodb.com/manual/reference/method/db.collection.find/#db-collection-find ( omit the query parameter will result in fetching the entire collection )

Spring JPA Crud Repository save() not updating entity

In my code i am trying to update my entity but data is not getting saved in db.
checked logs there is no exception as well.
Also tried #Transaction annotation but it doesn't work.
Method for save call
public void uploadBill(Long quoteId, MultipartFile multiPartFile) {
try {
ServiceQuote serviceQuote = serviceQuoteRepository.findOne(quoteId);
String extension = "";
if (multiPartFile.getOriginalFilename().lastIndexOf(".") != -1) {
extension = multiPartFile.getOriginalFilename().substring(
multiPartFile.getOriginalFilename().lastIndexOf("."),
multiPartFile.getOriginalFilename().length());
}
String filename = SERVICING_BILL_FILE_PREFIX
+ serviceQuote.getReferenceNo() + extension;
filename = AmazonS3Util.uploadBill(multiPartFile, filename);
serviceQuote.setBillPath(filename);
serviceQuoteRepository.save(serviceQuote);
} catch (Exception e) {
e.printStackTrace();
}
}
Here serviceQuoteRepository is JPA crud repositorty
#Repository
public interface ServiceQuoteRepository extends CrudRepository<ServiceQuote, Long> {}
Please suggest what could be possible fix.
There was no issue related to crud repository. Actually after upload method another service was updating the entity with null bill path at the same time so I was seeing the null column always. Figured out after debugging the issue for few hours.

Parse: How can I get Relation from a local data store (...fromLocalDatastore())?

I am developing an app to serve as a learning and I'm using Parse (parse.com) as a data source.
I am conducting download all objects of my classes in the parse and saving to a local store that has Parse. The following code snippet that performs one of downloads:
public void noticia_getOrUpdate(boolean isUpdate) throws ParseException {
ParseQuery<Noticia> query = new ParseQuery(Noticia.class);
query.orderByDescending("createdAt");
List<Noticia> lNoticias = null;
try {
if (isUpdate) {
lNoticias = query.whereGreaterThan("updatedAt", this.sPref.ultimaAtualizacao_noticia()).find();
if (!lNoticias.isEmpty())
ParseObject.pinAllInBackground(lNoticias);
} else {
query.whereEqualTo("ativo", true);
lNoticias = query.find();
for (Noticia noticia : lNoticias) {
if (noticia.getUpdatedAt().getTime() > this.sPref.ultimaAtualizacao_noticia().getTime())
this.sPref.atualiza_noticia(noticia.getUpdatedAt());
}
ParseObject.pinAllInBackground(lNoticias);
this.sPref.atualiza_isUpdate(true);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
The problem is I'm downloading all my classes, one is the File type, is a file that works as an image for my news ("Noticia"). I can download and store all on-site data storage, but can not recover using the following code:
public static byte[] NoticiaMidiaRelation(Noticia noticia) {
try {
ParseRelation<Midia> relation = noticia.getImagem();
Midia midia = relation.getQuery().fromLocalDatastore.whereEqualTo("ativo", true).getFirst();
if (midia != null && midia.getFileData() != null)
return midia.getFileData();
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
If retreat the "fromLocalDatastore" the query, he seeks the server and brings the file correctly, but do not want to pursue it again, because as said, already have the same image stored in the local data store.
Another way to do would be to get the relationship the media Id, after that perform a search for comparing ObjectId within the local store, but I think there's no way the property "parent". But if any, can be used as a solution.
You can't according to the documentation:
By default, when fetching an obj
ect, related ParseObjects are not fetched
There are work arounds but they might not be practical because by what I understand, you only have one image in your relation per ParseObject in your "Notica" class as you call getFirst(). In this case, using a Pointer would be a better decision but those aren't fetched by default either BUT they are cached to the LocalDatastore. You'll need to add the following code to your query to fetch the Pointer object.
query.include("your_column_key");

How to bind JTable with database using Hibernate

Hi,
I have database in MySql and want to get my table to be bind with Swing JTable.
Now my DAO class retrieves data from my table and stores it into java.util.List.
What approaches I could use to bind db table with JTable?
As you have data fetched from Database wrapped by DAO, using DAO put those information in relevant row/column of your JTable.
Here are SO Question and answer for your requirement. Hope they will help you.
Populate JTable Using List
Java GUI aplication, load data to Jtable from a list<objects>
How to add data to JTable created in design mode?
Other Resources.
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
Session sesion = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = sesion.beginTransaction();
List today = sesion.createQuery("FROM class WHERE something").list();
for (Iterator iterator = today.iterator(); iterator.hasNext();){
Salidas Sal = (Salidas) iterator.next();
tablemodel.addRow(new Object[]{
//`enter code here`columns
Sal.getId(),
Sal.getUsuarios().getNombre().toString(),
Sal.getCantidadPrestada(),
Sal.getCantidadPedida(),
Sal.getFechaSalida()});
}
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
sesion.close();
}

Is it possible to detach Hibernate entity, so that changes to object are not automatically saved to database?

I have Hibernate entity that I have to convert to JSON, and I have to translate some values in entity, but when I translate values, these values are instantly saved to database, but I don't want to save these changes to database. Is there any workaround for this problem?
You can detach an entity by calling Session.evict().
Other options are create a defensive copy of your entity before translation of values, or use a DTO instead of the entity in that code. I think these options are more elegant since they don't couple conversion to JSON and persistence layer.
I am also converting hibernate entities to JSON.
The bad thing when you close the session you cannot lazy load objects. For this reason you can use
hSession.setDefaultReadOnly(true);
and close the session after when you're done with the JSON.
You can also avoid that your entities are attached to the Hibernate Session by using a StatelessSession:
StatelessSession session = sessionFactory.openStatelessSession();
instead of
Session session = sessionFactory.getCurrentSession();
Note that you must take care yourself of closing the StatelessSession, unlike the regular Hibernate Session:
session.close(); // do this after you are done with the session
Another difference compared to the regular Session is that a StatelessSession can not fetch collections. I see it's main purpose for data-fetching-only SQLQuery stuff.
You can read more about the different session types here:
http://www.interviewadda.com/difference-between-getcurrentsession-opensession-and-openstatelesssession/
Close the Session. That will detach your entity for you, and no modifications will be flushed. If that's not possible, look into disabling autoFlush...but that's a whole other can of worms. The easiest is to close the Session and be done with it!
public static <E> E deepClone(E e) {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo;
try {
oo = new ObjectOutputStream(bo);
oo.writeObject(e);
} catch (IOException ex) {
ex.printStackTrace();
}
ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi;
try {
oi = new ObjectInputStream(bi);
return (E) (oi.readObject());
} catch (IOException ex) {
ex.printStackTrace();
return null;
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
return null;
}
}
first: deepClone the session pojo
second: alter fields
then: do whatever you want to do
In my case I just flushed and cleared the session.
session.flush();
session.clear();

Categories