-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Mapping class twice - once for CRUD, second for reporting
PostPosted: Wed Aug 19, 2009 6:40 am 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
I'm finding that with a number of hibernate mappings I have a simple version of an entity defined for basic CRUD operations, and then I really need an expanded version of the same entity that includes all of its associations for reporting process optimization.

For example: I have an entity (e.g. course) that has a number of related sub entities (e.g. attendees, class times etc) but I don't want to include these sub entities as properties in the main entity class structure.

To get around this I'm using the following class hierarchy...
Code:
AbstractCourse
    |---- Course
    |---- CoursePlusBookings

public abstract class AbstractCourse {
   private int CourseID;
   private String Title;
   private String Teacher
}

public class Course extends AbstractCourse {
}

public class Attendee {
   private int AttendeeID;
   private int CourseID;
   private String StudentName;
}

public CoursePlusBookings extends AbstractCourse {
   private Set<Attendee> Attendees;
}


Now for all entity level CRUD operations I'm using 'Course' and 'Attendee' separately to add entries to the underlying database. However, for reporting purposes I want to use 'CoursePlusBookings, which allows me to show a list of courses with the number of bookings.

Now the only way I can find to support this approach is to have two separate mappings - one for 'Course' and a second for 'CoursePlusBookings' - both of which map to the same table. I can't find a way of implementing mapping inheritance to avoid this duplication. I did raise this question sometime ago (see viewtopic.php?f=1&t=984333) but unfortunately the respondents misunderstood my question, so I'm asking it again.

I suppose in a way this posting boils down to a question about whether it is possible to have mapping element associations that are read only in nature. In otherwords if persisted CoursePlusBookings then the Attendee set would not be persisted as part of that operation.

Or is there another way to achieve what I'm after?


Top
 Profile  
 
 Post subject: Re: Mapping class twice - once for CRUD, second for reporting
PostPosted: Wed Aug 19, 2009 1:04 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
Hi davout_uk,
after looking at the cross post, and all big shot guys trying to answer.. I dont know how much i will be helpful. But I did went through this scenario and here are my 2cents.
This is what I had to do assuming similar scenarios like yours. There is no mapping needed for the Reporting Entity CoursePlusBookings it will be a stand alone POJO to hold data.
CoursePlusBookings will look something like this:-
public CoursePlusBookings extends AbstractCourse {
//dont know how the AttendeeID is mapped to the database so make the data type as java.lang.Number instead of int.
private int AttendeeID;
//optional.
//private int CourseID;
private String StudentName;
//And the set too to make you wonder why we need this..
private Set<Attendee> Attendees;
...
}
Write a SQL query that would return the data that you need for the reporting purposes. And the data would be returned something like this:-
CourseID Title Teacher AttendeeID StudentName
1 java mark 10 student1
1 java mark 11 student2
2 advjava brad 12 student3
2 advjava brad 13 student4
// unfortunately transformers do not do a compareTo operation on the POJO's defined even if they have an implementation.
List<CoursePlusBookings> objects = HibernateUtil.getSession().createSQLQuery(<<your query here>>).setResultTransformer(
Transformers.aliasToBean(CoursePlusBookings.class)).list();

your list of CoursePlusBookings will have the data as returned in the query results. Use some kind of logic to shrink the data to fit into what you need. I can post the logic too but before that want to make sure this is what you want.

Hope this helps,
-Srilatha.


Top
 Profile  
 
 Post subject: Re: Mapping class twice - once for CRUD, second for reporting
PostPosted: Thu Aug 20, 2009 2:50 am 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
Thanks for the reply, but it looks like I'll have to stick with the duplicate mapping solution.

In the meantime I've posted an enhancement request to address this area.


Top
 Profile  
 
 Post subject: Re: Mapping class twice - once for CRUD, second for reporting
PostPosted: Thu Aug 20, 2009 10:43 am 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
Another idea...

Is there a way to make the sets read only? In that the sets are not included in any INSERT or UPDATE actions.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.