-->
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.  [ 11 posts ] 
Author Message
 Post subject: select - inheritance problem
PostPosted: Tue Dec 12, 2006 3:31 am 
Newbie

Joined: Wed Nov 15, 2006 4:47 am
Posts: 11
Hi! I am i newbie and i have a problem with this (which honestly seems quite simple, but still...!):

Concerning the reference's example of the Cat and the DomesticCat subclass,
I want to retrieve with one query (for training purposes) all of the Cats (including the Domestic). But i want to have in the results the "name" property of the Domestic Cats as well, of course only for the Domestic Cats.

I have had several problems trying to do that, including having to retrieve the DomesticCats twice, some casting problems and several NullPointerExceptions when trying with joins(which also have to do with casting).

Could you please point out a solution, or a direction towards it.
I know I am just stuck, but it's for good!

For those who dont remember the Cat example:

class Cat{
//data members

//geters and setters
}

class DomestcCat extends Cat{
String name;
getName(),setName
}


The mapping:

<class name="Cat" table="cats" discriminator-value="C">
<discriminator column="subclass" type="character"/>
....
....
<subclass name="DomesticCat" discriminator-value="D">
<property name="name" type="string"/>
</subclass>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 4:58 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Can you post HQL query and code you trying to use?

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 5:22 am 
Newbie

Joined: Wed Nov 15, 2006 4:47 am
Posts: 11
There is nothing much about the code. Simply trying to fetch all the records, including the name of the cat, which only exists for DomesticCats. The following, of course retrieves all the Cats, but doesn't retrieve the names of the Cats that are Domestic.

ses = HibernateUtil.getSessionFactory().getCurrentSession();
ses.beginTransaction();

String SQL_QUERY ="from Cat";

cats = ses.createQuery(SQL_QUERY).list();
pageContext.setAttribute("cats",cats);
ses.getTransaction().commit();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 5:25 am 
Newbie

Joined: Wed Nov 15, 2006 4:47 am
Posts: 11
There is nothing much about the code. Simply trying to fetch all the records, including the name of the cat, which only exists for DomesticCats. The following, of course retrieves all the Cats, but doesn't retrieve the names of the Cats that are Domestic.


The code you asked for:

ses = HibernateUtil.getSessionFactory().getCurrentSession();
ses.beginTransaction();

String SQL_QUERY ="from Cat";

cats = ses.createQuery(SQL_QUERY).list();
pageContext.setAttribute("cats",cats);
ses.getTransaction().commit();

Of course i have tried many other queries, which i don't even remember know...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 5:33 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
And when you recive an exception or casting problems?

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 6:36 am 
Newbie

Joined: Wed Nov 15, 2006 4:47 am
Posts: 11
When i use simple queries, like the one above, everything works fine. As i said before the issue is which is the right query to get all Cats including a column with the names of the DomesticCats.

Please point out the right query.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 6:55 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Sorry, if i don't understand the problem, but with this simple query ("from Cat") you must recive superclasses (Cat), wich has no name property, and subclasses (DomesticCat) with actual retrivied name property!

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 7:14 am 
Newbie

Joined: Wed Nov 15, 2006 4:47 am
Posts: 11
First of all thank you so much for tryinh to help.
(i think i' ll credit you anyway!!!)

After some hours of being confused, i see that the trouble is how to read the "name" property of the subclass.

the error i get when i try to print the name of a DomesticCat is:

""javax.servlet.ServletException: Unable to find a value for "name" in object of class "hibernate.Cat" using operator "." ""

Here is most of the code in my .jsp:

<body>

<%
Session ses=null;
List cats=new ArrayList();
try{
ses = HibernateUtil.getSessionFactory().getCurrentSession();
ses.beginTransaction();
String SQL_QUERY ="from Cat";
cats = ses.createQuery(SQL_QUERY).list();
pageContext.setAttribute("cats",cats);
ses.getTransaction().commit();
}
catch (Exception e){
System.out.println(e.getMessage());
}
%>

<b>CATS</b><br>
<table border="1">
<tr><th>ID</th><th>Birthdate</th><th>Sex</th><th>Weight</th><th>LitterId</th><th>Mother</th><th>Name</th></tr>
<c:forEach var="row" items="${cats}">
<tr>
<td>${row.id}</td>
<td>${row.birthdate}</td>
<td>${row.sex}</td>
<td>${row.weight}</td>
<td>${row.litterId}</td>
<td>${row.mother.id}</td>
<td>${row.name}</td>
</tr>
</c:forEach>
</table>



I have tried among others to put the "${row.name}" inside an if statement, where i check the class of the currently extracted object of the "cats" list. However this doesn't work, and i get the feeling it's not the right way to go. It has to be simpler and more direct...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 7:45 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
So, it seems, the problem not in Hibernate, but in the web layer. Because you put collection of Cat instances into pageContext, you recive an exception when you trying to acces undefined property (name). What if the part of your code will be little different (i hope, that you use jsp only for testing purposes, in other case consider some of the web mvc frameworks):
<%@ page import="somePackage.CatUI" %>
<%
Session ses=null;
List cats=new ArrayList();
try{
ses = HibernateUtil.getSessionFactory().getCurrentSession();
ses.beginTransaction();
String SQL_QUERY ="from Cat";
cats = ses.createQuery(SQL_QUERY).list();
ses.getTransaction().commit();
List catsUI = new ArrayList();
for(Iterator it = cats.iterator(); cats.hasNext();) {
Cat cat = (Cat) it.next();
CatUI catUI = new CatUI(cat);
catsUI.add(catUI);
}
pageContext.setAttribute("cats",catsUI);
}
catch (Exception e){
System.out.println(e.getMessage());
}
%>

<b>CATS</b><br>
<table border="1">
<tr><th>ID</th><th>Birthdate</th><th>Sex</th><th>Weight</th><th>LitterId</th><th>Mother</th><th>Name</th></tr>
<c:forEach var="row" items="${cats}">
<tr>
<td>${row.model.id}</td>
<td>${row.model.birthdate}</td>
<td>${row.model.sex}</td>
<td>${row.model.weight}</td>
<td>${row.model.litterId}</td>
<td>${row.model.mother.id}</td>
<td>${row.name}</td>
</tr>
</c:forEach>
</table>


The jsp then uses small Adapter class, CatUI:

package somePackage;

class CatUI {
property Cat model;

public CatUI(Cat model) {
this.model = model;
}

public Cat getModel() {
return this.model;
}
// use model.propertyName or delegate to model when displaying other properties

public String getName() {
if(model instanceof DomesticCat)
return ((DomesticCat) model).getName();
return "";
}

}

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 8:38 am 
Newbie

Joined: Wed Nov 15, 2006 4:47 am
Posts: 11
Thanks lester. That worked.
However, since I am just beginning with hibernate and the company i work for is still checking if we should move on with hibernate or not, are hou saying that hibernate should not be used in combination with jsp?
Are there any defects, according to your experience?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 9:00 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
JSP doesn't intend for data access or processing logic. Best use of JSP is formatting already available and prepared data. Good scheme is when you have Data Access Object (DAO) wich handle data acces, Service Layer ( possible with some AOP framework or EJB container) wich expose business methods to web layer, and some MVC web framework.

I know some big enough projects, wich use Hibernate succesfully. Hibernate really simplifies code for work with data. The main apprehensions i've heard was about perfomance, but perfomance is complex area and you always can use native sql query support in Hibernate. In almost all cases Hibernate performs well.

_________________
Best Regards


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