aaronyeo22
asked on
hibernate one to many with 3 table
table 1:collage
table 2: students
table 3 : teachers
one collage, multiple student and multiple teacher. how to do in hibernate one to many.
table 2: students
table 3 : teachers
one collage, multiple student and multiple teacher. how to do in hibernate one to many.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You use 2 different collections mapped in nHibernate to load/update the data from/to the data store and you create a 3rd collection that exposes the 2 internal collections. The third collection isn't a real collection -- it's just an implementation of the interfaces the client code will expect (IList, IEnumerable most likely). Actually, that's all a real collection is, my point is that it doesn't have its own data store.
If you look at my modificataions to the mapping example you provided, you'll notice that I renamed the one-to-many maps to give each a different name. So what you are doing is telling nHibernate to map it as if there are 2 tables (which there are) and telling the client that there is a single collection that acts as a factory to return the appropriate object type. You need to write the code in the middle that makes it appear to the client that there is a single collection.
e.g.
Code:
public class InvoiceItems : IEnumerable, IList
{
/* in the implementation, use the 2 internal collections
as the data source for InvoiceItems.
*/
}
I should also point out that it may be possible to do what you are trying to do in nHibernate without adopting my approach. I don't know enough about it to be able to say it isn't in there.
https://forum.hibernate.org/viewtopic.php?f=25&t=951879