I have a hibernate issue where I have three layer of data, Parent-Child-Grandchild, Parent to Child one to many and child to grand child one to many. I am unable to save data when I say parent save (hibernate session save method), I use cascade.ALL. I am able to save two layer, parent to child one to many. What I need to do in this case. I have one parent record which has two child records and each child record has three grand child record. Please help.
You need a Self Reference One-to-Many relationship. It does not matter how many levels you need.
Here is a good tutorial which can guide you:
http://viralpatel.net/blogs/hibernate-self-join-annotations-one-to-many-mapping/
Related
I know there are three types of inheritence strategies: SINGLE_TABLE, TABLE_PER_CLASS, JOINED. I think the best choice is JOINED if I want my underlying database to be normalized. However, let us consider this usecase: I have a Parent Entity and one Child Entity that extends this Parent Entity. The inheritence strategy I am using is JOINED. Now, from the front-end of the application I can add Parent and Child entities. When I do search from front-end to get all Child Entities I am able to get all the Child Entities. Now, when I try to do a search only on Parent Entity I see that the result set contains all the entities(Parent + Child) and I understand why this is happening. My question is that is there any way we can search/query for only Parent entities(excluding rows from Child entity) when I am using the JOINED inheritence startegy?
Newer versions of Hibernate optimize this, but you have to make sure you are not selecting the entity or use fields of the parent table.
I have a parant entity Project and child entity Task(onetomany relationship). I would like to know how update child right. All examples I found are with one table.Thanks
What exactly do you need here? if you want to update the child of current parent then iterate through the child list / set and then modify the appropriate child within that for each loop. You could also update your parent too. so here when you flush / commit your transactions your parent table and the child tables as well get updated successfully.
I am lil bit confused in hibernate,
Here i am having one parent object and this parent object (Suppose A is a parent and B is child object) has many to one unidirectional relation with child object B.
Now, I have a object A along with Object B . What i have done is , i saved all the child objects in database, so now every child objects are saved in db and have id .
Now i want to save parent object A for its related child object. How can i do that ?
Though it might be very simple for u guys, i am new to hibernate. so need lil help.
Thanx in advance. :)
Why are you saving parent and child separately? Set the child to parent and merge the parent. Underlying ORM technology will take care of rest. But just add cascade type PERSIST and MERGE (ideally PERSIST is enough, but MERGE handles updates as well) over the relationship in parent entity.
I'm looking for some suggestions for best practices around modeling the relationship between various entities and their documents (binaries such as PDF, TIFF etc). The entities are standard JPA/Hibernate stored in a PostgreSQL database. The documents themselves will be stored in MongoDb database.
The plan is to create a child entity to represent the document, which contains the id to the binary data to retrieve it as needed. But what would the relationship be?
If I simply created one if these document entities for each parent entity then a simple one to many relationship would work, but that seems to redundant.
I could simply put a "type" column that indicates which entity the document belongs to, and then query the document table with a named query of "id = ? and type = ?". I guess that would work, but there is something about that I'm not crazy about either - just can't put my finger on it :) Maybe that's just fine.
Another option I have looked at (although I admit I have never used it before, and would need to study it a bit more) is to use a unidirectional one to many with join table. However, I don't think this will work either since there is no guarantee that there wouldn't be duplicate parent keys. I use a single sequence for all basic relation tables primary keys, which should guarantee it, but it still doesn't sound like a good idea.
Finally, I have considered whether I create an entity and then extend it for each parent entity, but I think that would have the same flaw - the theoretical existence of non-unique parent ids.
Before I make a final decision, I'd like to see what other suggestions the community might have to offer.
Thanks in advance for your ideas.
If I simply created one if these document entities for each parent entity then a simple one to many relationship would work, but that seems to redundant.
I'm a bit confused. If you create a document for each parent, isn't that one-to-one and not one-to-many? It sounds like you want a one-to-many relationship. In which case, you would only create a single document for all parent entities that reference it.
I am trying to optimize a particular instance of an owned one-to-many relationship where the parent has a list of children where the list of children could be very large. Children may be created, removed, and edited.
Editing is actually ok as I can retrieve a child without ever retrieving the parent.
For deleting, I could retrieve the parent, remove the child from the parent's child collection, and persist it. However, I would really like to avoid the expense of loading the parent's entire list of children just to delete one child. But if I just delete the child directly, then one what happens to the reference to the deleted child in the List<> that the
parent maintains?
As for the creation of a child entity, it really seems that I have no choice but to load the parents entire list of children in order to append the new child.
If I want to avoid loading the parent's list of children in all these situations, are unowned relationships the only way to accomplish it? Does anyone have experience using primarily unowned relationships? Does it hurt?