Delete one side of one-to-one relationship and null out its reference from the other side - asp.net-mvc-3

I have two entities which can be related, but they can both exist without having each-other as well. In essence they are both 1 to 0..1.
Entity B can have an Entity A created from it - when this is done it establishes a relationship so that Entity B has 1 Entity A. Like-wise, since it can return to being optional, the user must be able to delete entity A without deleting its parent Entity B.
In the database my Entity A does not have an Entity B foreign key, so deleting Entity B would never be a problem.
Entity B, however, has a nullable field to hold an Entity A foreign Key. So far I've been able only to get Nhibernate to leave the bad key in the table, or delete the associated row entirely when you delete it's associated Entity A.
Long story short, if I click delete on Entity A, it should null out the reference to it in the Entity B table, should one exist. How can I go about this in Fluent Mapping?

Related

Entity Framework db first: states machine associations

I want to manage a simple state machine with EF.
I have a registry table of states and I'm supposed to have a table with two fields: an ID of a starting state and an ID of a destination state.
The associations table should have distinct couples of starting and destination ids.
I tried to make both of field as primary key and as foreign key referring to the registry table, but it's bad imported into the edmx model.
The registry state is like this:
ID DESCR
1 A
2 B
3 C
the associations table should be like this:
ID_STATE_START ID_STATE_DEST
1 2
2 3
So, in this case I can say that a process can go from A to B and from B to C.
EDIT:
This is the model VS2017 created for me:
I have two tables in db, but VS imported just the registry with two references on its own.
By the way this is not importan since i just need a right solution.

How to create a rollup field on a related entity?

Perhaps I'm overcomplicating how this is represented in Dynamics 365 because on paper it seems simple to me. I have the image representation of the setup below:
I want to create a rollup field on Entity C that counts all records of Entity A that have the same Shared ID as a record in Entity C. I would then like to filter that result down based on field values on Entity A. I have tried creating a 1:N relationship from Entity C to Entity A, but I'm not sure how I can specify that Shared ID is the common field between the two entities. How can I go about creating such a relationship and then filtering the data down further?

Foreign key empty on Envers audit table for relation OneToMany

I have the following relation and the foreign key is always empty in the audit table after the new revision:
#ManyToOne
#Audited(targetAuditMode=RelationshipTargetAuditMode.NOT_AUDITED)
#JoinColumn(name="mail_iid")
#private Mail mail;
...
#OneToMany(cascade=Cascade.ALL, orphan = true, fetch= fetchType.LAZY)
#JoinColumn(name="mail_iid")
private List<Attachments> attachments;
After the insertion of a new register, the original table have the iid but not the revision one.
Somebody knows about this issue.
There is only one way for this to happen, which is not managing bidirectional relationships properly.
I suspect you are never calling Attachments#setMail to assign the newly created Mail entity to the Attachments entities and instead simply add the Attachments entity to the collection that your Mail entity cascades.
This type of one-sided maintenance of bidirectional relationships is wrong and can lead to really incorrect results, particularly if entity instances are being inspected from the 1LC and are never being refreshed from the database; which is precisely why you're seeing the audit table with null in your mail_iid field.
Your code should make sure that both sides of the relationship get set properly
// setup bidirectional mappings
attachments.setMail( mail );
mail.getAttachments().add( attachments );
When you do it this way, you'll end up with mail_iid being populated in your audit table as you would have expected and also avoids any issues when traversing cached instances of an entity's object graph that is already loaded in the 1LC.

Treat Entity with Id NULL as NEW

To the question "Save Differences with Entity ID" I found the following answer:
"For Entities, Id property cannot be null, so you need to map this class as ValueObject. If so,
Id property is treated as regular property and it not goes to GlobalId of this object."
My question is:
Why can't an entity be treated as NEW if the Id is NULL?
I have an object graph that is fetched from the database, and between two javers commits an entity is added to a list in the graph.
Two commits and in the second commit there is a new entity (Id NULL)
Get the change => exeption because Javers can't create a GlobalId.
I can get arround this by doing EntityManager - persist (creates Id:s), but I would like to avoid doing that. The present code may do a persist later or it just lets the transaction finish.
Because the Id is NULL, the entity is NEW. Would it be possible to generate a unigue temp Id (allow Id = NULL) to be able to create the GlobalId?
In the change list, the entity would be reported as NEW. No need to compare with earlier commits.
You should compare/commit your objects when they are fully initialized so when they have Ids.
An entity without Id can't be handled by JaVers for several reasons:
it can't be compared to other entity/version (diff algorithm is based on GlobalIds)
it can't be queried from JaVersRepository (queries use GlobalIds)
If you are using Hibernate, compare/commit your new objects after Hibernate assigns them Ids from sequences.
Another options:
don't use sequence-generated values as JaVers Id but some business identifiers
if an Entity doesn't have a business identifier you can generate UUID in a constructor and use it as JaVers id (and also database PK if you like)

Hibernate ManyToMany Relationship from another Transaction

I have a method that creates a #Transactional and fetches some entities A and creates some entities B that have relationship to A.
Inside this method, I need to persist an entity C that has a #ManyToMany relationship to A, however this persistence needs to happen no matter what, so in the save method of C's repository, I add a #Transactional(propagation=Propagation.REQUIRES_NEW).
The problem is when I try to persist C, the A's in the relationship are in another Session.
Is it possible to save C and it's ManyToMany relationship table without attaching A to the session? All the A's in the relationship have have been already persisted hence they have ids, and that is all I need to persist in the auxiliary table.

Resources