Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Collection Mapping Problem

 
Nilesh Sanyal
Ranch Hand
Posts: 33
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create an application that will display list of books written by an author. For this I am using oracle 10g express edition database and Hibernate Framework.

The stacktrace displayed by browser is as follows...

type Exception report

message An exception occurred processing JSP page /DisplayBooks.jsp at line 22

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /DisplayBooks.jsp at line 22

19: <%
20: String aname=request.getParameter("author");
21: mypack.SearchHelper sh=new SearchHelper();
22: List booksCollection=sh.getBooks(aname);
23: for(Iterator itr=booksCollection.iterator();itr.hasNext();)
24: {
25: Books b=(Books)itr.next();


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)

root cause

java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode
\-[IDENT] IdentNode: 'bookname' {originalText=bookname}

PROJECT DETAILS
========================

I have created 2 tables which are as follows...

AUTHORS table
===============
Name Null? Type
----------------------------------------- -------- --------------------

AUTHORID NOT NULL VARCHAR2(4)
AUTHORNAME VARCHAR2(30)


BOOKS table
===============
Name Null? Type
----------------------------------------- -------- ------------

BOOKID NOT NULL VARCHAR2(4)
BOOKNAME VARCHAR2(30)
AUTHORID VARCHAR2(4)

Data for AUTHORS table
========================
AUTH AUTHORNAME
---- -----------
A1 Arvind
A2 Bikas


Data for BOOKS table
======================

BOOK BOOKNAME AUTHORID
---- ------------------------------ -------------------
B1 C Programming A2
B2 C++ Programming A2
B3 C# Programming A2
B5 J2ME Programming A1

SearchBooks.jsp
===============


DisplayBooks.jsp
===============


hibernate.cfg.xml


hibernate.reveng.xml


Authors.java
========


Books.java
=======


Authors.hbm.xml
============


Books.hbm.xml
===========


NewHibernateUtil.java
==============


SearchHelper.java
============
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all move the Java code to a Java file. Never write a single line of Java code in JSP. The structure of a Java web app is servlets do the work (connect to the database, execute queries,...), JSPs display the results. When I started in 2004 that was already the best practice and it didn't change. For more information read this excellent article and this topic.

Then I feel it will be very easy for you to identify the root cause of the exception.
 
Nilesh Sanyal
Ranch Hand
Posts: 33
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok sir, thank you
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After you have refactored your code, it will probably also be much easier to post a SSCCE (Short, Self Contained, Correct/Compilable, Example) instead of a bunch of (irrelevant) code snippets. Together with a stack trace of the exception, it will be much easier for us to help you and determine what's causing your problem and propose a solution.

But based on the stack trace, it seems something is wrong with your query. Try qualifying all properties of the classes in your query, e.g.Here you can find more information about JPQL.

And here's another tip! You should never concatenate a value (entered by a user) directly into your query. It's not only considered a bad practice, but also makes your code vulnerable to SQL injection attacks. You can find more info on SQL injection here. You should always (wherever possible) use a prepared statement. With JPQL, you can either choose between named parameters (as I used in the above example) or positional parameters. For more information about using parameters, have a look at the Parameters section of the Java Persistence WikiBook.

Hope it helps!
Kind regards,
Roel
 
Dave Tolls
Ranch Hand
Posts: 2103
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I'd posted last night, but it did lock up, so must have vanished into the aether.

Anyway (apart from the bits Roel has noted), that HQL query is returning only the bookname, but you seem to want a List of Books. I don't think that'll be the cause of the immediate exception, but it will fall over later when it tries to cast String to Book.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Anyway (apart from the bits Roel has noted), that HQL query is returning only the bookname, but you seem to want a List of Books. I don't think that'll be the cause of the immediate exception, but it will fall over later when it tries to cast String to Book.

That's indeed a valid remark. If you want to have a list of Books objects, you should change the SELECT part of your query toAnd another minor remark: according to the naming conventions, a class name should be a noun (e.g. Author and Book). And that makes more sense than using e.g. Books. When you retrieve one single object (book) from your database, your code looks like thisIt clearly indicates one single book, whereas the following code snippet might suggest it's not one book but a collection of books
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic