Spring Data Neo4j Repository.save works incorrectly if it performs update - spring

I tried to perform following actions via repository (SDN 4.0.0.RC2):
I save entity with an property with null value
I save entity again (i.e update it), but now property has a value
In this case updating is not performed. Value still is null. Is it a bug?

Related

Update only "changed values" for an existing entity

I am creating a rest API to update my entity with the new changes, but my entity is having like 20 attributs and i don't think it's a good way to just put multiple sets and "if" conditions to not set old values with NULL.
I want to know if there is a way in spring that can update the whole entity without changing the old unchanged values to null instead of putting a lot of Setters (~20 in my case).

How to delete from db?

I am using Spring framework, the following code was use to delete a record, but it failed to worked, why?
List<PostAttachment> postAttachments = postAttachmentRepo.findByObjectKey(key, Sort.by(Sort.Direction.DESC, "createdAt"));
postAttachments.remove(0);
findByObjectKey is returning collection to you and
postAttachments.remove(0);
Will remove the record from the collection only and not from the database. So, to remove data from dabase either
You need to call entityManager.remove(postAttachments.get(0)), for this entity needs to be managed and transaction.
Use spring repository method, postAttachmentRepo.delete(postAttachments.get(0)) in this case, spring manags everything (transaction as well entity is managed).

Spring hibernateTemplete get not getting updated record value

When process is in execution a stored proc is called using Java standard callable statement, which updates the record in db.
Later in flow, using hibernatetemplate get method(passing entity Id object), instance of record is fetched.
The problem is this instance has old value for record columns, before update by stored proc happened.
Its probably happening because hibernateTemplalte was called earlier for same table to update the same record. But even then, if get method is called shouldn't it make a fresh call to db to get updated record.
I tried using flush, but still issue remains.
Any inputs would be appreciated.

Double instances in database after using EntityManager.merge() in Transient Method

I am new with Spring, my application, developed with Spring Roo has a Cron that every day download some files and update a database.
The update is done, after downloading and parsing the files, using merge(),
an Entity class Dataset has a list called resources, after the download I do:
dataset.setResources(resources);
dataset.merge();
and dataset.merge() does the following:
#Transactional
public Dataset Dataset.merge() {
if (this.entityManager == null) this.entityManager = entityManager();
Dataset merged = this.entityManager.merge(this);
this.entityManager.flush();
return merged;
}
I expect that doing dataset.setResources(resources); I would overwrite the filed resources, and so even the database entry would be overwritten.
But I get double entries in the database: every resource appear twice, with different IDs (incremental).
How can I succed in let my application doing updates and not insert? A naive solution would be delete manually the old resource and then call merge(); is this the way or is there some more smart solution?
This situation occurs when you use Hibernate as persistence engine and your entities have version field.
Normally the ID field is what we need for merging a detached object with its persistent state in the database, but Hibernate takes the version field in account and if you don't set it (it is null) Hibernate discards the value of ID field and creates a new object with new ID.
To know if you are affected by this strange feature of Hibernate, set a value in the version field, if an Exception is thrown you got it. In that case the best way to solve it is the data to parse contain the right value of version. Another ways are to disable version checking (see Hibernate ref guide to know about it) or load persistent state before merging.

Get original object property stored in DB with Hibernate in same transaction

I opened hibernate transaction and read the object. I changed some properties of object without store. I want to get the original properties stored in DB but with
Criteria cr = new Criteria(...);
cr.add(Restrictions.eq("id", id));
cr.setProjection(Projections.property("someProperty"));
cr.uniqueResult();
or reload whole object with getSession().get(id). But as a result I got changed properties and if I reload whole object I got the same instance of changed object. How to get original object properties stored in DB with same transaction, changed object must remain with changed properties.
And how to do it with Spring transaction annotations?
You can use session.refresh() method. See documentation: http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#objectstate-loading
But this will overwrite any changes. You can clone the object before refresh, i think.

Resources