Here are some notes from the last time I implemented a many-to-many relationship using NHibernate. Because we are talking about mappings and Hibernate Query Language (HQL), I think most of what I'll discusss will apply to Hibernate in general, not just NHibernate.
Setting up a many-to-many relationship is pretty well documented in the Hibernate documentation. However, there was some lack of description on how to retrieve objects.
Let's consider two objects, User and Site. Each object has an ArrayList of the other in the business object.
There is a USER_SITE table that contains three columns, id, user_id, and site_id
The mapping User.hbm.xml contains
<bag name="Sites" table="USER_SITE" inverse="false" lazy="true" cascade="none">
<key>
<column name="iser_id" sql-type="int" not-null="true"/>
</key>
<many-to-many class="WWW.Site, DAO">
<column name="site_id" sql-type="int" not-null="true"/>
</many-to-many>
</bag>
Likewise, Site.hbml.xml contains
<bag name="Users" table="USER_SITE" inverse="false" lazy="true" cascade="none">
<key>
<column name="site_id" sql-type="int" not-null="true"/>
</key>
<many-to-many class="WWW.User, DAO">
<column name="user_id" sql-type="int" not-null="true"/>
</many-to-many>
</bag>
This is all well documented, but how do you pull out objects of this relationship. Turns out you can use the HQL (Hibernate Query Language) with join to consume these relationships.
Here is some simple code from my SiteManager.
public IList LoadAllByUser(User u) {
IQuery query = null;
query = ORMSession.CreateQuery("from Site s join s.Users as user where :user = user");
query.SetParameter("user", u);
return query.List();
}
If you return from the second HQL statement, the result you get back will by of type Object[] with each of your business objects loaded as entities in that array.
I am developing a little web application for my wife and I to use (I'm also writing essays about it's creation). As often happens during early development, I realized there was something about the application's foundation that I didn't like. Specifically, I realized I had created a bunch of business objects relying on java.util.Set instead of java.util.List. I wanted to keep charging ahead with development (I'm at the point where the core app is really coming into focus), but recognized that this is the most crucial time for this app. I could build on the shaky foundation, or I could stop development and fix the error, thus revisiting a lot of functionality that had already been done. I've been down this unsolid foundation path (on other apps and in business) before, so this time, I decided to fix it now.
What I realized as I was fixing it was, this should have been much more difficult. I realized how much I appreciate the java.util collections framework. Because those two object types follow the same abstract implementation, I was able to address the architecture change in about 20 minutes. This could have been a much more difficult change. I know this isn't mind-blowing, but I realized I should stop to appreciate when things are easy and not just when they're difficult.
Oh an here's a great Stroustrup quote:
"There are only two kinds of [programming] languages: the ones people complain about and the ones nobody uses"
-Bjarne Stroustrup
Java, Ant, Hibernate, WebWork, Spring, Freemarker, Log4J, Eclipse, XDoclet, and Tomcat. This series of articles will demonstrate how to put together some of the best Java frameworks available in order to create an easily managed, flexible web application. This is the most current way that high performance Java applications are written and deployed in the real world. This series will assume you have familiarity with Java and the JVM. It is intended for intermediate coders who are looking for an understanding of how Java applications are created in the real world. .NET developers can get an understanding of some of the technologies and methodologies used to create standards based applications in the Java world.
Part 1, this article, will give some background in the various projects and how they will benefit you. Later articles will focus on the various different pieces as they are incorporated into our application (don't worry, it is _not_ going to be an online shopping cart ;).
A weblog focusing on software development, computer science, mathematics, music, and probably lacking focus.
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|---|---|---|---|---|---|
| << < | > >> | |||||
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | ||