In Spring-Data-JPA CrudRepository, what plays for "C"? - spring-data-jpa

CRUD stands for create, read, update, and delete.
So, "C" stands for "create".
What method in CrudRepository plays for "C" then?

It's the CrudRepository#save which can update an existing entity (merge) or persist a newly created one.

Related

EF invalidate a single entity cache

Is there a way to invalidate a single entity such that the next time I ask for it, EF goes to the database for it?
Please note that I am not asking for any Refresh-Reload method. I just need to invalidate the cache such that in case this entity is needed again EF is forced to go to the database (only in case it is needed).
This is my concrete scenario (just if you have a couple of advises to me).
Lets suppose that I have a relationship in which a parent can have many children and the parent has properties that are summarized data about the children, e.g., Order.TotalQuantity.
If I insert, update, delete an OrderItem, I do it using a store procedure (that is mapped to these operations in EF). The aforementioned store procedures insert, update, and delete the OrderItem, but also update certain fields of the parent Order.
How am I supposed to handle this in EF? Do I "invalidate" the Order? If I ask for the Order before a call to SaveChanges the order would return with the wrong TotalQuantity anyway....
Thank you so much!!!

notifying entity call back methods after bulk query in jpa

How does jpa entity call back method work?
are they only notified when doing single jpa operations like em.merge() and em.create() ? or they will be notified if i use em.createQuery().executeUpdate()? (also for namedQueries)
Can i use them instead of real database triggers?
I did not find a direct statement, but from the quote below (JPA 2.1 spec,
Chapter 4.10 Bulk Update and Delete Operations), I understand that in a BULK update or delete, the listeners are not called, because the persistence context won't see the changed entities:
[In a bulk update or bulk delete] The persistence context is not synchronized with the result of the
bulk update or delete.
So the answer is: only in single JPA operations will the listeners be changed.
A listener is not the same as a database trigger: a constraint of JPA listeners is that you should not change relationships or other entities inside them (although some JPA providers may support this).

JPA/EclipseLink: disable cascade for certain merge operations?

I am using EclipseLink 2.3.3. with a data model with about 100 entities. I have a Java class mapped to each database table using annotations.
I have two use cases to implement. One is that a new record enters the system that hits about 60-75 of the tables. For this case, I want merge and persist to cascade, so that I can just merge the top level object and have that cascade to all related entities.
Another use case is that I need to insert a collection of individual objects, often one from each of a bunch of different tables. In this case I don't want the cascading merge, because I need to have control over the insertions. If I have cascade enabled, merging the first object might or might not merge the other objects, depending on if or how they are related, so I'd rather explicitly merge each of them.
So essentially, I want cascading merge and persist in one situation, but not another. So if I include the cascade annotations in the mapped classes, I need to selectively disable the cascading for certain operations; or, if I turn off cascading in the mapped classes, I would like to enable cascading for certain operations.
So far I am not finding any way to selectively turn on or off cascading for a particular operation. There is a CascadePolicy class but that seems to only be used with queries. There are dynamic entities, and I was thinking perhaps I could use that to do something like create a dynamic entity from an existing entity and turn off the cascading behavior on that entity's relationships and somehow use that for the merge, but I have not been able to find the right API for that.
So I am wondering if there is a better answer somewhere that I'm overlooking? Thanks for any information.
I'm not certain about what level of control you are after, especially in the case that you mention you want to insert individual objects. From the sounds of it, cascade merge is exactly what you want for your Entity object tree in the first case for use with the EntityManager.merge. Merge called on an entity will check if it is new or not, and update or insert as appropriate. Marking relationships as cascade merge will allow finding new objects and having them inserted.
The second case though where you want to handle individual insertions, why not exclude the cascade persist option on mappings and just call EntityManager.persist on the objects you want to insert? Persist then will not cascade, so only the entity you call em.persist on will get inserted. Relationships will be used just to set the foreignkey values - though you might want to leave them nulled out and set them later as part of larger merge calls. Both sides of bidirectional relationships need to be maintained, and if the other side is exists and doesn't get merged, its relationship changes are not stored.
If that isn't what you want, EclipseLink has native API on the UnitOfWork (the EntityManager essentially wraps a UnitOfWork for transactional work) that allows you to specify the merge policy. See mergeClone, deepMergeClone and shallowMergeClone on UnitOfWork, which essentially use CASCADE_ALL_PARTS, CASCADE_PRIVATE_PARTS and NO_CASCADE respectively as the merge policies, while the JPA merges use CASCADE_BY_MAPPING.

Update A Collection With Entity Framework

I have a detached set of client objects that I'd like to update (I know they already exist in the db by primary key). Now I want to update them to the database. Knowing I need to query them first, I do so and now have to basically take the properties from the deattached objects and apply them to the attached objects. I finally call save changes. Is there a slick way to apply these properties from the detached collection to the attached one?
NOTE: The detached objects don't have the primary keys in them but I do have enough information to link with via a comparer class.
You don't need to do what you're doing. You can just call the Attach method on your ObjectContext to tell it that you want to work with your detatched objects. Then just call SaveChanges to update the database with your changed objects.

Entity Framework and soft deleted records

I'm developing WCF service which is using entity framework as data source. Almost all is ok except problem with deleted records. In our database we're using soft delete (mark record attribute IsDeleted = true). My question how to exclude soft deleted records from entity set?
For example, entity "A" has entity set "Bs" (FK to table "B").
How to make that "Bs" entity set only contains from records which is not deleted?
Thank you
I have written a post about this topic, hope it helps.
http://blog.jorgef.net/2010/12/ef-soft-delete.html
One way would be to use a defining query. But we typically handle this in the Repository, since we actually do want to materialize "soft deleted" entities in rare cases.
You could map you EF entities to views instead of tables
CREATE VIEW vw_Currency AS
SELECT
*
FROM
Currency c
WHERE
c.IsAKDeleted=0
I have worked on a system which used this approach but it was not based on EF. I have not tried it with EF