Querying/Selecting rows from Parent Entity when using Hibernate JOINED inheritence strategy - java

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.

Related

Join multiple tables for one entity with JPA?

I was wondering if it was possible to create an entity that fetches data from multiple tables.
I have an entity based on a TableA.
The particularity is that I also need data from TableD, which is linked to TableC, which is linked to TableB finally linked to TableA with TableA id
I am fully aware that I could create an entity on TableB,C,D.
But I don't need ANY data on those tables except on TableD. B and C are just "a path to follow".
http://img11.hostingpics.net/pics/878537schema.png
I saw the #SecondaryTables annotation that I could use on EntityA but it seems like you can only join with the PK of EntityA.
If you know a solution that will prevent me from creating useless entities, I'd be grateful.
You can link each entity through inheritance strategy InheritanceType.JOINED. Then you can get all values of parent classes in child class.

save parent in hibernate with child

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.

three layer of data, Parent-Child-Grandchild

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/

How to model relationship between different parent entities and one common child 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.

How to filter entities by their parents in Many-To-One in AppEngine using JDO

I have two classes: Parent and Child. Parent has a List of Child objects and child object has a reference to it's parent using #Persistent (mappedBy = "parent").
How can I filter child objects by it's parents attributes?
I have tried query.setFilter("parent.key == " + parentKey); But that didn't work. Do I have to map parent's attribute to child somehow?
I'm pretty much asking the same as here How to filter entities by their parents in ManyToOne side in Google App Engine but using JDO not JPA.
You can't. This would require a join, which the App Engine datastore does not support. Instead, either fetch all the parents that match the parent criteria, then perform a separate query on each result for its children, or denormalize, and add the properties you care about to the child entity.

Categories