JPA Cascade ALL to skip unmodified entities - jpa

When we update a root entity with CASCADE.ALL option all the child objects also gets updated in JPA.
If I know before hand that a pariticular child entity is not modifed , is there a provision to denote in the entity that it is not modified , so that unnecessary update query is not fired.
We can very well exclude the particular child from the parent collection , so that JPA will not update anything .
I would like to know if there is any flag to denote at Entity level to specifiy that no properties are modified

Related

FOSElasticaBundle: ManyToMany relationship

I'm trying to use FOSElasticaBundle on my symfony 4 project and I have some problems.
I have an entity, "Users", with the next annotation in the property "segments":
* #var Collection $segments
* #ORM\ManyToMany(targetEntity="App\Entity\Segment", mappedBy="users", cascade={"persist", "remove"})
Which is the correct form to do the mapping in the fos_elastica.yaml file?
I would suggest you to think in your use cases and then decide which related entities are you going to use for search purposes. Once decided, you can do two things:
Option A, use a nested field. Is simpler but, if you update a child (related) entity, the Elasticsearch index will not be updated. Yo must ensure that this happens within your code. For example, each time the child entity gets updated, you set a timestamp in the parent entotity, so FosElasticaBundle catch's that chahge and runs the entity serializer so the child entity becomes serialized.
Option B, use a child parent relation in Elasticsearch. In this case FosElasticaBundle will track your entity changes correctly for each Doctrine entity, so you don't need to manage it in your code.
In both cases, you many to many relationship becomes in two one to many relations. In option A this is done by the serializers that embeds the child document in the parent one. In the second case it is tranlated to a parent child relationship.

Remove entities and remove relationships on Entity framework

I´m using EF databaseFrist and creating a model, now having a problem trying to understand how to delete an entity or a relationship.
Lest say a have and Table "A" and a Table "B", and a "a_b" table that relates A and B by id (many to many), a_b only has id_A and id_B, so there is no entity a_b created on the model. A has a list<B> and B has a list<A>, i need to know how can i perform the next functions:
-Remove all B entities related to A, it means delete the rows of B.
-Remove only the relationships of A to B, so all entities still exist on DB but they are dissociated.
-Delete A and remove all B related as well (remove entities from DB).
-Delete A and preserve all B entities.
-How will it change if a_b has any other property so it becomes an entity
thanks for your time.
pd: I´m using Lambda syntax.
You can tell EF on DB First model creation to expose all primary and foreign-key properties in the entities. What it means to you is that you can query separately by querying for child objects to a parent by using the foreign-key property of the child object linked to the parent object's primary key. You can delete each child object to the parent by the child's primary-key property value to remove the relationship between parent and child. You can delete the parent if no child object is linked to the parent. That's if one is doing things normally in a deletion process using EF where one takes complete control of the deletion process.
So, you can kind of do something like that.

How to use DBContext.Add/Attach (using EF CodeFirst 4.1) with nested opbjects

Problem: When adding an object "Order" to my dbcontext, all nested objects of the order gets "readded" to the database, though the nested objects is static data and only a reference shoudl be added in the database.
Example:
The database holds 0 orders, and 3 items.
I add one order with 2 items.
Now the database hold 1 order, and 5 items. The two items in the order has been "readded" to the database, even though the items had the right primary keys before db.SaveChanges().
I realize that i may be able to attach the existing items to the dbcontext before saving changes, but is that really the only way to go? Can't EF figure out that to item already exists when the primary key matches an existing item?
Does anyone know if this is different in the new version of EF CodeFirst?
No EF cannot figure if entities are existing one or new one - both Add and Attach commands are graph oriented operations. You call them on one entity in the graph and they traverse all relations (and their relations and so on) and perform the operation for them as well.
You must figure correct state of each entity in the graph for example by using:
dbContext.Orders.Add(newOrder);
foreach(var item in newOrder.Items) {
dbContext.Entry(item).State = EntityState.Unchanged;
}
dbContext.SaveChanges();
You can use the reverse operation by calling Attach(newOrder) and set the order to Added state. The main difference will come with independent associations (for example many-to-many relations). The first approach will correctly add new relation between order and each item whereas second will not unless you manually set each relation to Added state (and changing state for relations is more complex).

How to map association for Navigation property to select but not update?

I have a navigation property of an entity mapped to another table (a "link table" to enabled a many to many relationship).
This selects the data into the navigation property.
To update it I have written an SP to update this link table, which now exists in Function Imports in the model, which I can call, as it is exposed on the context.
However, updating the entity and saving causes an exception:
Unable to update the EntitySet 'setName' because it has a DefiningQuery and no element exists in the element to support the current operation., since I have not mapped a function for Insert as I am calling my imported function on the context.
Is there a way to update the entity's Association to only select from the link table and leave the update/insert to handled by other code?
This has been solved by mapping the link table to an entity in the EDM, associating it appropriately (with Navigation properties), including the SP in the EDM, mapping the SP to the Insert function and unit testing.
Seems to be happy.

Adding object to navigation property collection creates new entity

I am using Entity Framework 4.
I am trying to associate a new entity with an existing entity. The system ends up creating a new child entity when in fact I just want to add a reference to the child object to the parent.
There is a many to many relationship between the two entities so I cannot simply set the FK property of the parent entity. I have tried parent.ChildCollection.Add(child) which simply creates a new child object in the database. This is what I am trying to avoid.
I must be doing something obviously wrong.
thanks
updated code sample
Code sample for my Self-Tracking-Entities that I have to do client side
Right now I have something like this to get all children from server then loop through to find the one i want, then add it to the object collection
List<Service.Child> childs = _client.GetChildren();
I have to loop through that collection to find the right one to add to the parent.childs collection ie.
List<Service.Child> childList = new List<Service.Child>();
foreach (Service.Child child in childList) {
if (child.ChildId == childId)
childList.Add(child);
}
contact.Childs = childList;
If an entity originally came from the database and has its own EntityKey properties populated, using Add to link it to another entity will change its EntityState to Added. Even though it is a preexisting entity, SaveChanges will create an insert command for this entity. You should consider using Attach instead:
parent.ChildCollection.Attach(child);
Using the Attach method, you can define relationships between entities that already
exist in the ObjectContext but that have not been connected automatically.