-->
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.  [ 8 posts ] 
Author Message
 Post subject: newbie : Trying get associated groups in Set, Urgent!
PostPosted: Fri Feb 10, 2006 7:32 pm 
Newbie

Joined: Fri Feb 10, 2006 7:19 pm
Posts: 6
My User class has a Set of groups. In the table of course there are multiple rows representing this association, however when I select all users I want to consolidate all groups in one single Set. Following is the information about my class and mapping file. I am faily new in ORM space, just started a week back and have deadline next week. I don't understand what went wrong, it was working until I added that association. Any help will be greatly appreciated.

Thanks,
Meghana

-------------------------------------------------------------------------------

Hibernate version: 3

Mapping documents:
------------------------ User.hbm.xml -----------------------------------
<hibernate-mapping>
<class name="com.serena.dashboard.security.users.User" table="USERS">
<id name="id" column="ID" />
<property name="userName" column="USER_NAME" />
<property name="fullName" column="FULL_USER_NAME"/>
<set name="groups" inverse="true">
<key column="MEMEBER_USER_NAME_KEY"/>
<many-to-many class="com.serena.dashboard.security.users.Group"/>
</set>

<property name="email" column="EMAIL_ADDR"/>
</class>
</hibernate-mapping>

---------------------------------- User.java -------------------------------
package com.serena.dashboard.security.users;

import java.util.Set;

public class User {
private int id;
private String userName;
private String fullName;
private Set groups;
private String email;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

public String getUserName() {
return this.userName;
}
public void setUserName( String userName ) {
this.userName = userName;
}
public String getFullName() {
return this.fullName;
}
public void setFullName( String fullName ) {
this.fullName = fullName;
}
public Set getGroups() {
return this.groups;
}
public void setGroups( Set groups ) {
this.groups = groups;
}

//utility method not needed by Hibernate
public void addGroup( Group group ) {
if( group == null )
throw new IllegalArgumentException("Null Group");
group.getUsers().add( this );
groups.add( group );
}
public String getEmail(){
return email;
}
public void setEmail( String email ){
this.email = email;
}
}

Code between sessionFactory.openSession() and session.close():
hiberSession = HibernateUtil.getSessionFactory().getCurrentSession();
hiberSession.beginTransaction();
String hqlStatement = "select * from User as user where user.userName like '" + searchCriteria.getUserName()+ "' and user.fullName like '" + searchCriteria.getFullName()+ "' and user.email like '" + searchCriteria.getEmail() + "'";
List result = null;
try {
result = hiberSession.createQuery(hqlStatement).list();
}
catch( Throwable t ){
t.printStackTrace();
}
hiberSession.getTransaction().commit();

Full stack trace of any exception that occurs:
Caused by: java.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.re
cover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V
at org.hibernate.hql.antlr.HqlBaseParser.selectClause(HqlBaseParser.java
:1391)
at org.hibernate.hql.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1
106)
at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:70
2)
at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.j
ava:296)
at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:15
9)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.j
ava:238)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorIm
pl.java:155)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl
.java:105)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:74)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:53)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCa
che.java:71)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessio
nImpl.java:108)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImp
l.java:88)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1540)
... 66 more

Name and version of the database you are using:
Oracle 9i
The generated SQL (show_sql=true):
NA
Debug level Hibernate log excerpt:
NA


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 12, 2006 11:24 pm 
Newbie

Joined: Fri Feb 10, 2006 7:19 pm
Posts: 6
Can anyone please answer my question? I read 'hibernate in action' book and the code looks ok to me. Can someone please point out what am I doing wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 12:01 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
What is the exact hibernate version you're using? I can't find an org.hibernate.hql.antlr package in 3.0.5 or 3.1.1. Not even in 2.1...

I'm told that HQL has lost the "as" variant of HQL aliasing in 3.1.2, so if that's what you're using, try "from User user" instead of "select * from User as user".

I know it has nothing to do with your problem, but you should try using either Criteria or HQL + named parameters instead of that kludgey string building. It completely bypasses JDBC statement precompilation, which is useful if the query gets called more than once during the lifetime of a single JDBC connection.


Top
 Profile  
 
 Post subject: Re: newbie : Trying get associated groups in Set, Urgent!
