spring JPA - delete by marking the entry - spring

I would like to know if Spring or JPA has something implemented to take care of this problem.
In Entity Framework there is something similar with this.
When I want to delete an object, I don't want to remove it from the database. Instead I want to mark it as deleted by using a datetime field.

Related

How to refresh Spring JPA when data is updated from querydsl directly?

I'm using spring data jpa and querydsl. I don't want to update directly through JPA because I have to submit a complete entity, which introduces an unnecessary query. So, I use querydsl instead:
val qRecord = QCoupon.coupon
jpaQueryFactory.update(qRecord)
.set(qRecord.useState, Coupon.STATE_ORDERED)
.set(qRecord.useTime, useTime)
.where(qRecord.id.eq(recordId))
.execute()
But the query after that in the same transcation cannot get the latest data, I think it has something to do with the first-level cache.
Adding #Modifying doesn't work because I didn't use #Query, the first annotation will be ignored.
I know EntityManager.clear() can fix that. But it looks too heavy, I'm not sure this is a good idea.
This post explained the cause of the problem very well but not my case -- I have rejected both two answers.
#Modifying is ignored.
EntityManager.clear() looks too heavy, and find() also need an entity.
Thanks #Jan-WillemGmeligMeyling 's idea. Because he didn't reply to me, I wrote it in my answer.
Have you tried refreshing the entity?
entityManager.refresh(entityManager.find(Coupon.class, recordId))
em.find() will not look up the entire table, that's what I want.

Is it necessary to use Entity annotation for Select query records from Database

I have spring boot application with JPA and MySQL. The table is already created and data are present. From the spring boot application, we need to create two API - get a particular record using id and get all the records. I created a model class but confused with usage of #Entity. Because I'm not going to insert/delete a record from the database. I want to use only for select the record.
For Select query (findBy), do we need to use #Entity annotation at the top of model class?
Yes, you would need to use #Entity to mark your model class, this tells JPA that your class is used for mapping to and from a database table. If you want to make sure that you don't accidentally overwrite the data, you could implement a check via #EntityListener, as described in this answer.

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).

JPA save/update only if modifications

I have a problem, with the Spring JPA, I did not find anything about saving entity only if there are modification. And I am not talking about saving certain fields.
After the database data is loaded by the JPA entity manager, how can I verify, before I save the entity, if there are modification/changes to the fields. Without taking each field and verify it with:
if (field != ...)
Class i = repo.findByExternalId(externalId);
...
if (modifications)
repo.save(i);
else
//don't save
I tried with dynamic-update=true but it doesn't work.
If you have some information about what to search on the web... that would be helpful as well.
Thanks.
Just don't call save() but put everything in one transaction.
JPA will do the checking for you and save changes if necessary.

when I try to add a second field set to an entity it doesn't show in the screens

I have six entities and I want to create two field sets from one to two others. One field set is ONE_TO_MANY and I used field set in the ONE side to build the relation and the other is a MANY_TO_MANY and I tried the simplest example from Northwind and that didn't work, and I tried adding --joinTable, --joinColumns, etc. and that didn't work either. Only the ONE_TO_MANY works and only one of those.
Is there a good example with Roo 2.0.0.RC1 that works? or a manual work around?
Remember that Spring Roo just generates and maintains the code that he knows. In this case, you are trying to create a really specific structure, so I recommend you to create it manually. Spring Roo doesnt't have any problem with custom code.
You could check the following repository where exists a lot of examples about relation management managed by Spring Roo or manually.
https://github.com/DISID/disid-proofs
Hope it helps,

Resources