I am using Hibernate 2, and won't be able to upgrade to 3 for a few months.
I have many Sites, each Site has many Users.
I have many Courses, each Site can be assigned to zero or more Courses. Within each Site, a Site administrator can assign zero or more of the Site's courses to each User.
When a Site administrator logs into the software, I want a list. Each entry in the list would contain a User and each Course (for that Site) that they are permitted to take.
Bad Option 1: Get a list of all Users for a Site. Iterate through the list, and for each user do Hibernate.initialize(user.getAssignedCourses());
I believe this is classic "n+1 selects" problem, and involves one query per User plus one query to get the user list.
Slightly Bad Option 2: Get a list of all Users joined with Courses for a site.
e.g. session.createQuery("select us, cu from CourseUser cu join cu.user us ...") That's only 2 queries to get all of the data - one for the list of Users and then one for the User and Course combination, but then I have many duplicate User objects in my List result. I also have to iterate through the entire list and transform the data into the format I want.
Slightly Bad Option 3: Get a list of all Users for the site, and a list of all Courses for the site. Generate an SQL query with many table joins that returns a result set with columns: user_id, has_course1, has_course2, has_course3, ... Then I get all of the data I want in the format I want, but it's not mapped to a Hibernate object so I have to take extra steps in my code to save changes back to the database.
I'm sure there's some simple piece of Hibernate functionality I'm missing that can breeze through this. Any suggestions?
|