PostPosted: Mon Feb 13, 2006 2:10 am 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
meghanai_99 wrote:
[b]Code between sessionFactory.openSession() and String hqlStatement = "select * from User as user where user.userName like '" + searchCriteria.getUserName()+ "' and user.fullName like '" + searchCriteria.getFullName()+ "' and user.email like '" + searchCriteria.getEmail() + "'";
List result = null;


NoSuchMethodError's like that are usually the result of incorrect versions of supporting libraries in your class path. In this case maybe the correct version of "antlr-XXXX.jar" but yes knowing which version of Hibernate you are using affects which dependant libraries you need.

If you can please dump a directory listing of the JAR files in your applications classpath, that might help identify if that is the problem.


I dont use "SELECT *" inside HQL (I even think its illegal???), but just omit it with similar statements, as I thought Hibernate has to be able to deal with a nice object rather than a random list of random columns from randomly joined tables that SQL can give you.

Code:
String hqlStatement = "from User as user where user.userName like '" + ...SNIP...


You are asking for a list of "User" objects.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 4:04 pm 
Newbie

Joined: Fri Feb 10, 2006 7:19 pm
Posts: 6
Thank you so much for your replies. I got correct antrl jar in the class path and removed 'select' and 'as' keywords from the query. That seemed to work for simple columns.

But I got 'LazyInitializationException' when it tried to get usergroups which is a 'Set' in my class for individual user. It seems when I try to get the group information in my JSP frontend, it tries to populate the 'Set' at that time so my 'session.commit()' is already called in the user manager's search method and session is lost.

Is there a way to override the 'Late Binding'?

Thank you,
Meghana


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 4:32 pm 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
meghanai_99 wrote:
Thank you so much for your replies. I got correct antrl jar in the class path and removed 'select' and 'as' keywords from the query. That seemed to work for simple columns.

But I got 'LazyInitializationException' when it tried to get usergroups which is a 'Set' in my class for individual user. It seems when I try to get the group information in my JSP frontend, it tries to populate the 'Set' at that time so my 'session.commit()' is already called in the user manager's search method and session is lost.

Is there a way to override the 'Late Binding'?

Thank you,
Meghana


JSP... ah lookup "Open Session in View" design pattern. I swear by it :)

What is happening is that you are expecting to be able to reference an object you found in your servlet from your JSP code.

The problem, the normal HibernateSession Singleton pattern people start out with only exists around the Servlet part (the Controller part) and by the time the JSP is executed (the View part) the session is closed.

You can't access proxied objects after you close the session.

http://www.hibernate.org/43.html


Other choices you have (since Open Session in View is not trivial to setup for the beginner):


Configure lazy settings in your map files: http://www.hibernate.org/315.html

If your application is simple maybe you can turn a proxied object into a detached one before you pass it to JSP. Infact if you find out how to do this please post back here :) but I can imagine the reasons why you cant, since do you deep-clone or not and if you deep-clone how do you deal with circular references backed by infinaly huge graphs. Minefield!



No Open Session in View is the way :)

Or be hardcore and create your own Data Transfer Objects for the presenation layer.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 4:35 pm 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
meghanai_99 wrote:
Thank you so much for your replies. I got correct antrl jar in the class path and removed 'select' and 'as' keywords from the query. That seemed to work for simple columns.

But I got 'LazyInitializationException' when it tried to get usergroups which is a 'Set' in my class for individual user. It seems when I try to get the group information in my JSP frontend, it tries to populate the 'Set' at that time so my 'session.commit()' is already called in the user manager's search method and session is lost.

Is there a way to override the 'Late Binding'?

Thank you,
Meghana


You can use threadlocal sessions and Open/Close sessions in filters.
In this way your session will be available in jsp's also.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 4:37 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
meghanai_99: There are lots of ways to do what you want. Perhaps the simplest in this case would be to use "join fetch" in your HQL:
Code:
from User user
  join fetch user.SetYouNeedToGet s
  where user.userName like :UserName
  and user.fullName like :UserFullName
  and user.email like :Email


dlmiles: select in HQL is fine. It's very useful when you're doing a cross join, or when the thing you want to return isn't the first class in the HQL, e.g.
Code:
select child from Parent p join p.Children child where p.Married = false
would select all children from one-parent families, or
Code:
select c from Home h, Child c
  where where h.Type = 'Trailer'
  and h.Owner = c.Parent
would select all children who live in trailers (assuming that parents live with their children and that only adults own homes ;) )


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