I am working on a Spring REST application where we have RequestMappings with nested path variables like so...
/grandparent/{grandparentId}/parent/{parentId}/child/{childId}
In my database there is a fk constraint from
child => parent
parent => grandparent
so a child knows its parentId and a parent knows its grandparentId
does Spring provide some easy way to validate that all of these are associated with one another?
If I had a request for
/grandparent/1/parent/2/child/3
I would want to ensure that child had a fk with parent and that parent had an fk with grandparent.
Ideally I'd like to abstract this away in the most elegant way possible and my intuition tells me there is a baked in way to handle this situation, I just need to be pointed in the right direction.
Thanks!
Related
I have an entity that has a Set of child entities. Can I add/remove some of the children in the collection in Hibernate's org.hibernate.Interceptor.onFlushDirty()?
The documentation (javadoc, JBoss userguide) doesn't explicitly mention changing relationship state, so I just want to make sure it's legitimate.
Update
Decided to describe by problem.
There's a Parent entity that has a Set of Child entities. The Child entities are created based purely on fields of the Parent entity, no additional information needed. The children need to be updated each time before persisting the Parent.
I wanted to do it in Interceptor.onFlushDirty() by clearing existing children and adding new reclaculated (i.e. Transient) children, but I'm getting the TransientObjectException. As I understand, it's because I add transient Child entities during flush, but I'm not sure.
I've read the docs and I cannot seem to figure out how to structure my GraphQL for a particular query. For my data I have:
child
|_ school
|_ class
A child has schools and schools have classes, but a child is only assigned to specific classes in a school.
I want to query a specific child to get only the classes they are in.
query={
child(id:$id){
schools{
name
classes{
name
}
}
}
}
I can technically filter the classes while resolving the schools field in the child type by looking deep down the fields but I wanted to clarify that this is still conforming to GraphQL. Should I be placing the classes as a field in the child type instead?
A child also have classes, so it makes sense to create a classes field in the child object. This, in addition to the classes field in the school object.
Of course you could also filter the classes while resolving it but it's just another extra work with no particular reason.
Since there is a direct relationship between a child and his classes it seems better for a child to have a classes field.
I have an entity(ex: Document) that is used as child in 4 other entities(using #OneToMany with #JoinTable in parents). I am not using Bidirectional Mapping. My requirement is to remove the Child(i.e Document), and I have two ways to do that, one way is, get the 4 parents, remove child from them and update them. Second, using native query(using jdbcTemplate) to remove entry from 4 join tables and remove the child.
Is there any other way it can be done in much simpler manner?
Create an abstract base class containing the Document as member and user JPA inhertiance --> http://en.wikibooks.org/wiki/Java_Persistence/Inheritance
Than it should be possible to get all users of a document with just one query.
Than it should be relatively easy to remove all references.
Don't do magic behind automatic deletion stuff. Thats for the cost of documentation.
Add orphan deletion (ie. delete child object when it's removed from collection in the parent). To enable it, you need to add
#OneToMany(orphanRemoval=true)
in owning entity.
So im using EFBulkInsert https://efbulkinsert.codeplex.com/
The problem is I have child objects I need to set the ID of the inserted parent objects.
Previously after inserting the parents objects I've tried to rely on the context to return the id's and set them on the child objects - then use EFBulkInsert to insert the child objects - every now and then the context gets confused even after recreating the context and I get the wrong id on the child objects.
Does anyone have a good pattern / strategy for setting the parent id on the child object I should mention i'm doing this for a batch of 1000 parent objects. So I don't particularly want to get the id's from the database after SaveChanges for the parent object unless it's performant.
The best idea I have is to add two temporary columns, firtst to hold an original Id, and second to hold parentId. After bulk insert update proper columns. This method requires privilages to modify the table.
I had a similar problem when I couldn't modify database, so I set AutoDetectChangesEnabled and ValidateOnSaveEnabled to false, but results were not very satisfied.
I am trying to implement a multi level library. There are only single parent nodes:
Parent1
- Child1
- Child2
Parent2
- Child1
- Child2
I started by implementing this using a link table
LibraryItem -< LibraryItemLink (FkParentId)
-< LibraryItemLink (FkChildId)
Which is fine, and powerful since it allows for multiparent nodes as well as multichild nodes. However I have got into difficulties when trying to delete parent library items. Ideally one should put a "Cascade Delete" on both of the constraints to cover parent or child node deletion, but SQL Server does not like this as it think cyclic deletion may happen. So I wondered whether I was making life to tricky for myself and instead should use a self referencing association to LibraryItem.
LibraryItem -< LibraryItem(Children)
Would this be easier to implement, especially from a CRUD perspective within the MVC3/EF5 world?
The final option is 2 table for 2 levels which is much easier, but more limited.
Thoughts much appreciated.
I would go with a PARENT_ID column in the LibraryItem table, allowing for NULLS and a FK back to the PK of the same table.
For your cascading delete issue, I would use a INSTEAD OF DELETE trigger on that table that;
Deletes all reocrds where the PARENT_ID is the PK of the row being deleted
Then delete the record of the parent itself.