-->
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.  [ 10 posts ] 
Author Message
 Post subject: Mapping a read-only ‘view’ class in 1.2
PostPosted: Tue Jul 03, 2007 3:23 am 
Newbie

Joined: Tue Jul 03, 2007 3:21 am
Posts: 6
Location: Drachten, The Netherlands
Hello,

I have a question about mapping. In the project I am currently working on, I have a Contact class from which Person and Organisation derive using table–per-class inheritance. The mapping of those classes is working perfectly using the joined-subclass mapping.

The problem is that I need to implement a search algorithm which should return only a small subsection of the information stored in the Contact, Person and Organisation table. I have created a new class: ContactView. The class looks like this:

ContactView
Id (Contact table)
First Name (Person table)
Last Name (Person table)
Name (Organisation table)
Description (Contact table)

The class will be read-only.


The question is how do I map this class? I have found two possible solutions, but neither of them is perfect. I use version 1.2 of NHibernate.

Solution 1:
Get latest version of NHibernate from the repository and then use the <join> elements. I don’t like this because the version might be unstable. I know I can compile version 1.2 with the join patch, but again I don’t really like that either.

Solution 2:
I am not even sure if it works but: create a view in MS-SQL 2005 and use that view. I like this solution even less than solution 1.


Thanks in advance,
Bert Willems

_________________
Weblog: Dev @ Work


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 8:12 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
What you really want to do is your own custom HQL. Just add that class to mapping file as an import. Then create a constructor for that class which takes all parameters.

Then in your HQL statement you would do something like "SELECT new ContactView(<parameters>) FROM xyz". You would then just return the data you want in a read only container.

Is there any reason this wouldn't work for you?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 9:24 am 
Newbie

Joined: Tue Jul 03, 2007 3:21 am
Posts: 6
Location: Drachten, The Netherlands
First of all thank you for your response.

I tried your solution but I encountered some problems:

Problem 1:
When I import the ContactView class in the mapping file, I can't seem to reference it in other mapped classes.

Problem 2:
I want to use the Person and Organisation classes in my HQL query but how do I join them with the Contact class? It has to be a left outer join. NHibernate wants a path expression, but what is the path expression to a joined subclass?

_________________
Weblog: Dev @ Work


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 10:37 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
Trilobyte wrote:
Problem 1:
When I import the ContactView class in the mapping file, I can't seem to reference it in other mapped classes.


You can't. It's not a mapped class, but rather it is a "reporting object". Use HQL when you want this data.

Trilobyte wrote:
Problem 2:
I want to use the Person and Organisation classes in my HQL query but how do I join them with the Contact class? It has to be a left outer join. NHibernate wants a path expression, but what is the path expression to a joined subclass?


Use a different syntax. NHibernate will let you use the old school join syntax (of course you can't use outer joins though). use "FROM Person p, Organization o WHERE p.Id = o.pId"


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 10:46 am 
Newbie

Joined: Tue Jul 03, 2007 3:21 am
Posts: 6
Location: Drachten, The Netherlands
Thanks jchapman,

I don't really like your solution because it forces me to write a lot more HQL and C#. Are there any other solutions? Otherwise I will stick to my first solution, the one with the dev version of NHibernate which supports the <join> tag.

_________________
Weblog: Dev @ Work


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 11:06 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
Trilobyte wrote:
Thanks jchapman,

I don't really like your solution because it forces me to write a lot more HQL and C#. Are there any other solutions? Otherwise I will stick to my first solution, the one with the dev version of NHibernate which supports the <join> tag.


I would argue that you are taking the wrong approach. I think you are concerning yourself with performance at an all too early stage in the game.

In addition to using HQL you could also have a method on the Person which returned the detail you were interested in. Again this would be read-only, and you would have a simple way to return the data you need once you have a person. If you need the data from somewhere else, return the data in the format you need where you build the object from two parameters (the Person and the Organization).

There are many ways to handle this. Using HQL everywhere won't necessarily be the right way, but it is a tool which is available in addition writing your own methods.

I don't think you want to have a person mapped two different ways. One way as a Person and another way as a PersonContact. You violate the identity principle of the session, which may cause many issues further down the line for you. Instead use objects which are not part of the session!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 2:59 pm 
Newbie

Joined: Tue Jul 03, 2007 3:21 am
Posts: 6
Location: Drachten, The Netherlands
jchapman wrote:
I would argue that you are taking the wrong approach. I think you are concerning yourself with performance at an all too early stage in the game.

You have a point there, however the performance is not important to me right now, I am more interested in learning the best practices of NHibernate. Tomorrow I will rewrite the mapping to use the full Contact class instead of the ContactView class.

Just hypothetically: If the performance was a real issue, would the <join /> solution be the best option? I think it would or at least save me some time because now I have to write C# code to convert the data.


Again thank you very much, you have been very helpful!

_________________
Weblog: Dev @ Work


Top
 Profile  
 
 Post subject: write an sql query and map the object to it
PostPosted: Tue Jul 03, 2007 3:07 pm 
Newbie

Joined: Tue Jul 03, 2007 2:06 pm
Posts: 14
Location: Turkey, Ankara
You can simply write the sql query and map the results to the value class after then you can use nhibernate hql performance. :)


Top
 Profile  
 
 Post subject: Re: write an sql query and map the object to it
PostPosted: Tue Jul 03, 2007 3:25 pm 
Newbie

Joined: Tue Jul 03, 2007 3:21 am
Posts: 6
Location: Drachten, The Netherlands
hcfk wrote:
You can simply write the sql query and map the results to the value class after then you can use nhibernate hql performance. :)

That is exactly how I handle it right now, but I am not to happy with hard-coded sql queries in my code, just a preference, that is why I am looking for an alternative solution. Thank you for your response.

_________________
Weblog: Dev @ Work


Top
 Profile  
 
 Post subject: Re: write an sql query and map the object to it
PostPosted: Mon Jul 16, 2007 11:34 am 
Beginner
Beginner

Joined: Wed Nov 29, 2006 10:32 am
Posts: 34
We use Views on SQL Server and are happy with it.
Views are more stable than queries "somewhere" in a program (but Named Queries would be similarly stable ...); and when performance is critical, Views give you more possibilities.

IMHO, an OR mapping is NOT a tool to abstract away the database - for this, our databases are too complicated tools and at times also much more powerful. NHibernate is a tool to simplify coding - but handling dependencies between e.g. views and some "fast lane reader" business objects is well in the realm also of big projects (e.g. the software should check on startup whether the views can be read so that alrady at boot time you find out when there is a discrepancy between view and class/mapping definition).

(in our project of about 500 business classes, we have right now about 7 fast reader views ... so it's not a big hoopla anyway.)

Last, a technical issue: Dont forget to mark the view mappings in NHibernate as "mutable=false" - otherwise, some funny errors occur.

Regards
Harald


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.