I have two tables (folder and document) which have a many to many relationship with a join table in between, this join table then also has another one to many relationship with a third table (document_title).
I want to be able to fetch a document_title from a document, what is the correct way to model this in hibernate?
The object model needs to be a Document that contains a DocumentTitle instance.
Thanks
I'm afraid what you're trying to do does not fit nicely into into OO world. I'd introduce thrid entity class as in-between. This third class can contain any attributes you like. So insted of many-to-many you would have two one-to-many relationships (or one-to-many and many-to-one).
Related
This can seem a stupid questions for some people, but I couldn't find any information anywhere why we should use mappings (#OneToOne, #OneToMany etc) in JPA while defining entity classes. I know one the advantage is code reduction, so that we don't have to explicitly write queries in order to fetch data from relationship tables. But is there any other benefit (from code optimisation perspective at SQL side) that we have?
The reason is to load object trees or graphs.
That's the goal of object-relational mapping to fill the gap between database tables and objects.
And as you said it reduces code.
In a summary the idea of the ORM is to map tables to objects, so the developer works with the objects instead of the tables.
Tables in SQL have relationships through key columns.
in JPA these relationships are expressed via #OneToMany, #OneToOne etc.
This means that if you want to fetch a row from one table and join that with corresponding row from another table (via a relationship) the JPA implementation (mostly Hibernate) can do that for you, looking at the relationships you have defined for your entities.
You need to describe the entities relationships because it's part of the DB schema model which you are mapping to application level objects. As you mention - it saves you writing the SQL queries yourself, but that's not the main point.
The main point is that you can model/represent one domain (database tables, rows, relationships, SQL commands) as another type of domain (objects/classes, OOP paradigm, programming language commands) which completely shifts the way you work with it.
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 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.
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 have an entity that has many-to-many association to itself.
If I needed some additional properties (like asked here) the answer would be to use a new intermediate entity. But without them is it bad practice to use direct many-to-many association to the entity itself?
Aleksey,
If you have additional columns, you need a new intermediate Entity.
But without them is it bad practice to use direct many-to-many association to the entity itself ?
No, you can if you want since you do not have additional columns on The joined Table
Using a mapping table is the optimal way for N to N relations without creating new columns in the mapped tables themselve.